[Python] How to get a space for a negative number in format() function

Discussão em 'Python' iniciado por Stack, Julho 7, 2025.

  1. Stack

    Stack Membro Participativo

    Use " " (a space) to insert a space before positive numbers and a minus sign before negative numbers:

    txt = "The temperature is between{: } and{: } degrees celsius."
    print(txt.format(-3, 7))


    Output:

    The temperature is between-3 and 7 degrees celsius.


    How do I get the space inserted before the negative number -3 with the minus sign, if the txt is written the way I wrote it? Since the space is to insert a space before positive numbers and a minus sign before negative numbers.

    Continue reading...

Compartilhe esta Página