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.


Syntax

str.translate(table[, deletechars]);


Example

#!/usr/bin/python

from string import maketrans   # Required to call maketrans function.

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

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


Output / Return Value

This will produce following result −


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


Limitations


Alternatives / See Also


Reference