A basic introduction to Python teaches you how to use the strip() function to remove spaces\n\r\t

Y2J
Release: 2017-05-18 14:24:49
Original
4992 people have browsed it

python3.4 study notes (20) python strip() function to remove spaces\n\r\tUsage of function

In Python, there are three space removal functions in the string processing function (including '\ n', '\r', '\t', ' ') function:

strip removes the spaces on the left and right at the same time
lstrip removes the spaces on the left
rstrip removes the spaces on the right

Specific examples are as follows:

>>>a=" gho stwwl "
>>>a.lstrip() 'gho stwwl '
>>>a.rstrip() ' gho stwwl'
>>>a.strip() 'gho stwwl'
Copy after login

Statement: s is a string, rm is the character sequence to delete
s.strip(rm) deletes the s character The characters at the beginning and end of the string, located in the rm deletion sequence
s.lstrip(rm) Delete the characters at the beginning of the s string, located in the rm deletion sequence
s.rstrip(rm) Delete the s string At the end, the characters located in the rm deletion sequence

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

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

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

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

【Related recommendations】

1. Python free video tutorial

2. Detailed explanation of strip() and split() in python How to use

3. Detailed explanation of the usage scenarios of strip function in python

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

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

The above is the detailed content of A basic introduction to Python teaches you how to use the strip() function to remove spaces\n\r\t. 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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!