python删除str中特定字符的方法
1、删除字符串首尾的多余字符串strip()
#删除字符串中多余字符
defstring_remove():
str1='abc\n'
printstr1.strip()#abc
str2='----abcdf++++'
printstr2.strip('-+')#abcdf
2、replace函数,删除字符串中某一个所有的字符串
ss='oldoldstring'
ret=ss.replace('old','new',1)
print(ret)
3、sub函数,同时删除多个字符串,使用正则表达式
str2='\nabc\nwrt22\t666\t'#删除字符串中的所有\n,\t
importre
print(re.sub('[\n\t]','',str2))#abcwrt22666
以上就是python删除str中特定字符的方法,希望对大家有所帮助。更多Python学习教程请关注IT培训机构:千锋教育。