hhafez hhafez - 9 months ago 86
Python Question

Character Translation using Python (like the tr command)

Is there a way to do character translation (kind of like the tr command) using python


See string.translate

import string
"abc".translate(string.maketrans("abc", "def")) # => "def"

Note the doc's comments about subtleties in the translation of unicode strings.

Edit: Since tr is a bit more advanced, also consider using re.sub.