Chinaunix首页 | 论坛 | 博客
  • 博客访问: 109133
  • 博文数量: 49
  • 博客积分: 2612
  • 博客等级: 少校
  • 技术积分: 431
  • 用 户 组: 普通用户
  • 注册时间: 2009-12-01 14:31
个人简介

来来去去

文章分类

全部博文(49)

文章存档

2015年(1)

2012年(4)

2011年(1)

2010年(42)

2009年(1)

我的朋友

分类: Python/Ruby

2010-01-18 19:00:20

Python String maketrans() Method



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!!!
阅读(402) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~