python strip()函数的用法

函数原型

声明:s为字符串,rm为要删除的字符序列

s.strip(rm)         删除s字符串中开头、结尾处,位于 rm删除序列的字符

s.lstrip(rm)        删除s字符串中开头处,位于 rm删除序列的字符

s.rstrip(rm)        删除s字符串中结尾处,位于 rm删除序列的字符

注意:

1. 当rm为空时,默认删除空白符(包括' ', ' ',  ' ',  ' ')

例如:

>>> a = '     123'
>>> a.strip()
'123'
>>> a='		abc'
'abc'
>>> a = 'sdff
'
>>> a.strip()
'sdff'

2.当rm不为空:按照字符串从前往后或从后往前的顺序删除rm序列,如果不遵从前后或后前序列则不删除。

例如 :

>>> a='12345'
>>> a.strip('13')
'2345'    #3在中间,所以不删除
>>> a.strip('21')
'345'
>>> a.strip('54')
'123'

>>> a.strip('123')

'45'
>>> a.strip('321')
'45'

 

原文地址:https://www.cnblogs.com/xisheng/p/7339956.html