Quarry School

Format a line of output — quick review

In plain words

A volunteer writes shelf lengths on small bin cards.

Rule

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.

Step

  1. 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.

  2. Inside print(...), start the template with f". Write {name} where the shelf name goes. Type a colon and one space after it.

  3. Add {length:.2f}, then one space and m. Type the closing quote and the closing parenthesis ).

  4. 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.

Trap

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.