以下实例展示了 translate()函数的使用方法:
#!/usr/bin/pythonfrom string import maketrans # 引用 maketrans 函数。 intab = "aeiou" outtab = "12345" trantab = maketrans(intab, outtab) str = "this is string example....wow!!!";print str.translate(trantab);
以上实例输出结果如下:
th3s 3s str3ng 2x1mpl2....w4w!!!
以上实例去除字符串中的 ‘x‘ 和 ‘m‘ 字符:
#!/usr/bin/pythonfrom 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‘);
以上实例输出结果:
th3s 3s str3ng 21pl2....w4w!!!
原文:http://www.cnblogs.com/iathena/p/02a269e22742471552accb1723a723e1.html