首页 > 编程语言 >python中删除字符串中的指定字符

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

时间:2022-08-15 16:59:34浏览次数:47  
标签:字符 python str1 replace ## split 字符串 abcdedfg

 

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'

 

标签:字符,python,str1,replace,##,split,字符串,abcdedfg
From: https://www.cnblogs.com/liujiaxin2018/p/16588820.html

相关文章