Att String Translate
# Python2.x Python translate() Method
[ Python Strings](#)
* * *
## Description
The Python translate() method translates the characters in a string according to a table given by the parameter `table` (which contains 256 characters). Characters to be filtered out are placed in the `del` parameter.
## Syntax
The syntax for the translate() method is:
str.translate(table[, deletechars]);
## Parameters
* table -- The translation table, which is created using the maketrans method.
* deletechars -- A list of characters to be filtered out from the string.
## Return Value
Returns the translated string.
## Example
The following examples demonstrate the usage of the translate() function:
## Example (Python 2.0+)
#!/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);
The output of the above example is:
th3s 3s str3ng 2x1mpl2....w4w!!!
The following example removes the characters 'x' and 'm' from the string:
## Example (Python 2.0+)
#!/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');
The output of the above example is:
th3s 3s str3ng 21pl2....w4w!!!
* * Python Strings](#)
YouTip