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

Replacing multiple characters at once

Is there any way to replace multiple characters in a string at once, so that instead of doing:

"foo_faa:fee,fii".replace("_", "").replace(":", "").replace(",", "")

just something like (with str.replace())

"foo_faa:fee,fii".replace(["_", ":", ","], "")

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 :

An option that requires no looping or regular expressions is translate:

>>> "foo_faa:fee,fii".translate(str.maketrans('', '', "_:,"))
"foofaafeefii"

Note that for Python 2, the API is slightly different.

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