1.strip():
str.strip([chars]);去除字符串前面和后面的所有设置的字符串,默认为空格
chars -- 移除字符串头尾指定的字符序列。
st = " hello " st = st.strip() print(st+"end")
输出:
如果设置了字符序列的话,那么它会删除,字符串前后出现的所有序列中有的字符。但不会清除空格。
st = "hello" st = st.strip('h,o,e') print(st)
因为,在h去除之后,e便出现在首位,所以e也会被去除,最终得到的答案就是ll
2.replace():
替代字符串中的某一些子串为另一些字符。 str.replace(old, new[, max])
old -- 将被替换的子字符串。
new -- 新字符串,用于替换old子字符串。
max -- 可选字符串, 替换不超过 max 次
替换某一个子串:
st = "i want a apple" st = st.replace("apple","mice") print(st)
规定最大替换次数:
st = "i want a apple and a apple" st = st.replace("apple","mice",1) print(st)
3.re.sub()
替换字符串中的某些子串,可以用正则表达式来匹配被选子串。
re.sub(pattern, repl, string, count=0, flags=0)
pattern:表示正则表达式中的模式字符串;
repl:被替换的字符串(既可以是字符串,也可以是函数);
string:要被处理的,要被替换的字符串;
count:匹配的次数, 默认是全部替换
如下,用正则方便多了,匹配所有连续出现的数字(把2019换成了danshenggou:):
st = "hello 2019" st = re.sub("([0-9]+)","danshengou",st) print(st)
匹配连续出现两次的a,并把它换成一个。
st = "hello aabbaa" st = re.sub("(a{2})","a",st) print(st)
以上这篇对python 中re.sub,replace(),strip()的区别详解就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。
华山资源网 Design By www.eoogi.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
华山资源网 Design By www.eoogi.com
暂无评论...
更新日志
2024年09月23日
2024年09月23日
- 国风大师纯音系列《古筝仙子 常静演奏 十指弹一》1CD[FLAC][919MB]
- DasKapital-OneMustHaveChaosInsidetoGiveBirthtoaDancingStar(2024)[24-44.1]WAV
- 王菲.1999-《只爱陌生人》日本东芝版[低速原抓WAV+CUE]
- 张小英1987-金奖金曲第一集[新加坡版][WAV+CUE]
- 鹿晗.2015-重启【海蝶】【FLAC分轨】
- 张芸京.2012-小女孩【金牌大风】【WAV+CUE】
- 余天.2002-雄狮魅力金曲2CD(南方金点系列)【南方】【WAV+CUE】
- 银霞.1982-《结·SAYYESMYBOY》台湾复刻版[WAV+CUE]
- 梁汉文1993-火热动感[引进版][WAV+CUE]
- 梁汉文2008-EDMONDHITS48[香港]3CD[WAV+CUE]
- 国风大师纯音系列《童丽-[渭城曲]》1CD[WAV分轨][593.5MB]
- 国风大师纯音系列《许菱子《烧筝2》古筝曲集》1CD[WAV分轨][1.3GB]
- 王子异《1996 Part.2》[320K/MP3][18.29MB]
- 群星.1994-宝丽金最倾情3CD【宝丽金】【WAV+CUE】
- 乐俊亨.2024-同泪【CharioticMusic】【FLAC分轨】