Skip to main content
 首页 » 编程设计

python之摆脱python中字符串中的某些字符

2025年05月04日20linjiqin

我想删除字符串中间的字符。这些字符是 =、p、 和 H。因为它们不是字符串中最左边和最右边的字符,所以我不能使用 strip()。是否有一个函数可以删除字符串中任意位置的某个字符?

请您参考如下方法:

这项工作的常用工具是str.translate

https://docs.python.org/2/library/stdtypes.html#str.translate

>>> 'hello=potato'.translate(None, '=p') 
'hellootato'