Python Advanced---python Strip () split () function combat

Source: Internet
Author: User

Let's look at an example:
>>> ipaddr = 10.122.19.10File"", Line 1ipaddr= 10.122.19.10 ^syntaxerror:invalid Syntax>>> ipaddr ="10.122.19.10">>>Ipaddr.strip ()'10.122.19.10'>>> ipaddr ='10.122.19.10'>>>Ipaddr.strip ()'10.122.19.10'>>> Ipaddr.split ('.')['Ten','122',' +','Ten']>>> Ipaddr.strip (). Split ('.')['Ten','122',' +','Ten']>>>

Description of the Python strip () function

Function prototypes

Declaration: S is a string, RM is a sequence of characters to be deleted

S.strip (RM) Remove the characters from the beginning and end of the S string in the RM delete sequence

S.lStrip (RM) Delete the characters at the beginning of the S string in the RM delete sequence

S.rStrip (RM) Delete the character at the end of the S string in the RM delete sequence

Attention:

1. When RM is empty, the default is to remove the whitespace characters (including ' \ n ', ' \ R ', ' \ t ', ')

For example:

Copy CodeThe code is as follows:
'      123'>>> a.strip ()'123'>>> a='  \t\tabc'ABC'sdff\r\n' >>> a.strip ()'sdff'

2. The RM delete sequence here is deleted as long as the character on the edge (beginning or end) is within the delete sequence.

For example:

Copy CodeThe code is as follows:
' 123ABC ' >>> a.strip ('3abc ')'    The result is the same >>> A.strip ('a ')'  3ABC'

A summary of the usage of the Python split function (

Split usage of strings

Description
There is no character type in Python, only a string, the character here is a string that contains only one character!!!
The reason for writing here is just for the convenience of understanding, that's all.

1. Divide by a character, such as '. '

1 str = ('www.google.com')2print  str3 str_split = str.split ('. ' )4print str_split

The results are as follows:



2. Divide by one character and divide n times. If you press '. ' Split 1 times

1 str = ('www.google.com')2print  str3 str_split = str.split ('. ', 1)4print str_split

The results are as follows:

3. Split by a string. such as: ' | | '

1 str = ('winxp| | win7| | win8| | Win8.1')2print  str3 str_split = Str.split (' || ' )4print str_split

The results are as follows:

4. Divide by a string and divide n times. such as: Press ' | | ' Split 2 times

1 str = ('winxp| | win7| | win8| | Win8.1')2print  str3 str_split = Str.split (' || ', 2)4print str_split
The results are as follows:

5. Divide by a character (or string), divide n times, and assign the completed string (or character) of the partition to a new (n+1) variable. (Note: See opening instructions)
such as: Press '. ' Splits the character, splits it 1 times, and assigns the split string to 2 variables Str1,str2

1 url = ('www.google.com')2 str1, str2 = Url.split (' . ', 1)3print  str14print str2
The results are as follows:

An example of a regular match:

>>> str= "xxxxxxxxxxxx5 [50,0,50]>,xxxxxxxxxx"

>>> lst = Str.split ("[") [1].split ("]") [0].split (",")

>>> Print LST

[' 50 ', ' 0 ', ' 50 ']

Decomposition is as follows

>>> list =str.split ("[") divided by left

>>> Print List

[' xxxxxxxxxxxx5 ', ' 50,0,50]>,xxxxxxxxxx ']

>>> list =str.split ("[") [1].split ("]") contains and then splits on the right

Then the desired string is stored in the list according to the split pair.

>>> List

[' 50,0,50 ', ' >,xxxxxxxxxx ']

>>> str.split ("[") [1].split ("]") [0]

' 50,0,50 '

>>> str.split ("[") [1].split ("]") [0].split (",")

[' 50 ', ' 0 ', ' 50 ']

Transferred from: http://blog.csdn.net/feeltouch/article/details/49404671

Python Advanced---python Strip () split () function combat (GO)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.