Last Updated : 11 Jul, 2025
Python translate() method is used to transform a string by replacing or removing specific characters based on a translation table. This table is created using the str.maketrans() method. It’s ideal for scenarios where we need to:
Example:
In the given example, we are using str.translate() method is used to translate a string by specifying a mapping using ASCII values.
Python
# Creating a translation table
tab = str.maketrans("aeiou", "12345")
# Original string
s = "hello world"
# Translating the string
res = s.translate(tab)
print(res)
Let's explore python string translate() method in detail:
Syntax of translate() method:str.translate(table)
Parameters:
Returns:
The maketrans() method is a built-in string method in Python that generates a translation table. It is primarily used in conjunction with the translate() method to perform character-by-character translations or deletions.
Python maketrans() SyntaxExample 1: Removing Specific Characters with TranslateSyntax: 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 need to be deleted.
Returns: Returns the translation table which specifies the conversions that can be used by translate()
We can use translate() to remove unwanted characters, like punctuation. Removing punctuation from a string is a common task, especially in text preprocessing for Natural Language Processing (NLP) or data cleaning.
Example:
Python
# Remove punctuation
import string
s = "Hello, World! Welcome to Python."
tab = str.maketrans("", "", string.punctuation)
res = s.translate(tab)
print(res)
Hello World Welcome to Python
Explanation:
Replace multiple characters in a string using a custom mapping. In this example, we demonstrate how to map specific characters to others for custom transformations.
Example:
Python
# Replace spaces with underscores and vowels with '*'
tab = str.maketrans({" ": "_", "a": "*", "e": "*", "i": "*", "o": "*", "u": "*"})
s = "This is an example."
res = s.translate(tab)
print(res)
Th*s_*s_*n_*x*mpl*.
Explanation:
Let’s build a basic substitution cipher using the translate() method. This is often used in simple encryption or encoding scenarios.
Example:
Python
# Simple substitution cipher
tab1 = str.maketrans("abcdef", "uvwxyz")
s = "abcde fg"
encoded = s.translate(tab1)
print("Encoded:", encoded)
# Decoding the text
tab2 = str.maketrans("uvwxyz", "abcdef")
decoded = encoded.translate(tab2)
print("Decoded:", decoded)
Encoded: uvwxy zg Decoded: abcde fg
Explanation:
Let’s combine character removal and replacement for a more complex text-processing task.
Example:
Python
import string # Import the string module
s = "Python3.9 is awesome!"
# Create a translation table
table = str.maketrans({"P": "p", "y": "Y", "o": "0"}) # Only use a dictionary here
# Remove digits using translate's deletechars parameter
res = s.translate(table).translate(str.maketrans("", "", string.digits))
print(res)
Explanation:
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