hhafez hhafez - 2 months ago 21
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

Answer

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.

Comments