最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Python去除字符串兩端空格的方法

 更新時(shí)間:2015年05月21日 09:53:29   投稿:junjie  
這篇文章主要介紹了Python去除字符串兩端空格的方法,本文主要講解了string.lstrip、string.rstrip、string.strip等函數(shù)的運(yùn)用,需要的朋友可以參考下

目的

  獲得一個(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.

 

具體的效果如下:

復(fù)制代碼 代碼如下:

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)有被移除,例如:

復(fù)制代碼 代碼如下:

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ù)器上傳文件示例

    今天小編就為大家分享一篇Python調(diào)用scp向服務(wù)器上傳文件示例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2019-12-12
  • 詳解Pandas中stack()和unstack()的使用技巧

    詳解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)有使用

    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)證方式

    這篇文章主要介紹了Pytorch DataLoader shuffle驗(yàn)證方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-06-06
  • Python 在局部變量域中執(zhí)行代碼

    Python 在局部變量域中執(zhí)行代碼

    這篇文章主要介紹了Python 如何在局部變量域中執(zhí)行代碼,幫助大家更好的理解和學(xué)習(xí)Python,感興趣的朋友可以了解下
    2020-08-08
  • python基于exchange函數(shù)發(fā)送郵件過(guò)程詳解

    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)的使用方法

    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生成詞云圖

    這篇文章主要介紹了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è)

    這篇文章主要介紹了python 根據(jù)列表下載網(wǎng)易云音樂(lè)的免費(fèi)音樂(lè),幫助大家更好的理解和學(xué)習(xí)python,感興趣的朋友可以了解下
    2020-12-12
  • Apache,wsgi,django 程序部署配置方法詳解

    Apache,wsgi,django 程序部署配置方法詳解

    這篇文章主要介紹了Apache,wsgi,django 程序部署配置方法,結(jié)合實(shí)例形式詳細(xì)分析了Linux環(huán)境下Apache,wsgi,django程序部署配置的相關(guān)操作技巧與注意事項(xiàng),需要的朋友可以參考下
    2019-07-07

最新評(píng)論

沈丘县| 庆安县| 五家渠市| 武冈市| 明光市| 福清市| 衡阳市| 卢氏县| 安西县| 巴塘县| 施甸县| 舒城县| 福清市| 喀喇| 仙桃市| 金山区| 谢通门县| 资源县| 石景山区| 瓮安县| 久治县| 玉环县| 韶关市| 剑川县| 阳城县| 新乐市| 平安县| 浮山县| 塔城市| 扎囊县| 镇坪县| 黑水县| 辉县市| 雷州市| 常山县| 横山县| 弥渡县| 汶上县| 灵丘县| 天等县| 松原市|