Open In App

maketrans() and translate() functions in Python

Last Updated : 15 Oct, 2020
Improve
Improve
Like Article
Like
Save
Share
Report

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 functions are discussed in this article.

maketrans()

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()

Translate using maketrans()

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 demonstrate 
# translations using 
# maketrans() and translate()
  
# specify to translate chars
str1 = "wy"
  
# specify 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

 

Translate without maketrans()

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 for maketrans() to perform translations.

 
Code #2 : Code to translate without maketrans().




# Python3 code to demonstrate 
# 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 an easy and quick way to replace and rectify them and would potentially save a lot of time.



Similar Reads

Python String Methods | Set 3 (strip, lstrip, rstrip, min, max, maketrans, translate, replace & expandtabs())
Some of the string methods are covered in the below sets.String Methods Part- 1 String Methods Part- 2More methods are discussed in this article1. strip():- This method is used to delete all the leading and trailing characters mentioned in its argument.2. lstrip():- This method is used to delete all the leading characters mentioned in its argument.
4 min read
Python String maketrans() Method
Python String 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 li
2 min read
Python | Pandas Series.str.translate()
Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric Python packages. Pandas is one of those packages and makes importing and analyzing data much easier. Pandas str.translate() is one of most important and complex string method. It uses a translate table to translate the caller series of s
2 min read
Python String translate() Method
Python String translate() returns a string that is a modified string of givens string according to given translation mappings. What is translate() in Python?translate() is a built-in method in Python that is used to replace specific characters in a string with other characters or remove them altogether. The translate() method requires a translation
4 min read
Python - Sympy Curve.translate() method
In Sympy, the function Curve.translate() is used translate the given curve by the given values of x, y. It translates the curve along with both the directions i.e. along x-axis and y-axis. Syntax: Curve.translate(x, y) Parameters: x: translation value along x-axis y: translation value along y-axis Returns: Translated Curve Example #1: # import Curv
1 min read
Build an Application to translate English to Hindi in Python
In these articles, We will write python scripts to translate English word to Hindi word and bind it with the GUI application. We are using the English-to-Hindi module to translate the English word into the Hindi word. Installation: Run this code into your terminal: pip install englisttohindi Approach: Import English to Hindi modules.Create an objec
2 min read
Python String translate() method
Python String translate() method returns a modified string replacing the characters described in a dictionary, or in a hash table. Syntax: string.translate(hash map) Parameters string- Original String hash map- mapping between two characters in the original string.Python String translate() method ExamplesExample 1: C/C++ Code # hash map trans_dic =
3 min read
numpy string operations | translate() function
numpy.core.defchararray.translate(arr, table, deletechars=None) is another function for doing string operations in numpy. For each element in arr, it returns a copy of the string where all characters occurring in the optional argument deletechars are removed, and the remaining characters have been mapped through the given translation table. If ther
1 min read
Mathematical Functions in Python | Set 2 (Logarithmic and Power Functions)
Numeric functions are discussed in set 1 below Mathematical Functions in Python | Set 1 ( Numeric Functions) Logarithmic and power functions are discussed in this set. 1. exp(a) :- This function returns the value of e raised to the power a (e**a) . 2. log(a, b) :- This function returns the logarithmic value of a with base b. If base is not mentione
3 min read
Mathematical Functions in Python | Set 3 (Trigonometric and Angular Functions)
Some of the mathematical functions are discussed in below set 1 and set 2 Mathematical Functions in Python | Set 1 (Numeric Functions) Mathematical Functions in Python | Set 2 (Logarithmic and Power Functions) Trigonometric and angular functions are discussed in this article. 1. sin() :- This function returns the sine of value passed as argument. T
3 min read
Article Tags :
Practice Tags :