Site icon i2tutorials

maketrans() method in Python String Module

maketrans() method in Python String Module


maketrans methodin string module is a static method which creates a translation table that can be used for translate() method

maketrans method creates a mapping of the characters and maps itself for translation or replacement.


Syntax:

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


Parameters:

There are 3 parameters in the maketrans method.

Here, 97 is the Unicode translation of ‘a’.


Here, we can build a translation table with one argument, two arguments, and three arguments.


Example-1: With one argument


In this example, every (key) character in the given key value pair  translated to Unicode representation

str.maketrans({'a': 'b', 'c': None})


Output:


Example 2: With two arguments


In this example, Unicode representation of each character in the given first string mapped with second string.

str.maketrans('abc', 'xyz')


Output:


Example 3: With three arguments


In this example, Unicode representation of each character in the given first string mapped with second string.

From the third String are mapped to None. It means not to replace anything but remove the characters that show up in the string.

str.maketrans('abc', 'xyz', 'hij')


Output:

Exit mobile version