The translate method of a string replaces one character by a string according to the translation that you provide.
Here are a few cases:
Original string: as 1234
Error in [s.translate(str.maketrans({'as': 'dfg', '1234': 'qw'}))]
Error in [s = s.translate(str.maketrans({'a': 'd', '12': 'q'}))]
s.translate(str.maketrans({'a': 'd', '1': 'q'})): ds q234
Workaround to get the result
After the edit of the question, here is a solution to get the desired replacements:
Split by the keys, and then join by the values in your translation dictionary.
Replaced all subsrings: dfg qw
The code:
s = 'as 1234'
print('Original string:',s)
try:
w = s.translate(str.maketrans({'as': 'dfg', '1234': 'qw'}))
print("s.translate(str.maketrans({'as': 'dfg', '1234': 'qw'}):", w)
except:
print("Error in [s.translate(str.maketrans({'as': 'dfg', '1234': 'qw'}))]")
try:
w = s.translate(str.maketrans({'a': 'd', '12': 'q'}))
print("s.translate(str.maketrans({'a': 'd', '12': 'q'})):", w)
except:
print("Error in [s = s.translate(str.maketrans({'a': 'd', '12': 'q'}))]")
try:
w = s.translate(str.maketrans({'a': 'd', '1': 'q'}))
print("s.translate(str.maketrans({'a': 'd', '1': 'q'})):", w)
except:
print("Error in [s = s.translate(str.maketrans({'a': 'd', '1': 'q'}))]")
trans_dict = {'as': 'dfg', '1234': 'qw'}
for k,v in trans_dict.items():
y = s.split(k)
s = v.join(y)
print('Replaced all subsrings:',s)