Python strip lstrip rstrip使用方法
更新時間:2008年09月06日 13:26:38 作者:
Python中的strip用于去除字符串的首位字符,同理,lstrip用于去除左邊的字符,rstrip用于去除右邊的字符。這三個函數(shù)都可傳入一個參數(shù),指定要去除的首尾字符。
注意的是,傳入的是一個字符數(shù)組,編譯器去除兩端所有相應(yīng)的字符,直到?jīng)]有匹配的字符,比如:
theString = 'saaaay yes no yaaaass'
print theString.strip('say')
theString依次被去除首尾在['s','a','y']數(shù)組內(nèi)的字符,直到字符在不數(shù)組內(nèi)。所以,輸出的結(jié)果為:
yes no
比較簡單吧,lstrip和rstrip原理是一樣的。注意:當(dāng)沒有傳入?yún)?shù)時,是默認去除首尾空格的。
theString = 'saaaay yes no yaaaass'
print theString.strip('say')
print theString.strip('say ') #say后面有空格
print theString.lstrip('say')
print theString.rstrip('say')
運行結(jié)果:
yes no
es no
yes no yaaaass
saaaay yes no
復(fù)制代碼 代碼如下:
theString = 'saaaay yes no yaaaass'
print theString.strip('say')
theString依次被去除首尾在['s','a','y']數(shù)組內(nèi)的字符,直到字符在不數(shù)組內(nèi)。所以,輸出的結(jié)果為:
yes no
比較簡單吧,lstrip和rstrip原理是一樣的。注意:當(dāng)沒有傳入?yún)?shù)時,是默認去除首尾空格的。
復(fù)制代碼 代碼如下:
theString = 'saaaay yes no yaaaass'
print theString.strip('say')
print theString.strip('say ') #say后面有空格
print theString.lstrip('say')
print theString.rstrip('say')
運行結(jié)果:
yes no
es no
yes no yaaaass
saaaay yes no
相關(guān)文章
keras自動編碼器實現(xiàn)系列之卷積自動編碼器操作
這篇文章主要介紹了keras自動編碼器實現(xiàn)系列之卷積自動編碼器操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-07-07Python數(shù)據(jù)結(jié)構(gòu)與算法之字典樹實現(xiàn)方法示例
這篇文章主要介紹了Python數(shù)據(jù)結(jié)構(gòu)與算法之字典樹實現(xiàn)方法,可實現(xiàn)針對單詞出現(xiàn)次數(shù)的統(tǒng)計功能,涉及Python樹結(jié)構(gòu)的定義、遍歷及統(tǒng)計等相關(guān)操作技巧,需要的朋友可以參考下2017-12-12詳解利用django中間件django.middleware.csrf.CsrfViewMiddleware防止csrf
這篇文章主要介紹了詳解利用django中間件django.middleware.csrf.CsrfViewMiddleware防止csrf攻擊,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-10-10Python3 Tkinkter + SQLite實現(xiàn)登錄和注冊界面
這篇文章主要為大家詳細介紹了Python3 Tkinkter + SQLite實現(xiàn)登錄和注冊界面,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-11-11pandas pd.cut()與pd.qcut()的具體實現(xiàn)
本文主要介紹了pandas pd.cut()與pd.qcut()的具體實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-01-01