Python去除字符串兩端空格的方法
目的
獲得一個首尾不含多余空格的字符串
方法
可以使用字符串的以下方法處理:
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用來刪除特定的符號,注意空格并沒有被移除,例如:
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ù)器上傳文件示例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-12-12
詳解Pandas中stack()和unstack()的使用技巧
當(dāng)你在處理包含某種序列(例如時間序列數(shù)據(jù))的變量的數(shù)據(jù)集時,數(shù)據(jù)通常需要進行重塑。Pandas?提供了各種用于重塑?DataFrame?的內(nèi)置方法。其中,stack()?和?unstack()?是最流行的,本文總結(jié)了這兩個方法的7種使用技巧,需要的可以參考一下2022-03-03
Pytorch DataLoader shuffle驗證方式
這篇文章主要介紹了Pytorch DataLoader shuffle驗證方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-06-06
python基于exchange函數(shù)發(fā)送郵件過程詳解
這篇文章主要介紹了python基于exchange函數(shù)發(fā)送郵件過程詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-11-11
python中numpy.zeros(np.zeros)的使用方法
下面小編就為大家?guī)硪黄猵ython中numpy.zeros(np.zeros)的使用方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-11-11
python?爬取豆瓣電影短評并利用wordcloud生成詞云圖
這篇文章主要介紹了python?爬取豆瓣電影短評并利用wordcloud生成詞云圖,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價值,需要的小伙伴可以參考一下2022-06-06
python 根據(jù)列表批量下載網(wǎng)易云音樂的免費音樂
這篇文章主要介紹了python 根據(jù)列表下載網(wǎng)易云音樂的免費音樂,幫助大家更好的理解和學(xué)習(xí)python,感興趣的朋友可以了解下2020-12-12

