Python strip lstrip rstrip使用方法
更新時間:2008年09月06日 13:26:38 作者:
Python中的strip用于去除字符串的首位字符,同理,lstrip用于去除左邊的字符,rstrip用于去除右邊的字符。這三個函數都可傳入一個參數,指定要去除的首尾字符。
注意的是,傳入的是一個字符數組,編譯器去除兩端所有相應的字符,直到沒有匹配的字符,比如:
theString = 'saaaay yes no yaaaass'
print theString.strip('say')
theString依次被去除首尾在['s','a','y']數組內的字符,直到字符在不數組內。所以,輸出的結果為:
yes no
比較簡單吧,lstrip和rstrip原理是一樣的。注意:當沒有傳入參數時,是默認去除首尾空格的。
theString = 'saaaay yes no yaaaass'
print theString.strip('say')
print theString.strip('say ') #say后面有空格
print theString.lstrip('say')
print theString.rstrip('say')
運行結果:
yes no
es no
yes no yaaaass
saaaay yes no
復制代碼 代碼如下:
theString = 'saaaay yes no yaaaass'
print theString.strip('say')
theString依次被去除首尾在['s','a','y']數組內的字符,直到字符在不數組內。所以,輸出的結果為:
yes no
比較簡單吧,lstrip和rstrip原理是一樣的。注意:當沒有傳入參數時,是默認去除首尾空格的。
復制代碼 代碼如下:
theString = 'saaaay yes no yaaaass'
print theString.strip('say')
print theString.strip('say ') #say后面有空格
print theString.lstrip('say')
print theString.rstrip('say')
運行結果:
yes no
es no
yes no yaaaass
saaaay yes no
相關文章
詳解利用django中間件django.middleware.csrf.CsrfViewMiddleware防止csrf
這篇文章主要介紹了詳解利用django中間件django.middleware.csrf.CsrfViewMiddleware防止csrf攻擊,小編覺得挺不錯的,現在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-10-10Python3 Tkinkter + SQLite實現登錄和注冊界面
這篇文章主要為大家詳細介紹了Python3 Tkinkter + SQLite實現登錄和注冊界面,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-11-11pandas pd.cut()與pd.qcut()的具體實現
本文主要介紹了pandas pd.cut()與pd.qcut()的具體實現,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2023-01-01