Skip to content
Related Articles
Open in App
Not now

Related Articles

Python String maketrans() Method

Improve Article
Save Article
  • Last Updated : 05 Oct, 2021
Improve Article
Save Article

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.


maketrans(str1, str2, str3)


  • 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 need to be deleted.


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


translate(table, delstr)


  • 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.

Example: 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))


The string before translating is : weeksyourweeks
The string after translating is : geeksforgeeks
My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!