Is it possible to write nested format strings?

I want to write something like

format_specifier = "{some_dict[some_key]}"

to later use it together with .format():

result = format_specifier.format(some_dict={"one": "eins", "two": "zwei"}, some_key="one")

which I wanted to evaluate to "eins", but instead I get a KeyError:

KeyError: 'some_key'

I also tried extra braces: "{some_dict[{some_key}]}" resulting in KeyError: '{some_key}'..

Is nesting format strings like this possible in the first place?

Note: I have to evaluate the string later so I guess f-string formatting is not an option for me..



Read more here: https://stackoverflow.com/questions/64858501/is-it-possible-to-write-nested-format-strings

Content Attribution

This content was originally published by frans at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: