Open In App

Python String translate() Method

Last Updated : 11 Jul, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

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:

  1. Replace specific characters with others.
  2. Remove unwanted characters like punctuation, digits or whitespace.
  3. Perform transformations without looping through the string manually.

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)

Output
h2ll4 w4rld

Let's explore python string translate() method in detail:

Syntax of translate() method:

str.translate(table)

Parameters:

  • table: A translation table created using the str.maketrans() method.

Returns:

  • A new string where the specified characters are replaced or removed according to the translation table.

Python maketrans()

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

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

Returns: Returns the translation table which specifies the conversions that can be used by translate()

Example 1: Removing Specific Characters with 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)

Output
Hello World Welcome to Python

Explanation:

  • Translation Table: The str.maketrans("", "", string.punctuation) specifies no replacements but deletes all characters listed in string.punctuation. This includes characters like !, ., ,, and others.

Example 2: Replacing Multiple Characters

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)

Output
Th*s_*s_*n_*x*mpl*.

Explanation:

  • Replacement Rules: Replace spaces (' ') with underscores ('_'). Replace vowels ('a', 'e', 'i', 'o', 'u') with the '*' character.

Example 3: Building a Simple Cipher

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)

Output
Encoded: uvwxy zg
Decoded: abcde fg

Explanation:

  • Encoding: The str.maketrans("abcdef", "uvwxyz") creates a mapping where: 'a' → 'u', 'b' → 'v', 'c' → 'w', etc. Applying translate() transforms the string into its encoded form.
  • Decoding: A reverse mapping is created with str.maketrans("uvwxyz", "abcdef") to decode the string back to its original form.

Example 4: Combining Removal and Replacement

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)

Output
pYthn. is awesme!

Explanation:

  • Replacement: 'P' → 'p' (uppercase to lowercase transformation). 'y' → 'Y' (lowercase to uppercase transformation). 'o' → '0' (replace the letter 'o' with the number zero).
  • Removal: string.digits ensures all numeric characters ("0123456789") are removed.

Similar Reads

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy