Python String maketrans() Method

Returns a translation table to be used in translations. Note that this doesn't do the translation, but rather creates the map for use in the translate function.

Syntax

Python
string.maketrans(x)
string.maketrans(x, y, z = None)

Parameters

ParameterDescription
x Required. The map containing the characters to replace with their replacements, or if there are two or more parameters, the set of characters to be replaced.
y Optional. The corresponding set of characters to replace the characters specified in x. This value is required if z is supplied.
z Optional. The set of characters to remove from the string. This can only be used when there are three parameters provided in the call to maketrans. Default is None

Example

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

Then, to use this translation map, do:

Python
translated = src.translate(translation)
print(translated)

Output

{77: 75, 97: 121, 114: 114, 99: 97}
Hi Kyra!