[Python] How to use str.format with nested dict

Discussão em 'Python' iniciado por Stack, Junho 30, 2025.

  1. Stack

    Stack Membro Participativo

    I have a nested dict:

    d = {
    'A': {'X': 0, 'Y': 1, 'Z': 2},
    'B': {'X': 0, 'Y': 1, 'Z': 2},
    'C': {'X': 0, 'Y': 1, 'Z': 2}
    }


    I'd now like to embed its values in a string using format, something like

    "d['A']['X']={A,X:d}".format(**d)


    to output:

    "d['A']['X']=0"


    How to do this correctly?

    Continue reading...

Compartilhe esta Página