strip() function removes spaces\n\r\tUsage of function
strip removes spaces on both sides at the same time(recommended learning: Python video tutorial)
lstrip Remove the spaces on the left
rstrip Remove the spaces on the right
Specific examples are as follows:
>>>a=" hello world!! " >>>a.lstrip() 'hello world!! ' >>>a.rstrip() ' hello world!!' >>>a.strip() 'hello world!!'
Statement: s is a string, rm is the character sequence to be deleted
s.strip(rm) deletes the s string The characters in the rm deletion sequence at the beginning and end of the s string
s.lstrip(rm) Delete the characters in the rm deletion sequence at the beginning and end of the s string
s.rstrip(rm) Delete the characters in the rm deletion sequence at the end of the s string
Note:
When rm is empty, whitespace characters (including '\n') are deleted by default , '\r', '\t', ' ')
>>> a = ' hello world!!' >>> a.strip() 'hello world!!' >>> a='\t\thello world!!' 'hello world!!' >>> a = 'hello world!!\r\n' >>> a.strip() 'hello world!!'
For more Python-related technical articles, please visit the Python Tutorial column to learn!
The above is the detailed content of How to remove the last space in Python. For more information, please follow other related articles on the PHP Chinese website!