如何删除python字符串中指定的字符?

图片[1]-如何删除python字符串中指定的字符?-uusu优素-乐高,模型,3d打印,编程

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

方法一:使用pop()方法删除特定位置的字符

string1='不要汉堡'#定义一个字符串
list_str=list(string1)#将字符串转换为列表
list_str.pop(1)#删去第一个字符
string2=''.join(list_str)#再将列表转换成字符串
print(string2)

输出

要汉堡

方法二:使用replace()方法删除指定字符

html="fasdfasdf..fadsfasf"
foriinhtml:
ifi=='.':
html=html.replace(i,'')#将.删掉

方法三:使用re.sub()方法可以替换特定模式的字符

>>>#导入re
>>>importre
>>>s='/n1/t2/r3/n4/t5/r6/n7/t8/r9'
>>>re.sub('[/n/t/r]','',s)
'123456789'
原文来自:https://www.py.cn
© 版权声明
THE END
喜欢就支持一下吧
点赞6 分享
评论 抢沙发
头像
欢迎您留下宝贵的见解!
提交
头像

昵称

取消
昵称表情代码图片

    暂无评论内容