在對字符串操作時(shí),為了清除左右兩邊的空格,常用strip()函數(shù)對字符串操作
s.strip(rm) 刪除s字符串中開頭、結(jié)尾處,位于 rm刪除序列的字符
s.lstrip(rm) 刪除s字符串中開頭處,位于 rm刪除序列的字符
s.rstrip(rm) 刪除s字符串中結(jié)尾處,位于 rm刪除序列的字符
注意:
- 當(dāng)rm為空時(shí),默認(rèn)刪除空白符(包括'\n', '\r', '\t', ' ')
例如:
>>> a =' 123'
>>> a.strip()
'123'
>>> a='\t\nabc'
>>> a.strip()
'abc'
>>> print(a)
abc
>>> a.lstrip()
'abc'
>>> a='\t\n\tabc\t\n'
>>> a.rstrip()
'\t\n\tabc'
>>>