Format a line of output
Quick review (2 minutes)
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.
1 In plain words
A volunteer writes shelf lengths on small bin cards. Every card follows the same pattern. Write the shelf name, a colon and a space, the length with two digits after the decimal point, then a space and m. On these cards, m stands for meters. Read Oak: 3.50 m as “Oak, three point five zero meters.”
The text a program displays is one kind of output. To format output is to choose how it looks. In Python 3.13, put f or F immediately before the opening quote to start an f-string. That first letter is the prefix. This lesson uses f. Think of the f-string as a template, a pattern with places to fill in. Remember the prefix and the braces {}. For these lengths, look up .2f when you need two digits after the decimal point.
2 Picture it
In the f-string, the name field connects to Oak, the text colon and space stays where it is, and the length field with .2f connects to 3.50. A space and m finish the printed line Oak: 3.50 m.
3 Why
name and length are names for stored values. Python uses the text held by name to fill {name}. In {length:.2f}, Python takes the number held by length and uses .2f to write it. The colon inside those braces starts the format instruction. The colon after {name} is part of the card text, along with the space after it.
4 The 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.
5 How
- 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.
6 Worked example
Fill the Oak shelf card
name = "Oak"
length = 3.5
print(f"{name}: {length:.2f} m")
Standard input
Empty
Standard output
Oak: 3.50 m
Type the program into an editor and run the file. In the output, count two digits after the decimal point. Check the space after the colon and the space before m. The number assigned to length remains 3.5.
Trace
| Part of template | Output | Explanation |
|---|---|---|
{name} | Oak | Use the text stored in name. |
| Colon and space | : | Copy both literal characters. |
{length:.2f} | 3.50 | Write the number with two decimal places. |
| Space and m | m | Copy these two literal characters. Then print ends the line. |
7 The 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.
Quick checks
Quick check 1 of 2
Keep the Oak values. In the editor, replace the last line with print("{name}: {length:.2f} m"). What line appears in the shell when you run the file?
Show the steps and the answer
{name}: {length:.2f} m
Why: The opening quote has no f before it, so the text inside the quotes appears as typed. The line ends with a newline.
Source: Python Software Foundation, Python 3.13 Tutorial, Formatted String Literals
Quick check 2 of 2
Use the supplied values name = "Elm" and length = 2.3125 in the editor. Use the original print line with its f prefix. What line appears in the shell when you run the file?
Show the steps and the answer
Elm: 2.31 m
Why: The third digit after the decimal point is 2, so rounding to two places keeps 1 as the second digit, in the hundredths place. The line ends with a newline.
Source: Python Software Foundation, Python 3.13 Tutorial, Formatted String Literals
Sources
- Python 3.13 Tutorial, Formatted String Literals. Python Software Foundation. Python Software Foundation License Version 2. Read 26 Nov 2026.