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

 

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'

 

posted @ 2022-08-15 16:44  小鲨鱼2018  阅读(699)  评论(0编辑  收藏  举报