Python String maketrans() Method with Examples

In the previous article, we have discussed Python String isupper() Method Examples
maketrans() Method in Python:

The string maketrans() method returns a translation mapping table that can be used by the translate() method.

To put it simply, the maketrans() method is a static method that generates a one-to-one mapping of a character to its translation/replacement.

Each character is given a Unicode representation for translation.

When the translate() method is called, this translation mapping is used to replace a character with its mapped character.

Syntax:

string.maketrans(x, y, z)

Parameters:

x: If there is only one argument, it must be a dictionary.
A 1-to-1 mapping from a single character string to its translation OR a Unicode number (97 for ‘a’) to its translation should be included in the dictionary.

y: If two arguments are passed, they must be of equal length.
Each character in the first string is a replacement for the index in the second string that corresponds to it.

z: If three arguments are passed, the third argument’s characters are mapped to None.

Return Value:

The maketrans() method returns a translation table that contains a one-to-one mapping of a Unicode ordinal to its translation/replacement.

Examples:

Example1:

maketrans() Translation table using a dictionary
gvn_dict = {"a": "100", "b": "200", "c": "300"}
gvn_str = "abc"
print(gvn_str.maketrans(gvn_dict))

gvn_dict = {97: "100", 98: "200", 99: "300"}
gvn_str = "abc"
print(gvn_str.maketrans(gvn_dict))

Output:

{97: '100', 98: '200', 99: '300'}
{97: '100', 98: '200', 99: '300'}

Explanation:

A dictionary gvn_dict is defined here. It has a mapping of the characters a,b, and c to 100,200, and 300, respectively.

maketrans() generates a mapping from the character’s Unicode ordinal to its translation.

As a result, 97 (‘a’) corresponds to ‘100,’ 98 ‘b’ corresponds to 200, and 99 ‘c’ corresponds to 300. The output of both dictionaries demonstrates this.

In addition, if two or more characters are mapped in the dictionary, an exception is raised.

Example2:

maketrans() Translation table using two strings
gvn_fststr = "abc"
gvn_scndstr = "def"
gvn_str = "abc"
print(gvn_str.maketrans(gvn_fststr, gvn_scndstr))

gvn_fststr = "abc"
gvn_scndstr = "defghi"
gvn_str = "abc"
print(gvn_str.maketrans(gvn_fststr, gvn_scndstr))

Output:

Traceback (most recent call last):
  File "/home/d57f28dec313688f30b673716d7d7ec8.py", line 9, in <module>
    print(gvn_str.maketrans(gvn_fststr, gvn_scndstr))
ValueError: the first two maketrans arguments must have equal length

Explanation:

First, two strings of equal length are defined: abc and def. The corresponding translation is then generated.

Only printing the first translation results in a 1-to-1 mapping from each character’s Unicode ordinal in the first string to the same indexed character in the second string.

In this case, 97 (‘a’) corresponds to 100 (‘d’), 98 (‘b’) corresponds to 101 (‘e’), and 99 (‘c’) corresponds to 102 (‘f’).

Attempting to create a translation table for strings of unequal length raises a ValueError exception, indicating that the strings must be of equal length.

Example3:

maketrans() Translation table with removable string
gvn_fststr = "abc"
gvn_scndstr = "def"
gvn_thrdstr = "abd"
gvn_str = "abc"
print(gvn_str.maketrans(gvn_fststr, gvn_scndstr, gvn_thrdstr))

Output:

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

Explanation:

The mapping between the two strings firstString and secondString is created first.

The third argument, thirdString, then resets each character’s mapping to None and creates a new mapping for non-existent characters.

In this case, thirdString resets the mappings of 97 (‘a’) and 98 (‘b’) to None, as well as creating a new mapping for 100 (‘d’) that is also mapped to None.

Go through our tutorial and learn about various Python String Method Examples and learn how to apply the knowledge while dealing with strings.