如何在Python字符串中删除逗号

msa*_*aio 60 python string strip

如何从Python字符串中删除逗号Foo, bar?我试过了'Foo, bar'.strip(','),但没办法.

eum*_*iro 127

你想要replace它,而不是strip它:

s = s.replace(',', '')
Run Code Online (Sandbox Code Playgroud)

  • @jamylak`s = re.sub(',','',s)`;) (18认同)

pra*_*nsg 13

使用replace字符串的方法不是strip:

s = s.replace(',','')
Run Code Online (Sandbox Code Playgroud)

一个例子:

>>> 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
Run Code Online (Sandbox Code Playgroud)

  • 男人!太明显了!太明显了!但很明显,我正在使用 strip 并试图理解为什么它不能作为批量替换...... (2认同)

小智 6

unicode('foo,bar').translate(dict([[ord(char), u''] for char in u',']))