Latest web development tutorials

Python3 translate () method

Python3 string Python3 string


description

translate () method parameter table given in the table (256 characters) character string conversion, to filter out the character into the del parameter.

grammar

translate () method syntax:

str.translate(table[, deletechars]);

parameter

  • table - translation table is converted from the translation table by maketrans methods.
  • deletechars - the list of characters in the string you want to filter.

return value

Returns a string translated.

Examples

The following example shows translate () function to use:

#!/usr/bin/python3

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

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

Examples of the above output results are as follows:

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

Examples of the above to remove the string 'x' and 'm' characters:

#!/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, 'xm');

Examples of the above output:

th3s 3s str3ng 21pl2....w4w!!!

Python3 string Python3 string