3

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'
Norrius
  • 7,558
  • 5
  • 40
  • 49
COVFEFE-19
  • 293
  • 1
  • 8
  • 1
    `str.format` isn't using the full parser, there are things that just aren't possible. – jonrsharpe Feb 11 '21 at 20:36
  • `'int {}, str {}'.format(d[1], d["2"])` – sahasrara62 Feb 11 '21 at 20:38
  • 1
    Upvoted the question. I'm really curious how the `d["2"]` part is actually parsed and why exactly `.format` is failing this way. – Norrius Feb 11 '21 at 20:38
  • 1
    Related: https://stackoverflow.com/questions/19796123/python-string-format-calling-a-function – jonrsharpe Feb 11 '21 at 20:41
  • @Norrius: It's all in [the format string syntax docs](https://docs.python.org/3/library/string.html#grammar-token-element-index). Short version: It supports numeric indexing as a special function; despite superficially similar syntax, it's not the same as subscripting in normal Python code. – ShadowRanger Feb 11 '21 at 20:43
  • Exact dupe: [Using dictionaries and compound field format() with Integer key stored as String](https://stackoverflow.com/a/11662565/1431750) – aneroid Feb 11 '21 at 20:55
  • @aneroid OK, answer on the dupe is useless though. It only shows the obvious workaround and doesn't even explain anything. – COVFEFE-19 Feb 11 '21 at 21:05
  • @COVFEFE-19 The answers and _comments_ explain that "Integers as Strings" can't be used for dictionary keys to work with `str.format()`. And [the answer by @jonrsharpe](https://stackoverflow.com/a/66162690/1431750) provides that in more detail, including _**why**_. – aneroid Feb 11 '21 at 21:09
  • @aneroid I think I am going crazy but the answer looked completely different a moment ago – COVFEFE-19 Feb 11 '21 at 21:29
  • @COVFEFE-19 You're not going crazy :-) I updated the "accepted answer" to one jonrsharpe posted, since it included details on the _why_, apart from [my comment on the question](https://stackoverflow.com/questions/11662492/using-dictionaries-and-compound-field-format-with-integer-key-stored-as-string/11662565#comment15457015_11662492) and the other answers which did provide some explanation. Specifically, that it _can't_ be done, except via the workarounds mentioned here and there. (Usually it's worth seeing those for details about the answer.) – aneroid Feb 11 '21 at 21:53
  • OK I see now. This question got closed and answer that was briefly appeared here moved to an 8 years old question. Kind of strange how this site works, to close the new question and remove answer, onto old question with Python 2 code when f-strings didn't even exist back then. Why not just leave the new answer on the new question??? – COVFEFE-19 Feb 11 '21 at 22:18

0 Answers0