Custom table for str.translate in Python 3
问题 If I run this code: s.translate(str.maketrans({'as': 'dfg', '1234': 'qw'})) I will get: ValueError: string keys in translate table must be of length 1 Is there a way to replace multiple characters at once using str.translate ? Docs says I can use codecs for flexible approach, but I can't find out how. If no, what can be done instead then? 回答1: No . str.translate can be used solely to replace single characters. The replacement strings can be of any length, but the keys must be a single