Open In App
Related Articles

Python String translate() Method

Like Article
Save Article
Report issue

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



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


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



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


# 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) 


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


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.


# 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,
# Translated String
print("Translated string:",


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.


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



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.


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"



Hello world This is a test

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