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

歡迎訪問 生活随笔!

生活随笔

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

python

python删除、替换字符串某字符后的字符串(删除字符串、替换字符串、strip、split、rstrip、lstrip、replace)

發布時間:2025/3/15 python 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python删除、替换字符串某字符后的字符串(删除字符串、替换字符串、strip、split、rstrip、lstrip、replace) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

刪除字符串某字符后的字符串

url = "phpmyadmin.css.php?3Fserver=1&lang=en&token=39e3d96974667d6163351cf22870a231&js_frame=right&nocache=3086758583" url = url.split("?", 1)[0] print (url)

?

strip

參數

  • chars -- 移除字符串頭尾指定的字符序列。

返回值

返回移除字符串頭尾指定的字符序列生成的新字符串。

#!/usr/bin/python3str = "*****this is **string** example....wow!!!*****" print (str.strip( '*' )) # 指定字符串 *結果: this is **string** example....wow!!!

?

split

參數

  • str -- 分隔符,默認為所有的空字符,包括空格、換行(\n)、制表符(\t)等。
  • num -- 分割次數。默認為 -1, 即分隔所有。

返回值

返回分割后的字符串列表。

#!/usr/bin/python3str = "this is string example....wow!!!" print (str.split( )) # 以空格為分隔符 print (str.split('i',1)) # 以 i 為分隔符 print (str.split('w')) # 以 w 為分隔符結果: ['this', 'is', 'string', 'example....wow!!!'] ['th', 's is string example....wow!!!'] ['this is string example....', 'o', '!!!']

?

rstrip

參數

  • chars -- 指定刪除的字符(默認為空格)

返回值

返回刪除 string 字符串末尾的指定字符后生成的新字符串。

#!/usr/bin/python3str = " this is string example....wow!!! " print (str.rstrip()) str = "*****this is string example....wow!!!*****" print (str.rstrip('*'))結果:this is string example....wow!!! *****this is string example....wow!!!

?

lstrip

參數

  • chars --指定截取的字符。

返回值

返回截掉字符串左邊的空格或指定字符后生成的新字符串。

#!/usr/bin/python3str = " this is string example....wow!!! "; print( str.lstrip() ); str = "88888888this is string example....wow!!!8888888"; print( str.lstrip('8') );結果: this is string example....wow!!! this is string example....wow!!!8888888

?

replace

參數

  • old -- 將被替換的子字符串。
  • new -- 新字符串,用于替換old子字符串。
  • max -- 可選字符串, 替換不超過 max 次

返回值

返回字符串中的 old(舊字符串) 替換成 new(新字符串)后生成的新字符串,如果指定第三個參數max,則替換不超過 max 次。

實例

以下實例展示了replace()函數的使用方法:

#!/usr/bin/python str = "this is string example....wow!!! this is really string"; print str.replace("is", "was"); print str.replace("is", "was", 3);

以上實例輸出結果如下:

thwas was string example....wow!!! thwas was really string thwas was string example....wow!!! thwas is really string

?

總結

以上是生活随笔為你收集整理的python删除、替换字符串某字符后的字符串(删除字符串、替换字符串、strip、split、rstrip、lstrip、replace)的全部內容,希望文章能夠幫你解決所遇到的問題。

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