微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

方法strip不能从我的字符串中删除字符

如何解决方法strip不能从我的字符串中删除字符

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

我想从此字符串中删除逗号,但是如上所示,strip方法不起作用。

解决方法

strip()函数仅删除开头和结尾字符。而且因为您给它加上了逗号作为参数,所以它只会删除前导和尾随逗号。

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

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

strip()方法通过删除开头和结尾字符(基于传递的字符串参数)来返回字符串的副本。例如,在您的工作案例strip()上可以使用类似的方法:

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

这将为您提供所需的东西:

y = 'This is string,but is not working'
x = y.replace(',','')

哪个输出:

print(x)
This is string should be without commas but is not working

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。