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用來刪除特定的符號(hào),注意空格并沒有被移除,例如:
In [12]: x='yxyxyxxxyy Hello xyxyxyy'
In [13]: print x.strip('xy')
Hello
相關(guān)文章
numpy創(chuàng)建神經(jīng)網(wǎng)絡(luò)框架
本文介紹了使用numpy從零搭建了一個(gè)類似于pytorch的深度學(xué)習(xí)框架,可以用在很多地方,有需要的朋友可以自行參考一下2021-08-08pycharm 使用tab跳出正在編輯的括號(hào)(){}{}等問題
這篇文章主要介紹了pycharm 使用tab跳出正在編輯的括號(hào)(){}{}等問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2021-02-02Flask框架學(xué)習(xí)筆記之消息提示與異常處理操作詳解
這篇文章主要介紹了Flask框架學(xué)習(xí)筆記之消息提示與異常處理操作,結(jié)合實(shí)例形式分析了flask框架表單登陸消息提示、錯(cuò)誤模板調(diào)用及異常處理相關(guān)操作技巧,需要的朋友可以參考下2019-08-08pyqt5 鍵盤監(jiān)聽按下enter 就登陸的實(shí)例
今天小編就為大家分享一篇pyqt5 鍵盤監(jiān)聽按下enter 就登陸的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-06-06Python最大連續(xù)區(qū)間和動(dòng)態(tài)規(guī)劃
這篇文章主要介紹了Python最大連續(xù)區(qū)間和動(dòng)態(tài)規(guī)劃,文章圍繞Python最大連續(xù)區(qū)間和動(dòng)態(tài)規(guī)劃的相關(guān)資料展開內(nèi)容,需要的小伙伴可以參考一下2022-01-01Python實(shí)現(xiàn)捕獲異常發(fā)生的文件和具體行數(shù)
這篇文章主要介紹了Python實(shí)現(xiàn)捕獲異常發(fā)生的文件和具體行數(shù)。具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-04-04python之wxPython應(yīng)用實(shí)例
這篇文章主要介紹了python之wxPython應(yīng)用實(shí)例,以加載圖片為例講述了wxPython的用法,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2014-09-09django將網(wǎng)絡(luò)中的圖片,保存成model中的ImageField的實(shí)例
今天小編就為大家分享一篇django將網(wǎng)絡(luò)中的圖片,保存成model中的ImageField的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-08-08Django多數(shù)據(jù)庫(kù)的實(shí)現(xiàn)過程詳解
這篇文章主要介紹了Django多數(shù)據(jù)庫(kù)的實(shí)現(xiàn)過程詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-08-08