maketrans() in Python

Learn via video course
FREE
View all courses
Python Course for Beginners With Certification: Mastering the Essentials
Python Course for Beginners With Certification: Mastering the Essentials
by Rahul Janghu
1000
4.90
Start Learning
Python Course for Beginners With Certification: Mastering the Essentials
Python Course for Beginners With Certification: Mastering the Essentials
by Rahul Janghu
1000
4.90
Start Learning
Topics Covered

Overview

The maketrans() function in Python is used to generate an Dictionary like object (collection of key-value pairs) that associates a character of a string (key) with its replacement (value) which provides character mapping for the string translation process.

Syntax for maketrans in Python

The syntax of maketrans() function in Python is:

where str is the Python String object and x is the required parameter whereas the y and z are optional parameters for the function.

Parameters for maketrans in Python

The maketrans() function acccepts three parameters which are described below:

x (Required Parameter)

  • This parameter can either accept a Python Dictionary or a String object depending on the presence of the optional parameter y.
  • If the parameter y is provided then it accepts a Python String object that specifies the list of characters that are required to be replaced in the string.
  • Otherwise, the parameter x accepts a Python Dictionary that represents the one-to-one mapping of a character to its translation (replacement).

y (Optional Parameter)

  • This parameter accepts a Python String object that specifies the replacement of the characters specified in the required parameter x.
  • The length of the string passed to this parameter should be equal to the length of the string passed as the required parameter x, as every character in the first string parameter (x) is substituted with the character present at the corresponding index in the second string parameter (y) via one-to-one character mapping.

z (Optional Parameter)

  • This parameter accepts a Python String object that specifies the list of characters that needs to be deleted.
  • Every character of the string passed as the optional z parameter is mapped to the None value.

Return Values for Maketrans in Python

Return Type : dict Description : The maketrans() function returns a translation table that specifies the conversions that can be used by translate(). Here, the translation table is a Python Dictionary object that contains the one-to-one mapping of Unicode code points to their replacement or translations.

Example for maketrans in Python

Output:

What is Maketrans in Python?

The maketrans() function in Python is a built-in string method that is used to generate a collection of key-value pairs where each key-value pair represents a one-to-one mapping between a character and its substitution. This mapping is used by another built-in method known as the translate() function to perform the string translation (process to replace the characters from a string according to the given character mapping).

The maketrans() translation table takes use of the Unicode representation of characters and generates an associative array where the key represents the characters that are to be replaced and the corresponding values indicate their translations. The value in the mapping can be a single character, a string, or a Unicode code point (An integer value defined in the Unicode encoding standard that uniquely identifies a given character).

We can notice the use of Unicode mapping in the earlier discussed example and the figure given below.

Notice that while generating the translation table table1, the maketrans() function is internally converting the characters of the string x into their Unicode ordinals (character a is represented by decimal 97, b by 98, c by 99), and then maps these Unicode ordinals with the corresponding characters of string y.

Maketrans Function Example

Also note that, when we use all the three parameters in the maketrans() function, the characters present in string z are all mapped to None value i.e., these characters (here, characters a, x, and y) will get deleted from the target string.

How to use Maketrans Function in Python?

We have already seen that the translation table generated by the maketrans() function depends upon the parameters passed to the method. Hence, based on the parameters of the maketrans() function, there are mainly three different ways in which we can use the maketrans() function in Python:

  • Maketrans() using Single Argument - In this method we fed a Python Dictionary object as the only argument to the maketrans() function.
  • Maketrans() using Two Arguments - In this method we provide two string arguments (x and y) to the maketrans() function.
  • Maketrans() using All the Arguments - This is the case when all the three arguments are passed to the maketrans() function.

More Examples

Example 1 Table Using a Dictionary With Maketrans()

Output:

Explanation:

In the case of using a single argument in the maketrans() function, the argument must be a Python Dictionary object that represents a mapping of characters. This Dictionary is then converted into a translation table by the maketrans() function.

Note - The main difference between a normal Python Dictionary and a translation table is that in the case of a translation table, Unicode ordinals (a decimal number which is used to identify a character encoded using the Unicode encoding standard) are used as keys in place of characters.

Example 2 : Translation Table Using Two Strings With maketrans()

Output:

Explanation:

When two arguments are passed while using the maketrans() function, the arguments must be two strings of the same length as each character in the first string argument is mapped with the character present in the corresponding index of the second string. Hence, in the above example the character y is mapped with h and the character o is mapped with i.

Example 3 : Translational Table with Removable String With maketrans()

Output:

When all the three arguments are passed while using the maketrans() function, then all the characters of the third parameter are mapped to None i.e., they are deleted from the target string, while the characters of the first parameter are replaced by the characters of the second parameter.

Hence, in the above example the characters X, Y, and Z are all mapped to None while the other characters are mapped according to the first two parameters.

Example 4 : Create Mapping Table Using maketrans() and Pass to translate()

Output:

Here, we are using another built-in string function known as the translate() function. This function accepts the translation table generated by the maketrans() function to perform string translations.

In the above example, we are generating a translation table that maps the vowels a, e, i, o, and u with numbers 1 to 5 respectively. Also, the translation table generated by the maketrans() function maps certain characters such as # and ! to None.

This translation table is provided to the translate() function which then performs the string translation on the target string i.e., the translate() function removes the characters # and ! and replaces the vowels with the corresponding numbers in the target string.

Example 5 : maketrans() and translate()

Output:

Here, we are using the maketrans() function with two parameters to generate a translation table that maps all the lower case characters with their corresponding upper case character. This translation table is then passed as an argument to the translate() function which translates the target string accordingly.

Example 6 : Remove Chars

Output:

We can also use the combination of maketrans() and translate() functions to only remove characters from the target string. This is done by passing empty strings in the first two parameters of the maketrans() function. In this case, only the third parameter (which is used to map characters to None value) will be used to generate the translation table as shown in the example.

Conclusion

  • The maketrans() method of the str class is used to generate a translation table for the string translation process.
  • It returns a Python Dictionary object that contains one-to-one Unicode-based character mappings.
  • Maketrans accepts at most 3 parameters (one mandatory and two optional) and generates different mappings according to these parameters.
  • The argument must be a Python Dictionary when only one argument is used in the maketrans() function.
  • Two strings of equal lengths are used, when two arguments are passed in maketrans().
  • The third argument is used to map the characters to the None value i.e., the third argument represents the deletion of characters.
  • maketrans() function is a helper function which is mainly used to generate translation table for the translate() function.
  • translate() function performs string translations on the basis of the translation table provided by the maketrans() function.

See Also: