如何从诸如Python之类的字符串中去除逗号Foo, bar
?我尝试过'Foo, bar'.strip(',')
,但是没有用。
Answers:
使用replace
字符串的方法不是strip
:
s = s.replace(',','')
一个例子:
>>> s = 'Foo, bar'
>>> s.replace(',',' ')
'Foo bar'
>>> s.replace(',','')
'Foo bar'
>>> s.strip(',') # clears the ','s at the start and end of the string which there are none
'Foo, bar'
>>> s.strip(',') == s
True
s = re.sub(',','', s)
;)