以下实例展示了 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.runoob.com/python/att-string-translate.html
时间: 2024-10-31 21:00:30