📅 2012-May-04 ⬩ ✍️ Ashwin Nanjappa ⬩ 🏷️ python, translation ⬩ 📚 Archive
Python has a couple of functions that make it easy to replace characters in a string with other characters. bytes.maketrans()
is used to create the translation table. string.translate()
uses this table to replace the matching characters in the string with their replacements in the table.
# Replace 'a' with '1', 'b' with '2' and 'c' with '3'
= bytes.maketrans( b"abc", b"123" )
table = "abracadabra"
s = s.translate( table )
s2 print( s2 ) # 12r131d12r1
Tried with: Python 3.2