Python String translate() Method
Last Updated :
16 Jun, 2023
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
translation = { 103 : None , 101 : None , 101 : None }
string = "geeks"
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
table = { 119 : 103 , 121 : 102 , 117 : None }
trg = "weeksyourweeks"
print ( "The string before translating is : " , end = "")
print (trg)
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
table = str .maketrans( 'aeiou' , '12345' )
text = 'this is a test'
translated_text = text.translate(table)
print (translated_text)
|
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
firstString = "gef"
secondString = "eks"
thirdString = "ge"
string = "geeks"
print ( "Original string:" , string)
translation = string.maketrans(firstString,
secondString,
thirdString)
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!
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
Please Login to comment...