Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

python remove all text between first and second comma in string

I have a string like this

gas,buck,12345,10fifty

how can I end up with this string?

gas,,12345,10fifty

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

One option might be using list comprehension with split and join, although it might be inefficient:

s = "gas,buck,12345,10fifty"

output = ",".join("" if i == 1 else x for i, x in enumerate(s.split(",")))
print(output) # gas,,12345,10fifty

Alternatively, in this specific case, you can use re:

output = re.sub(',.*?,', ',,', s, count=1)
print(output) # gas,,12345,10fifty
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading