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

歡迎訪問 生活随笔!

生活随笔

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

python

python strip() 函数探究

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

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: #無入?yún)?#xff0c;默認刪除首尾所有空字符 ‘\n\t空格‘ >>>s='\n 0000this is string example0000wow!!!0000\n \t' >>> s.strip() '0000this is string example0000wow!!!0000'
  • str.lstrip(rm) 刪除字符串中開頭處,位于 rm刪除序列的字符
t='0000this is string example0000wow!!!0000' >>> t.lstrip('0') 'this is string example0000wow!!!0000' #空入?yún)⑼瑯涌蓜h除首部空字符,'.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'

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

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'全被刪除,說明:符合入?yún)?'\n 0this')的字符皆是刪除對象,不論字符順序 #但,為何string后面的is沒有刪除?因為,'首部'指的是'連續(xù)符合'入?yún)⒁蟮淖址?#xff0c;string中的'r'隔斷了入?yún)⒌倪B續(xù)字符要求,python判定首部結束。

實驗證明:所謂的首、尾,判定依據(jù)是-是否連續(xù)符合入?yún)⒁?如果符合,不論順序,皆可操作,一直到遇到第一個非入?yún)⒆址麨橹?

轉載于:https://www.cnblogs.com/deepblue775737449/p/8279618.html

總結

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

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。