python3.4 study notes (20) python strip() function to remove spaces\n\r\tUsage of function
In Python, there are three space removal functions in the string processing function (including '\ n', '\r', '\t', ' ') function:
strip removes the spaces on the left and right at the same time
lstrip removes the spaces on the left
rstrip removes the spaces on the right
Specific examples are as follows:
>>>a=" gho stwwl " >>>a.lstrip() 'gho stwwl ' >>>a.rstrip() ' gho stwwl' >>>a.strip() 'gho stwwl'
Statement: s is a string, rm is the character sequence to delete
s.strip(rm) deletes the s character The characters at the beginning and end of the string, located in the rm deletion sequence
s.lstrip(rm) Delete the characters at the beginning of the s string, located in the rm deletion sequence
s.rstrip(rm) Delete the s string At the end, the characters located in the rm deletion sequence
Note:
1. When rm is empty, whitespace characters (including '\n', '\r', '\t', ' are deleted by default ')
>>> a = ' 123' >>> a.strip() '123' >>> a='\t\tabc' 'abc' >>> a = 'sdff\r\n' >>> a.strip() 'sdff'
2. The rm deletion sequence here is to delete the characters on the edge (beginning or end) as long as they are within the deletion sequence.
>>> a = '123abc' >>> a.strip('21') '3abc' 结果是一样的 >>> a.strip('12') '3abc'
【Related recommendations】
2. Detailed explanation of strip() and split() in python How to use
3. Detailed explanation of the usage scenarios of strip function in python
4. The little-known traps of strip() in python
5. The wonderful uses of the strip() function in Python that you don’t know
The above is the detailed content of A basic introduction to Python teaches you how to use the strip() function to remove spacesnrt. For more information, please follow other related articles on the PHP Chinese website!