Tutorials Point


  Python Basics
  Python Advanced
  Python Useful References
  Python Useful Resources
  Selected Reading

© 2011 TutorialsPoint.COM


  Home     References     Discussion Forums     About TP  

Python String maketrans() Method


previous next AddThis Social Bookmark Button


Description:

This method returns a translation table that maps each character in the intab string into the character at the same position in the outtab string. Then this table is passed to the translate() function. Note that both intab and outtab must have the same length.

Syntax:

str.maketrans(intab, outtab]);

Parameters:

Here is the detail of parameters:

  • intab: string having actual characters.

  • outtab: string having corresponding mapping character.

Return Value:

It returns a translate table to be used translate() function.

Example:

This example every vowel in a string is replaced by its vowel position:

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

This will produce following result:

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


previous next Printer Friendly



  

Advertisement

Online Image Processing

Indian Baby Names