小册名称:Python合辑3-字符串用法深度总结
str.maketrans(dict_map)
从字典映射创建一个翻译表,str.translate(maketrans) 用它们的新值替换翻译中的元素。例如:
example = "abcde"mapped = {'a':'1', 'b':'2', 'c':'3', 'd':'4', 'e':'5'}print(example.translate(example.maketrans(mapped)))
example = "abcde"
mapped = {'a':'1', 'b':'2', 'c':'3', 'd':'4', 'e':'5'}
print(example.translate(example.maketrans(mapped)))
Output:
12345