hhafez hhafez - 1 year ago 138
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 Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download