maketrans() and translate() functions in Python

This tutorial aims to teach you maketrans() and translate() functions in Python. The maketrans() method returns a mapping table also called a transition table which can be used by translate() method to translate the specified characters in a string. With the help of these functions, you will be able to make transformations on a string and be able to replace old characters with new ones and/or remove some old characters.

How to do String Transformation using maketrans() and translate() functions in Python

To replace some characters with the specified New characters and remove some specified characters.

We will use the following variables :

  1. stringOld (the old characters you wish to replace in the given string)
  2. stringNew (the new characters you want to replace the old characters within the given string)
  3. stringRemove (the characters you wish to remove from the given string)
  4. str (the given string)

maketrans() in Python

maketrans() method takes three parameters, stringOld, stringNew, and stringRemove to construct a transition table that contains the list of characters that are to be replaced in the given string and the characters that have to be removed from the given string.

translate() in Python

The transition table returned by the maketrans() method can be used by the translate() method to do the required changes in the given string or in other words it translates the characters in the given string. This method takes two parameters, a transition table, and an optional delete string parameter.

Example showing what we wish to achieve with the help of maketrans() and translate() functions

Let’s see how these functions in Python work with the help of an example.

Given string is “Cearning zisz Bun

The characters we wish to replace are “C” and “B“:

stringOld = “CB

We wish to replace these characters with the characters “L” and “F“:

stringNew= “LF

We wish to remove the character “z

stringRemove = “z

Let’s form the Transition Table with help of maketrans() method

Transition_Table = str.maketrans(stringOld, stringNew, stringRemove)

This will create the following Transition table:

{67: 76, 66: 70, 122: None}

Here,

67 is the ASCII value of “C”   :   74 is the ASCII value of “L”

66 is the ASCII value of “B”   :   70 is the ASCII value of “F”

122 is the ASCII vlaue of “z”  :    ‘None’ means that this character will be removed.

The translate() method will make the changes according to the Transition Table.

So The Translated String will become: “Learning is Fun

Python Program for demonstrating the use of maketrans() and translate() function in Python

str = "Cearning zisz Bun"
print ("Original String: ", end ="")
print (str)

stringOld = "CB"

stringNew= "LF"

stringRemove = "z"

# using maketrans() to make the transition table.
Transition_Table = str.maketrans(stringOld, stringNew, stringRemove)

print ("Transition_Table: ")
print(Transition_Table)

# using translate() to make translations.
print ("Translated String: ", end ="")
print (str.translate(Transition_Table))

Output:

maketrans() and translate() functions in Python

Also Read: Detection of COVID-19 From Chest X-Ray Images Using Machine Learning

Leave a Reply

Your email address will not be published. Required fields are marked *