Format a line of output — quick review
A volunteer writes shelf lengths on small bin cards.
Put f right before the opening quote. Inside the quotes, put the names whose values you want to use inside braces. Type the literal pieces where they should appear. Write :.2f after the number’s name inside its braces. For the lengths on these cards, .2f writes out two places after the decimal point, adding a zero or rounding the displayed number as needed.
In these examples, print(...) displays the finished text and then adds a newline, which ends the line.
Give each value a name. Put the shelf name in quotes and write the length as a number. On each line,
=gives the name on the left the value on the right.Inside
print(...), start the template withf". Write{name}where the shelf name goes. Type a colon and one space after it.Add
{length:.2f}, then one space andm. Type the closing quote and the closing parenthesis).Predict the printed line. Read the name, the colon and space, the number with two digits after the decimal point, then the space and unit.
If you see braces in the printed line, check for the f before the opening quote. In this example, leaving it out makes Python print the text inside the quotes as written, braces and all. With the prefix in place, the format instruction controls the number’s appearance on the card. length still holds 3.5 after the program prints 3.50.