日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python strip() 函数探究

發布時間:2025/3/20 python 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python strip() 函数探究 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

strip()方法語法:str.strip([chars]);

聲明:str為字符串,rm為要刪除的字符序列

  • str.strip(rm) 刪除字符串中開頭、結尾處,位于rm刪除序列的字符
eg1: #首尾端'0'被刪除,中間不動 >>> t='0000this is string example0000wow!!!0000' >>> t.strip('0') 'this is string example0000wow!!!' eg2: #無入參,默認刪除首尾所有空字符 ‘\n\t空格‘ >>>s='\n 0000this is string example0000wow!!!0000\n \t' >>> s.strip() '0000this is string example0000wow!!!0000'
  • str.lstrip(rm) 刪除字符串中開頭處,位于 rm刪除序列的字符
''' 遇到問題沒人解答?小編創建了一個Python學習交流QQ群:579817333 尋找有志同道合的小伙伴,互幫互助,群里還有不錯的視頻學習教程和PDF電子書! ''' t='0000this is string example0000wow!!!0000' >>> t.lstrip('0') 'this is string example0000wow!!!0000' #空入參同樣可刪除首部空字符,'.rstrip()'同理 s='\n 0000this is string example0000wow!!!0000\n \t' >>> s.lstrip() '0000this is string example0000wow!!!0000\n \t'
  • str.rstrip(rm) 刪除字符串中結尾處,位于 rm刪除序列的字符
t='0000this is string example0000wow!!!0000' >>> t.rstrip('0') '0000this is string example0000wow!!!'s='\n 0000this is string example0000wow!!!0000\n \t' >>> s.rstrip() '\n 0000this is string example0000wow!!!0000'

究竟何為’首尾’?實驗之

''' 遇到問題沒人解答?小編創建了一個Python學習交流QQ群:579817333 尋找有志同道合的小伙伴,互幫互助,群里還有不錯的視頻學習教程和PDF電子書! ''' s='\n 0000this is string is example0000wow!!!0000\n \t' >>> s.lstrip('\n 0') 'this is string is example0000wow!!!0000\n \t' #首部'\n 0000'被刪除>>> s.lstrip('\n 0this') 'ring is example0000wow!!!0000\n \t'

奇妙啊,我的目標是刪除首部’\n 0000this’,結果’\n 0000this is st’全被刪除,說明:符合入參(’\n 0this’)的字符皆是刪除對象,不論字符順序

但,為何string后面的is沒有刪除?因為,'首部’指的是’連續符合’入參要求的字符,string中的’r’隔斷了入參的連續字符要求,python判定首部結束。

實驗證明:

所謂的首、尾,判定依據是-是否連續符合入參要求,如果符合,不論順序,皆可操作,一直到遇到第一個非入參字符為止.

總結

以上是生活随笔為你收集整理的python strip() 函数探究的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。