Python String maketrans() Method: Complete Guide

Python string maketrans() function returns the mapping table for translation usable for the translate() method. In addition, it specifies the list of characters that are needed to be replaced from the main string or deleted.

Python String maketrans()

Python string maketrans() is a built-in mapping function that maps the characters of a string to its replacement or, in other words, its translation. The maketrans() is a helper method for the translate() method. It helps the translate() method by returning a translation table used for a one-to-one mapping of string characters.

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

It creates the Unicode representation of each character for translation.

That translation mapping is then used for replacing the character with its mapped character when used in the translate() method.

Syntax

See the following syntax.

maketrans( str_var1, str_var2, str_var3)

The arguments supplied are optional, but at least one argument should be there.

Parameters

There are three parameters.

The first is str_var1; it contains the characters required to be replaced.

The second is str_var 2. It contains the characters with which the characters of str_var1 need to be replaced.

The third is optional, which specifies the characters required to be deleted from the main list.

Return value

It returns a transition table used by the translate() function.

Example program using maketrans() method in python

Example 1: Write a program to show the working of the maketrans() method.

# app.py

h1 = "Hello"
h2 = "Girls"
str_var = "Hello"
print(str_var.maketrans(h1, h2))

Output

➜  pyt python3 app.py
{72: 71, 101: 105, 108: 108, 111: 115}
➜  pyt

Example 2: Write a program to print the transition table, including the third parameter, which removes characters from a string.

# app.py

h1 = "qwerty"
h2 = "bowled"
h3 = "qweled"
h4 = "qwerty"
print(h4.maketrans(h1, h2, h3))

Output

➜  pyt python3 app.py
{113: None, 119: None, 101: None, 114: 108, 116: 101, 121: 100, 108: None, 100: None}
➜  pyt

Translation table using a dictionary with maketrans()

Let’s use the maketrans() function with the Python dictionary.

See the following code.

# app.py

dict = {"a": "219", "b": "211", "c": "229"}
string = "kgb"
print(string.maketrans(dict))

dict = {97: "219", 98: "211", 99: "229"}
string = "kgb"
print(string.maketrans(dict))

Output

➜  pyt python3 app.py
{97: '219', 98: '211', 99: '229'}
{97: '219', 98: '211', 99: '229'}
➜  pyt

Here, the dictionary dict is defined. It contains the mapping of characters a,b and c to 219, 211, and 229, respectively.

The maketrans() creates mapping the character’s Unicode ordinal to its corresponding translation.

So, 97 (‘a’) is mapped to ‘219’, 98 ‘b’ to 211 and 99 ‘c’ to 229. This can be demonstrated from the output of both dictionaries.

Conclusion

Python string method maketrans() returns the translation table that maps each character in the intab string into a character at the same position in the outtab string. Then this table is passed to a translate() function. One thing to note is that both intab and outtab must have the same length.

That’s it for this article.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.