Home >>Python String Methods >Python String maketrans() Method

Python String maketrans() Method

Python String maketrans() Method

Python string maketrans() method is used to make a translation table. This table defines the list of the characters that need to be replaced in the whole string or the characters that need to be deleted from the string. This table is passed to the translate() function.

Syntax:

maketrans(str1, str2, str3)

Parameter Values

Parameter Description
str1: This parameter defines the list of characters that need to be replaced.
str2: This parameter defines the list of characters with which the characters need to be replaced.
str3: This parameter defines the list of characters that needs to be deleted.

Here is an example of Python maketrans() method:


str1 = "xr"
str2 = "PT"
str3 = "a"
trg = "xHxraxOINr"
table = trg.maketrans(str1, str2, str3) 
print ("The string before translating is : ", end ="") 
print (trg) 
print ("The string after translating is : ", end ="") 
print (trg.translate(table))

Output:
The string before translating is : xHxraxOINr
The string after translating is : PHPTPOINT

Example 2:


str1 = "rcd"
str2 = "inh"
trg = "Abdrmacyu"
table = trg.maketrans(str1, str2) 
print ("The string before translating is : ", end ="") 
print (trg) 
print ("The string after translating is : ", end ="") 
print (trg.translate(table))

Output:
The string before translating is : Abdrmacyu
The string after translating is : Abhimanyu