Printing & Output
Display results with print(), format text with f-strings, and control what appears on screen.
- Use print() to display values and messages
- Format output with f-strings and format specifiers
- Combine multiple values in a single print() call
An answer locked inside the machine
Try this: evaluate
By hand you would write the steps and the answer on paper:
Getting the answer is only half of solving a problem, the other half is communicating it. Now hand that same expression to a computer. It computes in a blink, and immediately forgets to tell you. The value sits silently inside the machine, and unless you demand it come out, you will never see it.
That invisible computation is the fundamental problem this lesson solves. A program needs a way to write its results where a human can read them, in Python, that instruction is print().
print(), reveal a value
print() takes a value and sends it to the screen. Any value works: Python converts it to text first.
print(13.4) # 13.4
print(42) # 42
print("hello") # helloYou don’t have to print a finished number, print() accepts any expression and evaluates it first:
print((17 * 3 + 4**2) / 5) # 13.4The pattern to keep in mind: compute something, then hand it to print(). Values have no way out of a program on their own; printing is the exit.
A number, and its label
A bare number rarely means much. On paper you wouldn’t write alone, you would write “Score: 13.4”. Give print() multiple arguments and it places a space between them:
print("Score:", 87) # Score: 87The first argument is the label, the second is the value. You can stack as many as you like, and print() keeps them apart for you.
Showing exactly the digits you mean
Here is where the trouble starts. The value
carries all of its digits with it at all times. But a table needs , a weather line needs 23.8°C, not 23.7891°C. The number of digits shown is a choice of how to present the number, it must not change the stored value, or you lose precision forever.
So the rounding must live in the printing, not in the computation. An f-string lets you decide at print time: write f"...", put the expression inside {...}, and append a format specifier after a colon:
price = 19.999
print(f"Total: ${price:.2f}") # Total: $20.00 (shown rounded)
print(price) # 19.999 (value untouched)
print(f"Double: {price * 2}") # 39.998 (any expression works)Two things are going on in {price:.2f} that are worth teasing apart:
{price}says put the value here, the f-string does the conversion to text for you.:.2fsays render it as a fixed-point number with 2 digits after the decimal, rounding happens only in the displayed form.
Format specifiers do more than round: they also align. A column of 7.5, 8.5, 87.5 looks ragged; give every entry the same width and the column lines up:
print(f"{7.5:>6}") # " 7.5" right-aligned in a width of 6
print(f"{87.5:>6}") # " 87.5"A worked example: the price column
A shop receipt wants the labels on the left and the numbers lined up by their decimals. Two format directions do both:
print(f"{'item':<10}{'price':>7}")
print(f"{'coffee':<10}{3.5:>7.2f}")
print(f"{'croissant':<10}{2.95:>7.2f}")
# item price
# coffee 3.50
# croissant 2.95<10 left-aligns the label across a width of ten columns; >7.2f right-aligns the number across seven, keeping two decimals. Alignment is just formatting in the other direction, the same {value:spec} you already know, with the arrow telling which way the text leans. This is the seed of every table the course will build: labels one way, numbers the other.
Common pitfalls
print()has no return value.print("hi")shows text but evaluates toNone, you cannot capture what it printed back into a variable. Printing is the end of a computation, never a step inside it.- Mixing types with
+.print("Score: " + 87)raises aTypeError, because a string and a number cannot be added. F-strings exist precisely to pair a label with a value:print(f"Score: {87}"). - A literal
{in an f-string needs{{.f"{{x}}"prints{x}; a lone{is read as the start of an expression. Doubling is the escape hatch.
🧩 Challenges
🧩 Challenge, think first, then reveal
Evaluate on paper, then print it without typing the answer yourself, let the computer compute and print in one step.
💡 Answer: print((2**5 + 9) / 5) → 8.2. One expression, handed straight to print(): the machine evaluates it and writes the result.
🧩 Challenge, think first, then reveal
Given temperature = 23.7891, print it as "Today: 23.8°C" (one decimal place) without touching the stored value.
💡 Answer: print(f"Today: {temperature:.1f}°C") → Today: 23.8°C. The :.1f specifier rounds at display time only; temperature remains 23.7891.
🤔 Socratic Questions
- Why does Python use
print()as a function (with parentheses) rather than a statement? What advantage does that give you? print("A", "B", "C")printsA B Cwith spaces. How could you print them with no spaces? With commas between them?- If
x = 3.14, what doesf"{x}"produce? What aboutf"{x:.0f}"? Explain the difference in terms of the value versus its rendering.
✅ Quick check
1. Your formula is computed, but the program shows nothing. Why?
2. How do you print 3.14159 with two decimal places shown, without rounding the stored value?