Python 刪除字串的空白

2022-01-30 17:01:48 字數 815 閱讀 1354

在程式中,額外的空白可能讓人迷惑,對於程式設計師來說,'python'跟'python '看起來幾乎一樣,但是對於程式來說,可是千差萬別

>>> language = '

python

'>>>language

'python

'>>>language.lstrip()

'python

'

根據列印結果可以看出,已經刪除了開頭空白

>>> language = '

python

'>>>language

'python

'>>>language.rstrip()

'python

'

結尾的空白已經成功刪除

>>> language = '

python

'>>>language

' 

python

'

>>> language.strip()
'

python

'

注意:字串刪除空白後必須重新賦值變數,否則是失效的。

>>> language = '

python

'>>>language.strip()

'python

'>>>language

'python

'>>> language =language.strip()

>>>language

'python

'

Python學習筆記 字串方法 刪除空白

python 和 python 是兩個不同的字串,空白很重要,因為經常需要比較兩個字串是否相同,比如,在使用者登入 時檢查其使用者名稱。這些剝除函式常用於在儲存使用者輸入前對其進行清理。確保字串開頭沒有空白 xx.lstrip 確保字串末尾沒有空白 xx.rstrip 確保字串兩側沒有空白 xx.s...

Python 字串之刪除空白字元或某字元或字串

strip rstrip lstrip 分別用來刪除兩端 右端 左端 連續的空白字元或字符集 s abc s2 s.strip 刪除空白字元 print s2 abc s3 n nhello world n n strip 刪除空白字元 print s3 hello world s4 aaaassd...

Python 刪除字串

1 strip lstrip rstrip 方法,在沒有引數時,分別能刪除字串開頭或結尾 左邊的 右邊的空格,傳入引數時,分別能刪除字串開頭或結尾 左邊的 右邊的相關字串。whitespace stripping s hello world n s.strip hello world s.lstri...