Python strip 函式踩坑

2021-09-10 12:05:36 字數 556 閱讀 3144

s.strip(chars=none)

strip 函式用於去除字串首尾的空格,當 chars 不為 none 時,則刪除字串首尾的 chars 中的字元。

當 chars=none 時,去除首尾空格,沒啥好說的,我們來看 chars 不為 none 時的情況。

str = 'abc123abc'

print(str.strip('a')) # bc123abc

print(str.strip('abc')) # 123

結果跟預期的一樣,我們再看下面的例子:

print(str.strip('cba'))      # 123

print(str.strip('decbafg')) # 123

這結果讓我們大跌眼鏡,明明是「abc」,為什麼用「cba」也能刪除呢?下面乙個更誇張,「defg」這4個字母原字母中根本沒有。

結果表明,只要首尾兩端的字元在 chars 之內,就會被刪除,直到遇到第乙個不在 chars 內的字元。

python strip方法的坑

str1 if str1 is none的寫法是錯的,正確的寫法是 if str1 is str1 str1 is none false 吐了有木有啊 空字元居然不能用none來表示,而列表就能用none來表示,這是一點突破了我的認知的地方。mark錯誤的方法 str str1 is none fa...

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字串中結尾處,位...