Home  >  Article  >  Backend Development  >  Detailed explanation of usage scenarios of strip function in python

Detailed explanation of usage scenarios of strip function in python

Y2J
Y2JOriginal
2017-05-18 14:28:282237browse

Python strip() function introduction, friends who need it can refer to it

Function prototype

Declaration: s is a string, rm is the character sequence to be deleted

s.strip(rm) Delete 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:

1. When rm is empty, whitespace characters are deleted by default (Including '\n', '\r', '\t', ' ')

For example:

The code is as follows:

>>> a = '  123'
>>> a.strip()
'123'
>>> a='\t\tabc'
'abc'
>>> a = 'sdff\r\n'
>>> a.strip()
'sdff'

2. Delete rm here The sequence is deleted as long as the characters on the edge (beginning or end) are within the deletion sequence.

For example:

The code is as follows:

>>> a = '123abc'
>>> a.strip('21')
'3abc'   结果是一样的
>>> a.strip('12')
'3abc'

[Related recommendations]

1. Python free video tutorial

2. The little-known traps of strip() in python

3. The wonderful uses of the strip() function in Python that you don’t know

4. The little-known traps of strip() in python

5. Basic introduction to python teaches you how to use the strip() function to remove spaces\n\r\ t

The above is the detailed content of Detailed explanation of usage scenarios 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