You are here : python_2stringstringtranslate

string.translate() - string


The method translate() returns a copy of the string in which all characters have been translated using table (constructed with the maketrans() function in the string module), optionally deleting all characters found in the string deletechars.

  • table -- You can use the maketrans() helper function in the string module to create a translation table.

  • deletechars -- The list of characters to be removed from the source string.


str.translate(table[, deletechars]);



from string import maketrans   # Required to call maketrans function.

intab = "aeiou"
outtab = "12345"
trantab = maketrans(intab, outtab)

str = "this is string!!!";
print str.translate(trantab)

Output / Return Value

This will produce following result −

th3s 3s str3ng 2x1mpl2....w4w!!!


Alternatives / See Also