Signup/Sign In

Python String maketrans()

In Python, the maketrans() method is used to map the character of a string with its replacement basically it indicates translation.

  • It is an inbuilt mapping function of string in Python.

  • The maketrans() method acts as a helper method to the translate() method.

  • It mainly specifies the characters that are needed to be replaced or deleted from the main string.

  • Thus, maketrans() method mainly returns a mapping table that is used for further translation by the translate() method.

  • It is basically a static method that is used for one to one mapping of characters of a string to its replacement characters.

  • This method helps in creating a Unicode representation of each character for its translation.

Python String maketrans(): Syntax

Below we have a basic syntax of the string maketrans() function in Python:

maketrans( str_var1, str_var2, str_var3)

Note: The parameters passed in the above method's syntax are optional but at least one parameter should be there.

Python String maketrans(): Parameters

The syntax indicates that there are three parameters accepted by the maketrans() method and the description for the same are given below:

  • str_var1

    It is used to represent the characters that are needed to be replaced.

  • str_var2

    It is used to represent the characters with which the characters of str_var1 need to be replaced.

  • str_var3

    It is an optional parameter and it represents the characters needed to be replaced from the list.

Python String maketrans(): Returned Values

The maketrans() method returns a translation table that is used by the translate() method.

Python String maketrans(): Basic Example

Below we have an example to show the working of the maketrans() function:

str1="hello"
str2="Girls"
str_var="Hello"
print(str_var.maketrans(str1, str2))

The output of the above code snippet will be as follows:


{104: 71, 101: 105, 108: 108, 111: 115}

In the example above, we have provided two strings str1 and str2, so the maketrans() method will create a transaltion table, where it will specify that when anyone uses the translate() method, then 'h' should be replaced with 'g', 'e' with 'i', 'l' with 'r' then again 'l' with 'l'(so 'l' will be replaced by 'l') and 'o' with 's'.

If we take output of the maketrans() function and use it with translate() function, then the above translation table created will be used,

str1 = "hello"
str2 = "girls"

str_var = "Hello"    #can be any string
x = str_var.maketrans(str1, str2)
# translate the string
print(str_var.translate(x))


gills

Python String maketrans() with Python Dictionary

Now we will use maketrans() method with Python Dictionary. Let us see given below code snippet:

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

The output of the above code snippet will be as follows:


{97: '219', 98: '211', 99: '229'}

From the above example, it is concluded that the dictionary dict is defined in the example. It contains the mapping of characters a,b and c to 219, 211, and 229, respectively. Thus maketrans() creates the mapping of the character's Unicode to its corresponding translation.

Time for a Live Example!

Now its time to take a look at the Live Example and it is given below:

Summary

In this tutorial, we have covered maketrans() method of string in Python which is used to map the character of a string with its replacement.



About the author:
Aspiring Software developer working as a content writer. I like computer related subjects like Computer Networks, Operating system, CAO, Database, and I am also learning Python.