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

前端 未结 4 1931
难免孤独
难免孤独 2020-12-02 01:24

Python neophyte here. I was wondering if someone could help with the KeyError I am getting when using a dictionary for string interpolation in str.format.

4条回答
  •  Happy的楠姐
    2020-12-02 02:17

    You're right - the parser isn't that complex, and can't handle variable subscripts for dictionaries. Your work around is:

    string2 = 'Interpolating {somevar}'.format(whateverhere..., somevar=dictionary['1'])
    

提交回复
热议问题