python strip()函數 介紹

函數原型python

聲明:s爲字符串,rm爲要刪除的字符序列函數

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

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

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

注意:ip

1. 當rm爲空時,默認刪除空白符(包括'\n', '\r',  '\t',  ' ')字符串

例如:原型

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


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

例如 :io

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

複製代碼

相關文章
相關標籤/搜索