Home > Article > Backend Development > What is the usage of strip() function in python?
The strip() method in Python is used to remove specified characters at the beginning and end of a string (default is a space or newline character) or character sequence.
Note: This method can only delete the beginning or end characters, but not the middle characters.
str.strip([chars])
chars is the character sequence that needs to be removed.
The return value is the new sequence generated after removing the specified character sequence.
str=" 000120abc021000 \n" #用于移除字符串头尾指定的字符(默认为空格或换行符)或字符序列。 print(str.strip() print() # str1="000120abc021000" #删除字符串开头和结尾指定为0的字符 print(str1.strip('0')) print() #删除字符串开头指定为0的字符 print(str1.lstrip('0')) print() #删除字符串结尾指定为0的字符 print(str1.rstrip('0'))
The running results are as follows:
Recommended tutorial: python tutorial
The above is the detailed content of What is the usage of strip() function in python?. For more information, please follow other related articles on the PHP Chinese website!