简体   繁体   中英

Remove first comma in a string with Python

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?

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:])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM