新聞中心
這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷解決方案
創(chuàng)新互聯(lián)Python教程:python刪除str中特定字符的方法
1、刪除字符串首尾的多余字符串strip()

# 刪除字符串中多余字符
def string_remove():
str1 = ' abc \n'
print str1.strip() # abc
str2 = '----abcdf++++'
print str2.strip('-+') # abcdf2、replace函數(shù),刪除字符串中某一個(gè)所有的字符串
ss = 'old old string'
ret = ss.replace('old', 'new', 1)
print(ret)3、sub函數(shù),同時(shí)刪除多個(gè)字符串,使用正則表達(dá)式
str2 = '\nabc\nwrt22\t666\t' # 刪除字符串中的所有\(zhòng)n,\t
import re
print(re.sub('[\n\t]','',str2)) # abcwrt22666以上就是python刪除str中特定字符的方法,希望對(duì)大家有所幫助。更多Python學(xué)習(xí)指路:創(chuàng)新互聯(lián)python教程
本文教程操作環(huán)境:windows7系統(tǒng)、Python 3.9.1,DELL G3電腦。
文章名稱:創(chuàng)新互聯(lián)Python教程:python刪除str中特定字符的方法
本文地址:http://m.fisionsoft.com.cn/article/cdsiojs.html


咨詢
建站咨詢
