13518219792

建站动态

根据您的个性需求进行定制 先人一步 抢占小程序红利时代

创新互联Python教程:python删除str中特定字符的方法

1、删除字符串首尾的多余字符串strip()

# 删除字符串中多余字符
def string_remove():
   str1 = ' abc     \n'
   print str1.strip()   # abc
 
   str2 = '----abcdf++++'
   print str2.strip('-+')  # abcdf

2、replace函数,删除字符串中某一个所有的字符串

ss = 'old old string'
ret = ss.replace('old', 'new', 1)
print(ret)

3、sub函数,同时删除多个字符串,使用正则表达式

str2 = '\nabc\nwrt22\t666\t'  # 删除字符串中的所有\n,\t
import re
print(re.sub('[\n\t]','',str2))   # abcwrt22666

以上就是python删除str中特定字符的方法,希望对大家有所帮助。更多Python学习指路:创新互联python教程

本文教程操作环境:windows7系统、Python 3.9.1,DELL G3电脑。


网页题目:创新互联Python教程:python删除str中特定字符的方法
分享链接:http://cdbrznjsb.com/article/cdsiojs.html

其他资讯

让你的专属顾问为你服务