How to remove some dots from a string in python
Solution 1:
Here is a solution:
p = '23.4565.90'
def rreplace(s, old, new, occurrence):
li = s.rsplit(old, occurrence)
return new.join(li)
# First arg is the string
# Second arg is what you want to replace
# Third is what you want to replace it with
# Fourth is how many of them you want to replace starting from the right.
# which in our case is all but the first '.'
d = rreplace(p, '.', '', p.count('.') - 1)
print(d)
>>> 23.456590
Credit to How to replace all occurences except the first one?.
Solution 2:
What about str.partition
?
p = '23.4565.90'
a, b, c = p.partition('.')
print(a + b + c.replace('.', ''))
This would print:
23.456590
EDIT: the method is partition
not separate