Python maketrans() and translate() functions

The maketrans() method returns a mapping table for translation usable for translate() method. This is a static method that creates a one to one mapping of a character to its translation/replacement. This method creates a Unicode representation of each character for translation.
The syntax of maketrans() method is −
string.maketrans(x[, y[, z]])
y and z are optional arguments.

The maketrans() method has 3 parameters.
x - In this case if only one argument is supplied, it must be a dictionary and the dictionary should contain 1-to-1 mapping from a single character string to its translation OR a Unicode number (97 for 'a') to its translation.
y - In this case if two arguments are passed, it must be two strings with equal length and each character in the first string is a replacement to its corresponding index in the second string.
z - In this case if three arguments are passed, each character in the third argument is mapped to none.
This method returns a translation table with a 1-to-1 mapping of a Unicode ordinal to its translation/replacement.
Translation table using a dictionary with maketrans()
dict= {"a": "123", "b": "456", "c": "789"}
my_string = "abc"
print(my_string.maketrans(dict))
# example dictionary
dict = {97: "123", 98: "456", 99: "789"}
my_string = "abc"
print(my_string.maketrans(dict))
{97: '123', 98: '456', 99: '789'}
{97: '123', 98: '456', 99: '789'}

Explanation − In this example, dict is a dictionary and It contains a mapping of characters a,b and c to 123, 456 and 789 respectively. The maketrans() creates a mapping of the character's Unicode ordinal to its corresponding translation. So, the result is 97 ('a') is mapped to '123', 98 'b' to 456 and 99 'c' to 789.
Translation table using two strings with maketrans()
my_string1 = "abc"
my_string2 = "def"
string = "abc"
print(string.maketrans(my_string1, my_string2))
# Example dictionary
my_string1 = "abc"
my_string2 = "defghi"
string = "abc"
print(string.maketrans(my_string1, my_string2))
{97: 100, 98: 101, 99: 102}
ValueError: the first two maketrans arguments must have equal length

Explanation − In this example two strings of equal length "abc" and "def" are defined. And the corresponding translation is created and printing only the first translation gives you a 1-to-1 mapping to each character's Unicode ordinal in my_string1 to the same indexed character on the my_string2.
But in this case, 97 ('a') is mapped to 100 ('d'), 98 ('b') to 101 ('e') and 99 ('c') to 102 ('f') and trying to create a translation table for unequal length strings raises a ValueError exception indicating that the strings must have equal length.
translate(). We know that a translation table maps characters to other characters. We use the translate() method in Python for making many character replacements in strings. First we build a translation dictionary with maketrans() and pass this to translate.
#Python program that uses maketrans, translate
dict = str.maketrans("abc", "def")
print(dict)
# Translate this value.
value = "aabbcc"
result = value.translate(dict)
print(result)
{97: 100, 98: 101, 99: 102}
ddeeff

# Create translation table.
table = str.maketrans("78", "12", "9")
# Translate this string.
input = "123456789"
result = input.translate(table)
# Write results.
print(input)
print(result)
123456789
12345612

#Create translation table.
trans = str.maketrans("abcdefghijklmnopqrstuvwxyz","nopqrstuvwxyzabcdefghijklm")
# Apply rot13 translation.
print("gandalf".translate(trans))
print("gandalf".translate(trans).translate(trans))
tnaqnys
gandalf