Home  >  Article  >  Backend Development  >  What is the usage of strip() function in python?

What is the usage of strip() function in python?

王林
王林Original
2020-05-07 13:55:238428browse

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:

What is the usage of strip() function in python?

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn