先写一个table
t=str.maketrans(string.ascii_letters,string.ascii_letters,’ ')
c=‘asfd c asd sd’
c.translate(t)
‘asfdcasdsd’
t=str.maketrans(string.ascii_letters,string.ascii_letters,’ s’)
c=‘asfd c asd sd’
c.translate(t)
afdcadd
maketrans(A,B ,C)中把A参数中的字符分别一一对应变为B中的字符,生成字符转换表table,但在translate调用时会先按照table中C参数(原delete参数)中的字符删除掉同样的字符,然后才会按照A->B对照转换字符.
t = string.maketrans(“abc”, “123”)
s = “ccab”
print s.translate(t)
3312