1. 程式人生 > >python 之 strip()--(轉載)

python 之 strip()--(轉載)

rip class 函數 地址 字符 pre abc blog python

原博地址:http://www.jb51.net/article/37287.htm

函數原型

聲明:s為字符串,rm為要刪除的字符序列

s.strip(rm) 刪除s字符串中開頭、結尾處,位於 rm刪除序列的字符

s.lstrip(rm) 刪除s字符串中開頭處,位於 rm刪除序列的字符

s.rstrip(rm) 刪除s字符串中結尾處,位於 rm刪除序列的字符

1. 當rm為空時,默認刪除空白符(包括‘\n‘, ‘\r‘, ‘\t‘, ‘ ‘)

例如:

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

2.這裏的rm刪除序列是只要邊(開頭或結尾)上的字符在刪除序列內,就刪除掉。

例如:

>>> a = ‘123abc‘
>>> a.strip(‘21‘)
‘3abc‘   結果是一樣的
>>> a.strip(‘12‘)
‘3abc‘

  

python 之 strip()--(轉載)