Open In App
Related Articles

Python String translate() Method

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

Python String translate() returns a string that is a modified string of givens string according to given translation mappings. 

What is translate() in Python?

translate() is a built-in method in Python that is used to replace specific characters in a string with other characters or remove them altogether. The translate() method requires a translation table that maps the characters to be replaced to their replacements. This table can be generated using the maketrans() method, which takes two arguments: the characters to be replaced and their replacements.

Python String translate() Syntax

str.translate(translation_table)

Example:

In the given example, we are using str.translate() method is used to translate a string by specifying a mapping using ASCII values.

Python3

# specifying the mapping using ASCII 
translation = {103: None, 101: None, 101: None}
 
string = "geeks"
 
# translate string
print("Translated string:",
       string.translate(translation))

                    

Output: 

Translated string: ks

Python String translate() Examples 

There are two ways to translate are:

  • Python String translate() with mapping as a dictionary.
  • Mapping using maketrans().

Translation/Mapping with translate() with manual translation table

In this example, we are going to see how to use Python string translate() methods with a dictionary.

Python3

# Python3 code to demonstrate
# translations without
# maketrans()
 
# specifying the mapping
# using ASCII
table = { 119 : 103, 121 : 102, 117 : None }
 
# target string
trg = "weeksyourweeks"
 
# 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))

                    

Output :

The string before translating is : weeksyourweeks
The string after translating is : geeksforgeeks

Translation/Mapping using a translation table with translate()

In the given example, the vowels are replaced with digits with the use of maketrans() and translate() in Python.

Python3

# Define the translation table
table = str.maketrans('aeiou', '12345')
 
# Apply the translation to a string
text = 'this is a test'
translated_text = text.translate(table)
 
print(translated_text)  # 'th3s 3s 1 t2st'

                    

Output :

th3s 3s 1 t2st

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:

In this code, we are demonstrating how to use str.translate() with a translation table created using string.maketrans() to modify a string by replacing and deleting specific characters.

Python3

# Python 3 Program to show working
# of translate() method
 
# First String
firstString = "gef"
 
# Second String
secondString = "eks"
 
# Third String
thirdString = "ge"
 
# Original String
string = "geeks"
print("Original string:", string)
 
translation = string.maketrans(firstString,
                               secondString,
                               thirdString)
 
# Translated String
print("Translated string:",
       string.translate(translation))

                    

Output : 

Original string: geeks
Translated string: ks

Use case of translate() and maketrans() Method

Remove Vowels from a String

In the given example, we are using the Maketrans() and translate()  to remove the vowels in the string in Python.

Python3

text = "Hello, world!"
vowels = "aeiouAEIOU"
table = str.maketrans("", "", vowels)
translated_text = text.translate(table)
 
print(translated_text) # Output: "Hll, wrld!"

                    

Output:

Hll, wrld!

Remove Punctuation from a String

In the given example, we are using the Maketrans() and translate()  to remove the punctuation mark in the string in Python.

Python3

import string
 
text = "Hello, world! This is a test."
table = str.maketrans("", "", string.punctuation)
translated_text = text.translate(table)
 
print(translated_text) # Output: "Hello world This is a test"

                    

Output:

Hello world This is a test


Last Updated : 16 Jun, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads