You want to replace it, not strip it:
s = s.replace(',', '')
Answer from eumiro on Stack OverflowYou want to replace it, not strip it:
s = s.replace(',', '')
Use replace method of strings not strip:
s = s.replace(',','')
An example:
>>> 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
I have a string that consists of something like:
my_string = 'Now is the time, , for all good men, , ,to come to the aid,, of their party'
...and I want to keep only a single comma for each repeating set of commas:
result = 'Now is the time, for all good men, to come to the aid, of their party'
I've looked a numerous methods to remove sequential characters, but 1) these are not sequential (may have one or more blanks between them); and 2) I only want to remove the extra commas and not other repeating characters that might appear in the string.
Any help would be greatly appreciated.
Remove Comma from Python String - Ask a Question - TestMu AI Community
I must be an idiot or something. Why can't I strip the comma's out of a string?
Removing commas in a string
Efficient Way To Remove Repeating Commas From String
Here's my code. Simple as it seems, it is driving me crazy, and I'm sure there is a simple solution.
test = '12,123,123,123'
test2 = test.strip(',')
test2
'12,123,123,123'
test2 = test.strip('\,')
test2
'12,123,123,123'
test2 = test.strip(r',')
test2
'12,123,123,123'What am I doing wrong? :)
Thanks,