Why does translate replace symbols with word codes
In this example, I get numeric symbol codes in the translation table, not the symbols themselves. Why? And how can I make a translation table with symbols based on variables?
str_1 = 'abcd'
str_2 = 'dcba'
tbl_trans = str.maketrans(str_1, str_2)
print(tbl_trans)
Why?
Why not? Because it's been designed that way... a simple search will tell you that str.maketrans is meant to be used in conjunction with str.translate ...
And how can I make a translation table with symbols based on variables?
By mapping the keys and values back to characters:
tbl_trans_chr = {chr(k):chr(v) for k,v in tbl_trans.items()}
or directly:
tbl_trans_chr = {k:v for k,v in zip(str_1, str_2)}
Output:
{'a': 'd', 'b': 'c', 'c': 'b', 'd': 'a'}