Python String maketrans()

The string maketrans() method returns a mapping table for translation usable for translate() method.

In simple terms, the maketrans() method is a static method that creates a one to one mapping of a character to its translation/replacement.

It creates a Unicode representation of each character for translation.

This translation mapping is then used for replacing a character to its mapped character when used in translate() method.


The syntax of maketrans() method is:

string.maketrans(x[, y[, z]])

Here, y and z are optional arguments.


String maketrans() Parameters

The maketrans() method takes 3 parameters:

  • x - If only one argument is supplied, it must be a dictionary.
    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 - If two arguments are passed, it must be two strings with equal length.
    Each character in the first string is a replacement to its corresponding index in the second string.
  • z - If three arguments are passed, each character in the third argument is mapped to None.

Return value from String maketrans()

The maketrans() method returns a translation table with a 1-to-1 mapping of a Unicode ordinal to its translation/replacement.


Example 1: Translation table using a dictionary with maketrans()

# example dictionary
dict = {"a": "123", "b": "456", "c": "789"}
string = "abc"
print(string.maketrans(dict))

# example dictionary
dict = {97: "123", 98: "456", 99: "789"}
string = "abc"
print(string.maketrans(dict))

When you run the program, the output will be:

{97: '123', 98: '456', 99: '789'}
{97: '123', 98: '456', 99: '789'}

Here, a dictionary dict is defined. It contains a mapping of characters a,b and c to 123, 456 and 789 respectively.

maketrans() creates a mapping of the character's Unicode ordinal to its corresponding translation.

So, 97 ('a') is mapped to '123', 98 'b' to 456 and 99 'c' to 789. This can be demonstrated from the output of both dictionaries.

Also, if two or more characters are mapped in the dictionary, it raises an exception.


Example 2: Translation table using two strings with maketrans()

# first string
firstString = "abc"
secondString = "def"
string = "abc"
print(string.maketrans(firstString, secondString))

# example dictionary
firstString = "abc"
secondString = "defghi"
string = "abc"
print(string.maketrans(firstString, secondString))

When you run the program, the output will be:

{97: 100, 98: 101, 99: 102}
ValueError: the first two maketrans arguments must have equal length

Here first, two strings of equal length "abc" and "def" are defined. And the corresponding translation is created.

Printing only the first translation gives you a 1-to-1 mapping to each character's Unicode ordinal in firstString to the same indexed character on the secondString.

In this case, 97 ('a') is mapped to 100 ('d'), 98 ('b') to 101 ('e') and 99 ('c') to 102 ('f').

Trying to create a translation table for unequal length strings raises a ValueError exception indicating that the strings must have equal length.


Example 3: Translational table with removable string with maketrans()

# first string
firstString = "abc"
secondString = "def"
thirdString = "abd"
string = "abc"
print(string.maketrans(firstString, secondString, thirdString))

When you run the program, the output will be:

{97: None, 98: None, 99: 102, 100: None}

Here, first the mapping between the two strings firstString and secondString are created.

Then, the third argument thirdString resets the mapping of each character in it to None and also creates new mapping for non-existent characters.

In this case, thirdString resets the mapping of 97 ('a') and 98 ('b') to None, and also creates a new mapping for 100 ('d') mapped to None.