首页 > 解决方案 > 方法 strip() 没有从我的字符串中删除字符

问题描述

>>> y = 'This is string, should be without commas, but is not working'
>>> y = x.strip(",")
>>> y
'This is string, should be without commas, but is not working'

我想从这个字符串中删除逗号,但如上所示,strip 方法不想工作。

标签: pythonpython-3.xstringstrip

解决方案


strip()函数仅删除前导字符和尾随字符。而且因为你给它一个逗号作为参数,它只会删除前导和尾随逗号。

要从字符串中删除所有逗号,您可以使用该replace()函数。所以你的第二行将变成:

y = x.replace(",", "")

推荐阅读