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
Python String translate() Method
Python String translate() returns a string that is a modified string of givens string according to given translation mappings.