The Python translate() method returns a copy of the string in which a specified translation table is used to map each character. We may use the maketrans() method to construct a chart of translation in various formats from character to character mappings.
The syntax of the method is: str.translate(table[, deletechars]);
We’ll be covering the following topics in this tutorial:
Return value from String translate()
• The translate() method returns a string in which each character is mapped to the corresponding table character.
Below is the python program to demonstrate the translate() function:
intab = "aeiou" outtab = "*#@!~" trantab = str.maketrans(intab, outtab) str = "The Best Learning Resource for Online Education"; print (str.translate(trantab))
When we run above the program, the outcome is as follows:
Th# B#st L#*rn@ng R#s!~rc# f!r Onl@n# Ed~c*t@!n
Python Remove Character from String using translate() Method
str = 'mama786papa' print(str.translate({ord('a'): None}))
When we run above the program, the outcome is as follows:
mm786pp
Below are several other functions that we can use to work with string in Python 3