Remove first comma in a string with Python

Viewed 225

I have this string

s = "1,395,54"

I would like to remove the first comma to obtain the following string:

s = "1395,54"

What is the most efficient way to solve this simple problem?

2 Answers

You can use str.replace, it takes a third argument which specifies the number of occurrences to replace.

>>> your_str = "1,395,54"
>>> your_str.replace(",", "", 1)
'1395,54'

By slicing the string. The find method return the index of the first match

s = "1,395,54"

index = s.find(',')
print(s[:index] + s[index+1:])
Related