Last Updated : 23 Jul, 2025
maketrans() method in Python is a powerful tool for creating mapping tables that specify how specific characters in a string should be replaced. This method is often used in conjunction with the translate() method to perform character replacements efficiently.
Let's understand with help of an example:
Python
# Creating a translation table
t = str.maketrans("abc", "xyz")
# Translating a string
res = "abcde".translate(t)
print(res)
Explanation:
b
with y, and c with z.Parameters
str.maketrans(x[, y[, z]])
Character mapping refers to associating one set of characters or symbols with another. Replacing vowels with corresponding uppercase letters.
Python
# Create a translation table
t = str.maketrans("aeiou", "AEIOU")
# Translating a string
res = "Learn Python with GFG".translate(t)
print(res)
LEArn PythOn wIth GFGExplanation
Removing unwanted characters can be done by using regular expression or simple string operations in Python.
Python
# Create a translation table with characters to remove
t = str.maketrans("", "", ",.!?")
# Translating a string
res = "Learn Python, with GFG!".translate(t)
print(res)
Learn Python with GFGExplanation
This is useful if we need to replace specific words represented by their initial letters.
Python
# Create a translation table using a dictionary
t = str.maketrans({"L": "D", "w": "v"})
# Translating a string
res = "Learn Python with GFG".translate(t)
print(res)
Dearn Python vith GFGExplanation
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4