制作映射表并替换字符串中内容
- 映射表
功能:制作用于字符串替换的映射表
格式:字符串名.maketrans(‘查找字符’,‘替换字符’)
返回:字典(一一对应的ASCII码值)
b = str.maketrans('abcdefg','ABCDEFG')
print(b)
# >>>{97: 65, 98: 66, 99: 67, 100: 68, 101: 69, 102: 70, 103: 71}
- 字符串替换
功能:按照映射表替换字符串
格式:字符串名:translate(映射表)
返回:替换完成的字符串
b = str.maketrans('abcdefg','ABCDEFG')
str = 'i am a little boy , but oneday became the man .'
c = str.translate(b)
print(c)
# >>>i Am A littlE Boy , But onEDAy BECAmE thE mAn .