首頁  >  文章  >  後端開發  >  Python怎麼去掉最後的空格

Python怎麼去掉最後的空格

(*-*)浩
(*-*)浩原創
2019-07-03 11:02:2611566瀏覽

strip()函數去空格\n\r\t函數的用法

Python怎麼去掉最後的空格

#strip 同時去掉左右兩邊的空格(建議學習: Python影片教學

lstrip 去掉左邊的空格

rstrip 去掉右邊的空格

#具體範例如下:

>>>a=" hello world!!  "
>>>a.lstrip() 'hello world!! '
>>>a.rstrip() ' hello world!!'
>>>a.strip() 'hello world!!'

宣告:s為字串,rm為要刪除的字元序列

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

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

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

注意:

當rm為空時,預設刪除空白符(包括'\n' , '\r', '\t', ' ')

>>> a = ' hello world!!'
>>> a.strip()
'hello world!!'
>>> a='\t\thello world!!'
'hello world!!'
>>> a = 'hello world!!\r\n'
>>> a.strip()
'hello world!!'

更多Python相關技術文章,請造訪Python教學欄位學習!

以上是Python怎麼去掉最後的空格的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn