Python之strip()

源於: 功能類代碼 – DatasettoFileClass.py – 函數make_directionary


strip()功能:
  python中的strip用於去除字符串的 首尾 字符 ,同理,lstrip用於去除左邊的字符,rstrip用於去除右邊的字符

注意: 傳入的是一個字符數組,編譯器去除兩端所有相應的字符,直到沒有匹配的字符


栗子1:

String = 'saaaay yes no yaaaass'
print(String.strip('say'))

依次去除首尾包含字符 ‘s’,‘a’,‘y’ 數組內的字符,其結果爲:yes no

栗子2: 傳入單個字符

String = 'aaa111aaa222aaa'
print(String.strip('a'))

結果爲:111aaa222

栗子3: 傳入多個字符

c ='abc111222abc333acbbca'
print(c.strip('abc'))
print(c.strip('acb'))

結果爲:

111222abc333
111222abc333

同理,lstrip用於去除左邊的字符,rstrip用於去除右邊的字符。

String = 'saaaay yes no yaaaass'
print(String.strip('say'))
print(String.strip('say ')) #say後面有空格
print(String.lstrip('say')) 
print(String.rstrip('say'))

結果爲:
在這裏插入圖片描述


學習鏈接:
python中strip()函數的使用
Python strip 函數小結

發佈了250 篇原創文章 · 獲贊 27 · 訪問量 2萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章