你的位置:首页 > 信息动态 > 新闻中心
信息动态
联系我们

python中删除字符串中的指定字符

2022/8/15 16:48:29

 

001、 split + join实现

>>> str1 = 'abcdedfg'
>>> str1
'abcdedfg'
>>> str1.split('d')                ## 拆分为列表
['abc', 'e', 'fg']
>>> "".join(str1.split('d'))       ## 组合成字符串
'abcefg'

 

002、字符串内置函数replace实现

>>> str1 = 'abcdedfg'              ## 测试字符串
>>> str1
'abcdedfg'
>>> str1.replace("d", "")          ## 删除d
'abcefg'
>>> str1.replace("d", "", 1)       ## 删除第一个d
'abcedfg'