Python String translate() Method
Given a string, translate replaces characters in that string with other characters as specified in the supplied "translation" and returns the new string. A translation is simply a map of characters, and the characters to those characters with.
One would typically use the maketrans method to create the translation map.
Syntax
Python
Copy Code
string.translate(map)
Parameters
Parameter | Description |
---|---|
map |
Required. The map containing the characters to replace with their replacements |
Example
Python
Copy Code
src = 'Hi Marc!' translation = src.maketrans('Marc', 'Kyra') translated = src.translate(translation) print(translated)
Output
Hi Kyra!