Python - Remove A Character The Second Time It Is Duplicated
I'm looking to remove a ',' (comma) from a string, but only the second time the comma occurs as it needs to be in the correct format for reverse geocoding... As an example I have t
Solution 1:
If you're sure that string only contains two commas and you want to remove the last one you can use rsplit
with join
:
>>> s = '43,14,3085'
>>> ''.join(s.rsplit(',', 1))
'43,143085'
In above rsplit
splits starting from the end number of times given as a second parameter:
>>> parts = s.rsplit(',', 1)
>>> parts
['43,14', '3085']
Then join
is used to combine the parts together:
>>> ''.join(parts)
'43,143085'
Solution 2:
What about something like:
i = s.find(',')
s[:i] + ',' + s[i+1:].replace(",", "")
Solution 3:
This will get rid of all your commas excepts the first one:
string = '43,14,3085'
splited = string.split(',')
string=",".join(splited[0:2])
string+="".join(splited[2:])
print(string)
Post a Comment for "Python - Remove A Character The Second Time It Is Duplicated"