In str.format
I can't get the value from a dict with the string key, both raises exception:
>>> d = {1: "one", "2": "two"}
>>> 'int {d[1]}'.format(d=d)
'int one'
>>> 'int {d[1]}, str {d["2"]}'.format(d=d)
KeyError: '"2"'
>>> 'int {d[1]}, str {d[2]}'.format(d=d)
KeyError: 2
One way looks for key '"2"'
and other way for key 2
, but how do you look for key "2"
? I mean using the format string itself, not just extracting it from outside the str.format
template.
With f-string it can access the values in dicts like normal:
>>> f'int {d[1]}, str {d["2"]}'
'int one, str two'