先按照转换表的替换出新的 string,然后再执行 del 参数的替换,有个先后顺序:
# -*- coding:utf-8 -*-
from string import maketrans
intab = 'aeiou'
outtab = '12345'
str1 = 'i am a example string for test! wow...!!!'
print "str1:",str1
print "intab:",intab
print "outtab:",outtab
transtab = maketrans(intab,outtab)
print "str1.translate(translate(intab,outtab)):"
print str1.translate(transtab)
print "str1.translate(translate(intab,outtab),'x1'):"
print str1.translate(transtab,'x1')
输出结果为:
str1: i am a example string for test! wow...!!!
intab: aeiou
outtab: 12345
str1.translate(translate(intab,outtab)):
3 1m 1 2x1mpl2 str3ng f4r t2st! w4w...!!!
str1.translate(translate(intab,outtab),'x1'):
3 1m 1 21mpl2 str3ng f4r t2st! w4w...!!!
小兔爱囧囧
小兔爱囧囧
dia***dtu@163.com2年前 (2018-05-22)