Python | maketrans() and translate() functions
In the world of programming, seldom there is a need to replace all the words/characters at once in the whole file python offers this functionality using functions translate() and its helper functions maketrans(). Both the functions are discussed in this article.
maketrans() function is used to construct the transition table i.e specify the list of characters that need to be replaced in the whole string or the characters that need to be deleted from the string
Syntax : maketrans(str1, str2, str3)
Parameters :
str1 : Specifies the list of characters that need to be replaced.
str2 : Specifies the list of characters with which the characters need to be replaced.
str3 : Specifies the list of characters that needs to be deleted.Returns : Returns the translation table which specifies the conversions that can be used by translate()
To translate the characters in the string translate() is used to make the translations. This function uses the translation mapping specified using the maketrans().
Syntax : translate(table, delstr)
Parameters :
table : Translate mapping specified to perform translations.
delstr : The delete string can be specified as optional argument is not mentioned in table.Returns : Returns the argument string after performing the translations using the translation table.
Code #1 : Code to translate using translate() and maketrans().
# Python3 code to demostrate # translations using # maketrans() and translate() # specify to translate chars str1 = "wy" # specify to to replace with str2 = "gf" # delete chars str3 = "u" # target string trg = "weeksyourweeks" # using maketrans() to # construct translate # table table = trg.maketrans(str1, str2, str3) # Printing original string print ("The string before translating is : ", end ="") print (trg) # using translate() to make translations. print ("The string after translating is : ", end ="") print (trg.translate(table)) |
Output :
The string before translating is : weeksyourweeks The string after translating is : geeksforgeeks
Translation can also be achieved by specifying the translation dictionary and passing as an object which acts as a mapping. In this case, there is no need of maketrans() to perform translations.
Code #2 : Code to translate without maketrans().
# Python3 code to demostrate # translations without # maketrans() # specifying the mapping # using ASCII table = { 119 : 103, 121 : 102, 117 : None } # target string trg = "weeksyourweeks" # Printing original string print ("The string before translating is : ", end ="") print (trg) # using translate() to make translations. print ("The string after translating is : ", end ="") print (trg.translate(table)) |
Output :
The string before translating is : weeksyourweeks The string after translating is : geeksforgeeks
Application :
There are many are times where mistakes can occur while coding or developing, these functions provide easy and quick way to replace and rectify them and would potentially save a lot of time.
Recommended Posts:
- Python String Methods | Set 3 (strip, lstrip, rstrip, min, max, maketrans, translate, replace & expandtabs())
- Python | String translate()
- Python | Pandas Series.str.translate()
- Mathematical Functions in Python | Set 3 (Trigonometric and Angular Functions)
- Mathematical Functions in Python | Set 4 (Special Functions and Constants)
- Mathematical Functions in Python | Set 2 (Logarithmic and Power Functions)
- Mathematical Functions in Python | Set 1 (Numeric Functions)
- Log functions in Python
- Functions in Python
- Decimal Functions in Python | Set 1
- Operator Functions in Python | Set 1
- Iterator Functions in Python | Set 1
- First Class functions in Python
- Operator Functions in Python | Set 2
- Partial Functions in Python
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



