maketrans()函数

leet = str.maketrans('abeiloprstz','28376280902')
ss = 'The quick brown fox jumped over the lazy dog.'
print(ss)
print(ss.translate(leet))

st = str.maketrans('abcd','1234')
strs = 'abcd'
print(strs[0].translate(st))

运行结果:

The quick brown fox jumped over the lazy dog.
Th3 qu7ck 802wn f2x jum83d 2v30 0h3 622y d2g.
1

Python 3.3中,已经没有string.maketrans()了,取而代之的是内建函数,新的有三个:bytearray.maketrans()、bytes.maketrans()、str.maketrans()

在Python2里面应该写成

import string

leet = string.maketrans('abeiloprstz','28376280902')



你可能感兴趣的:(python知识点和实例)