Python去除字符串兩端空格的方法
目的
獲得一個(gè)首尾不含多余空格的字符串
方法
可以使用字符串的以下方法處理:
string.lstrip(s[, chars])
Return a copy of the string with leading characters removed. If chars is omitted or None, whitespace characters are removed. If given and not None, chars must be a string; the characters in the string will be stripped from the beginning of the string this method is called on.
string.rstrip(s[, chars])
Return a copy of the string with trailing characters removed. If chars is omitted or None, whitespace characters are removed. If given and not None, chars must be a string; the characters in the string will be stripped from the end of the string this method is called on.
string.strip(s[, chars])
Return a copy of the string with leading and trailing characters removed. If chars is omitted or None, whitespace characters are removed. If given and not None, chars must be a string; the characters in the string will be stripped from the both ends of the string this method is called on.
具體的效果如下:
In [10]: x=' Hi,Jack! '
In [11]: print '|',x.lstrip(),'|',x.rstrip(),'|',x.strip(),'|'
| Hi,Jack! | Hi,Jack! | Hi,Jack! |
其中提供的參數(shù)chars用來(lái)刪除特定的符號(hào),注意空格并沒(méi)有被移除,例如:
In [12]: x='yxyxyxxxyy Hello xyxyxyy'
In [13]: print x.strip('xy')
Hello
相關(guān)文章
Python調(diào)用scp向服務(wù)器上傳文件示例
今天小編就為大家分享一篇Python調(diào)用scp向服務(wù)器上傳文件示例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-12-12
詳解Pandas中stack()和unstack()的使用技巧
當(dāng)你在處理包含某種序列(例如時(shí)間序列數(shù)據(jù))的變量的數(shù)據(jù)集時(shí),數(shù)據(jù)通常需要進(jìn)行重塑。Pandas?提供了各種用于重塑?DataFrame?的內(nèi)置方法。其中,stack()?和?unstack()?是最流行的,本文總結(jié)了這兩個(gè)方法的7種使用技巧,需要的可以參考一下2022-03-03
Python如何檢測(cè)項(xiàng)目哪些依賴庫(kù)沒(méi)有使用
這篇文章主要為大家詳細(xì)介紹了五個(gè)Python檢測(cè)項(xiàng)目中哪些依賴庫(kù)沒(méi)有使用的方法,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2025-04-04
Pytorch DataLoader shuffle驗(yàn)證方式
這篇文章主要介紹了Pytorch DataLoader shuffle驗(yàn)證方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-06-06
python基于exchange函數(shù)發(fā)送郵件過(guò)程詳解
這篇文章主要介紹了python基于exchange函數(shù)發(fā)送郵件過(guò)程詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-11-11
python中numpy.zeros(np.zeros)的使用方法
下面小編就為大家?guī)?lái)一篇python中numpy.zeros(np.zeros)的使用方法。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-11-11
python?爬取豆瓣電影短評(píng)并利用wordcloud生成詞云圖
這篇文章主要介紹了python?爬取豆瓣電影短評(píng)并利用wordcloud生成詞云圖,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下2022-06-06
python 根據(jù)列表批量下載網(wǎng)易云音樂(lè)的免費(fèi)音樂(lè)
這篇文章主要介紹了python 根據(jù)列表下載網(wǎng)易云音樂(lè)的免費(fèi)音樂(lè),幫助大家更好的理解和學(xué)習(xí)python,感興趣的朋友可以了解下2020-12-12

