Home  >  Article  >  Backend Development  >  What does strip mean in python?

What does strip mean in python?

爱喝马黛茶的安东尼
爱喝马黛茶的安东尼Original
2019-06-26 13:19:2936881browse

The strip() method in Python is used to remove specified characters (default is space or newline character) or character sequence at the beginning and end of a string.

Note: This method can only delete the beginning or end characters, but not the middle characters.

Its function prototype: string.strip(s[, chars]), which returns a copy of the string with leading and suffix characters removed.

(This means which characters in the string you want to remove, then you pass these characters in as parameters. This function will only delete the first and last characters, not the middle ones.)

If the parameter of strip() is empty, the blank characters at the beginning and end of the string (including \n, \r, \t) will be deleted by default.
lstrip(): Remove the left side
rstrip(): Remove the right side

What does strip mean in python?

Example 1:

>>> str = ' ab cd '
>>> str
' ab cd '
>>> str.strip() #删除头尾空格
'ab cd'
>>> str.lstrip() #删除开头空格
'ab cd '
>>> str.rstrip() #删除结尾空格
' ab cd'

Related recommendations: " Python Video Tutorial

Example 2:

>>> str2 = '1a2b12c21'
>>> str2.strip('12') #删除头尾的1和2
'a2b12c'
>>> str2.lstrip('12') #删除开头的1和2
'a2b12c21'
>>> str2.rstrip('12') #删除结尾的1和2
'1a2b12c'

Example 3:

a="aabcacb1111acbba"
print(a.strip("abc"))
print(a.strip("acb"))
print(a.strip("bac"))
print(a.strip("bca"))
print(a.strip("cab"))
print(a.strip("cba"))

Output:

1111
1111
1111
1111
1111
1111

The above is the detailed content of What does strip mean 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