[Python] Formatting hex values with leading zeros and 0x prefix - potential bug? [duplicate]

Discussão em 'Python' iniciado por Stack, Abril 9, 2025.

  1. Stack

    Stack Membro Participativo

    Consider the following python snippet

    value = 4
    print(f"{value:02x}")
    print(f"{value:#02x}")


    What I would expect to get as an output is

    04
    0x04


    but what I actually get is

    04
    0x4


    The {value:02x} should indicate to pad the hex value to a minimum length of 2.

    The {value:#02x}, I believe should pad the hex vlaue to a minimium length of 2 but also add the 0x prefix.

    Is this a mis-understanding of the formatting language or a bug?

    Continue reading...

Compartilhe esta Página