Python String maketrans() Method

The maketrans() method returns a mapping table that maps each character in the given string to the character in the second string at the same position. This mapping table is used with the translate() method, which will replace characters as per the mapping table.

Syntax:

str.maketrans(x, y, z)

Parameters:

  1. x: This can be either dictionary or string. If only one parameter is supplied, this parameter must be a dictionary that contains a mapping table in terms of key and value. A key can be a single character or a Unicode number (e.g. 97 for 'a') and a value can be anything that needs to be replaced with the character specified as key. If two or more parameters are specified, this parameter has to be a string specifying the characters you want to replace.
  2. y: Optional. The length of this parameter should be the same as parameter x. Each character in the first string is a replacement to its corresponding index in the second string.
  3. z: Optional. The string parameter specifies the characters to remove in the string.

Return Value:

Returns a dictionary containing mappings of unicode characters to its replacement.

The following example creates a dictionary containing a mapping table of Unicode characters.

Example: Create Mapping Table using maketrans()
mystr = 'Most'
mappingtbl = mystr.maketrans('M','H')
print(mappingtbl)
Output
{77: 72}

In the above example, the maketrans() method returns a dictionary of Unicode character where a key is a unicode character of M, and a value is unicode character of H. This is the simple mapping table containing only one mapping for a single character. Now, this mapping table is passed in the translate() method, which will replace all characters as per the mapping table, as shown below.

Example: Create Mapping Table using maketrans() and pass to translate()
mystr = 'Most'
mappingtbl = mystr.maketrans('M','H')
newstr = mystr.translate(mappingtbl) # pass mapping table in translate()
print(newstr)
Output
Host

Thus, it simply replaces 'M' with 'H' and returns a new string. The following replaces multiple characters specified as a string.

Example: maketrans() and translate()
mystr = 'Most'
mappingtbl = mystr.maketrans('Mts','Hdl')
print('Mapping table: ', mappingtbl)

newstr = mystr.translate(mappingtbl)
print('Translation: ', newstr)
Output
Mapping table: {77: 72, 116: 100, 115: 108}
New string: Hold

In the above example, the mapping table contains the replacement of each character in a string 'Mts'. The translate() method replace 'M', 't', 's' character with 'H', 'd','l' respectively.

Note that the parameter x and y must have equal length, otherwise it will throw an error.

Example: maketrans()
mystr = 'Most'
mappingtbl = mystr.maketrans('Mts','Hd')
Output
ValueError: the first two maketrans arguments must have equal length

The third parameter in the maketrans() method map to None, which means it will be removed if found, as shown below.

Example: Remove Chars
mystr = 'Most'
mappingtbl = mystr.maketrans('M','H' ,'s')
print('Mapping table: ', mappingtbl)

newstr = mystr.translate(mappingtbl)
print('Translation: ', newstr)
Output
Mapping table: {77: 72, 115: None}
Translation: Hot

The same thing can be achieved by passing a dictionary to the maketrans() method to form a mapping table and use it in the translate method, as shown below.

Example: Passing dict to maketrans()
mystr = 'Most'
mapdict = {'M': 72, 's':None}
mappingtbl = mystr.maketrans(mapdict)
print('Mapping table: ', mappingtbl)

newstr = mystr.translate(mappingtbl)
print('Translation: ', newstr)
Output
Mapping table:  {77: 72, 115: None}
Translation: Hot

Instead of specifying unicode character as a value, you can specify an actual character in the dictionary, as shown below.

Example: Passing Chars
mystr = 'Most'
mapdict = {'M': 'H', 's': 'l', 't':'d'}
mappingtbl = mystr.maketrans(mapdict)
print('Mapping table: ', mappingtbl)

newstr = mystr.translate(mappingtbl)
print('Translation: ', newstr)
Output
Mapping table:  {77: 'H', 115: 'l', 116: 'd'}
Translation: Hold