Example 1 — Build and Use a Table
Make a table from two strings, look inside it, and apply it with translate().
table = str.maketrans("abc", "xyz")
print(table)
print(table[ord("a")])
print("aabbcc".translate(table)) How It Works
- A dictionary of numbers. The keys 97, 98, and 99 are the code numbers of
a,b, andc. - Look up one letter.
ord("a")gives 97, and the table maps it to 120, the code number ofx. - Apply the table.
translate()replaces everyawithx, everybwithy, and everycwithz.

