Strip 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 character in the S string at the beginning of the RM delete sequence
S.rstrip (RM) Removes the character from the end of the S string that is located in the RM delete sequence
Note :
1. When RM is empty, the default is to remove the whitespace characters (including ' \ n ', ' \ R ', ' \ t ', ')
For example:
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:
Example:
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!!!
Split returns a list.
1. Divide by a character, such as '. '
1234 |
str = ( ‘www.google.com‘ ) print str str_split = str .split( ‘.‘ ) print str_split |
The results are as follows:
2. Divide by one character and divide n times. If you press '. ' Split 1 times
?
1234 |
str = ( ‘www.google.com‘ ) print str str_split = str .split( ‘.‘ , 1 ) print str_split |
The results are as follows:
3. Split by a string. such as: ' | | '
?
1234 |
str = ( ‘WinXP||Win7||Win8||Win8.1‘ ) print str str_split = str .split( ‘||‘ ) print str_split |
The results are as follows:
4. Divide by a string and divide n times. such as: Press ' | | ' Split 2 times
?
1234 |
str = ( ‘WinXP||Win7||Win8||Win8.1‘ ) print str str_split = str .split( ‘||‘ , 2 ) print 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
?
1234 |
url = ( ‘www.google.com‘ ) str1, str2 = url.split( ‘.‘ , 1 ) print str1 print str2 |
The results are as follows:
A summary of the usage of the Python strip () function and the Split function