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

王林
Release: 2020-05-07 13:55:23
Original
8440 people have browsed it

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])
Copy after login

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'))
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template