python strip 函式 介紹

2022-08-13 12:15:09 字數 618 閱讀 2736

python strip()函式 介紹,需要的朋友可以參考一下

函式原型

宣告: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 函式 介紹

函式原型 宣告 s為字串,rm為要刪除的字串行 s.strip rm 刪除s字串中 開頭 結尾 處,位於 rm刪除序列的字元 s.lstrip rm 刪除s字串中 開頭處,位於 rm刪除序列的字元 s.rstrip rm 刪除s字串中 結尾處,位於 rm刪除序列的字元 注意 1.當rm為空時,預設刪...

python strip 函式 介紹

python strip 函式 介紹,需要的朋友可以參考一下 函式原型 宣告 s為字串,rm為要刪除的字串行 s.strip rm 刪除s字串中開頭 結尾處,位於 rm刪除序列的字元 s.lstrip rm 刪除s字串中開頭處,位於 rm刪除序列的字元 s.rstrip rm 刪除s字串中結尾處,位...

python strip 函式 介紹

函式原型 宣告 s為字串,rm為要刪除的字串行 s.strip rm 刪除s字串中開頭 結尾處,位於 rm刪除序列的字元 s.lstrip rm 刪除s字串中開頭處,位於 rm刪除序列的字元 s.rstrip rm 刪除s字串中結尾處,位於 rm刪除序列的字元 注意 1.當rm為空時,預設刪除空白符...