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
string.translate(map)

Parameters

ParameterDescription
map Required. The map containing the characters to replace with their replacements

Example

Python
src = 'Hi Marc!'
translation = src.maketrans('Marc', 'Kyra')
translated = src.translate(translation)
print(translated)

Output

Hi Kyra!