当前位置:  首页>> 技术小册>> Python合辑3-字符串用法深度总结

str.maketrans(dict_map)

从字典映射创建一个翻译表,str.translate(maketrans) 用它们的新值替换翻译中的元素。例如:

  1. example = "abcde"
  2. mapped = {'a':'1', 'b':'2', 'c':'3', 'd':'4', 'e':'5'}
  3. print(example.translate(example.maketrans(mapped)))

Output:

  1. 12345

该分类下的相关小册推荐: