Advanced Formatting: Sign
Let's now consider the formatting of integer objects. Assume we want to represent our numbers along with plus/minus sign. The minus sign will be printed anyway, but plus sign never prints with an integer.
To make Python print a sign of a number we need to set sign option. There are three of them:
Option | Meaning |
---|---|
: (space after the colon) | Use a space before positive number, a minus - before negative |
:- | Use a minus sign for negative numbers only |
:+ | Use a plus sign for positive numbers, and minus - for negative |
For example,
12print(".{:+}.{: }.".format(3, -7)) print(".{:+}.{: }.".format(-6, 2))
Swipe to start coding
Given two variables: min_temp
with value -40
and max_temp
with value 42
. You need to insert correct format options to display -
sign for min_temp
, and +
sign for max_temp
, and format string with these variables.
Lösning
Tack för dina kommentarer!
single
Fråga AI
Fråga AI
Fråga vad du vill eller prova någon av de föreslagna frågorna för att starta vårt samtal
Awesome!
Completion rate improved to 4.35Awesome!
Completion rate improved to 4.35
Advanced Formatting: Sign
Let's now consider the formatting of integer objects. Assume we want to represent our numbers along with plus/minus sign. The minus sign will be printed anyway, but plus sign never prints with an integer.
To make Python print a sign of a number we need to set sign option. There are three of them:
Option | Meaning |
---|---|
: (space after the colon) | Use a space before positive number, a minus - before negative |
:- | Use a minus sign for negative numbers only |
:+ | Use a plus sign for positive numbers, and minus - for negative |
For example,
12print(".{:+}.{: }.".format(3, -7)) print(".{:+}.{: }.".format(-6, 2))
Swipe to start coding
Given two variables: min_temp
with value -40
and max_temp
with value 42
. You need to insert correct format options to display -
sign for min_temp
, and +
sign for max_temp
, and format string with these variables.
Lösning
Tack för dina kommentarer!
single
Awesome!
Completion rate improved to 4.35
Advanced Formatting: Sign
Svep för att visa menyn
Let's now consider the formatting of integer objects. Assume we want to represent our numbers along with plus/minus sign. The minus sign will be printed anyway, but plus sign never prints with an integer.
To make Python print a sign of a number we need to set sign option. There are three of them:
Option | Meaning |
---|---|
: (space after the colon) | Use a space before positive number, a minus - before negative |
:- | Use a minus sign for negative numbers only |
:+ | Use a plus sign for positive numbers, and minus - for negative |
For example,
12print(".{:+}.{: }.".format(3, -7)) print(".{:+}.{: }.".format(-6, 2))
Swipe to start coding
Given two variables: min_temp
with value -40
and max_temp
with value 42
. You need to insert correct format options to display -
sign for min_temp
, and +
sign for max_temp
, and format string with these variables.
Lösning
Tack för dina kommentarer!