String formatting [str.format()] with a dictionary key which is a str() of a number

No. According to the documentation:

Because arg_name is not quote-delimited, it is not possible to specify arbitrary dictionary keys (e.g., the strings ’10’ or ‘:-]’) within a format string.

So you can’t use strings consisting of numbers as dictionary keys in format strings.

Note that your key isn’t numeric, and it’s not trying to use it as a list index. Your key is a string that happens to contain a digit character. What it’s trying to do is use the number 1 (not the string “1”) as a dictionary key. It will work if you use the number 1 as your dictionary key (i.e., make your dict {'key1': 'val1', 1: 'val2'}).

Leave a Comment