maketrans() and translate() functions in Python
Last Updated :
15 Oct, 2020
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
# 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
# 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
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 m
1 min read
atan2() function in Python atan2(y, x) returns value of atan(y/x) in radians. The atan2() method returns a numeric value between -\pi and \pi representing the angle \theta of a (x, y) point and positive x-axis. Syntax : atan2(y, x) Parameter : (y, x) - Both must be a numeric value. Returns : Returns atan(y / x), in radians. T
2 min read
How to Call Multiple Functions in Python In Python, calling multiple functions is a common practice, especially when building modular, organized and maintainable code. In this article, weâll explore various ways we can call multiple functions in Python.The most straightforward way to call multiple functions is by executing them one after a
3 min read
numpy.typename() function â Python numpy.typename() function return a description for the given data type code. Syntax : numpy.typename(char) Parameters : char : [str] Data type code. Return : [str] Description of the input data type code. Code #1 : Python3 # Python program explaining # numpy.typename() function # importing numpy as
2 min read
Python Pillow - Flip and Rotate Images Prerequisites: Pillow Python Pillow or PIL is the Python library that provides image editing and manipulating features. The Image Module in it provides a number of functions to flip and rotate images. image.transpose() is the function used to rotate and flip images with necessary keywords as paramet
2 min read