Python String translate() Method (original) (raw)

Last Updated : 04 Jan, 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:

  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)

`

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

Syntax of translate() method:

str.translate(table)

**Parameters:

**Returns:

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:

**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:

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

**Explanation:

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:

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)

`

**Explanation: