1. 程式人生 > >python學習:刪除空白

python學習:刪除空白

lang pytho 方法 span rst rip bsp 必須 python學習

刪除空白

刪除尾部空白

確保字符串尾部沒有空白,使用rstrip();

刪除字符串開頭的空白,使用lstrip();

同時刪除字符串兩端的空白,使用strip()

代碼:

>>>favorite_language = ‘python ‘

>>>favorite_language

‘python ‘

>>>favorite_language.rstrip()

‘python‘

>>>favorite_language

‘python ‘

註意:對變量favorite_language調用方法rstrip()後,多余的空格被刪除了。但是這種刪除是暫時的,接下來再次詢問favorite_language的值時,依然包含多余的空白。要永久刪除空白,必須將刪除操作的結果存回到變量中。

代碼:

>>>favorite_language = ‘python ‘

>>>favorite_language = favorite_language.rstrip()

>>>favorite_language

‘python‘

python學習:刪除空白