09. The print toolkit
Since Chapter 07, print has been your window into a
running program. It does more than show one string. This chapter
collects the tricks you use from here on: several values at once,
controlling what goes between them, and blank lines.
Several values, separated by spaces
Separate values with commas to pass print more than one.
It prints them on one line with a space between
each:
print("Name", "Level", "HP")
print("Keiko", 7, 95)Output:
Name Level HP
Keiko 7 95
Two things to notice. Values can be different types — strings and
numbers mixed freely. And numbers need no quotes; 7 is a
number, "7" a string.
Controlling the separator
By default print uses a single space between values. You
can change that with the sep keyword argument:
print("Name", "Level", "HP", sep=", ")
print("Keiko", 7, 95, sep=", ")Output:
Name, Level, HP
Keiko, 7, 95
Set sep="" (empty string) to put nothing between the
values.
Controlling the ending
By default print puts a new line at the end. You can
change that with the end keyword argument:
print("Loading", end="")
print("... done")Output:
Loading... done
Both calls land on the same line because the first one did not end with a new line.
F-strings: building a line exactly
For full control of spacing and layout, use an
f-string. Put f before the opening quote
and use {} curly braces to drop a value into the text:
name = "Keiko"
hp = 95
level = 7
print(f"{name} has {hp} HP at level {level}")Output:
Keiko has 95 HP at level 7
With an f-string there is no automatic separator — you decide what goes between the pieces, spaces included. F-strings also handle numbers directly; no conversion needed.
Open exercises/09/01-comma-vs-fstring.py. It prints
three values twice: once with commas, once with an f-string. Run it and
compare the two outputs.
Commas or f-strings: which to use?
Both are fine. A rough guide:
- Commas: quickest when space-separated output is good enough (debugging, quick checks).
- F-strings: for a polished sentence with exact
spacing and punctuation, like
"Keiko has 95 HP".
name = "Keiko"
hp = 95
print(name, "has", hp, "HP") # Keiko has 95 HP
print(f"{name} has {hp} HP") # Keiko has 95 HPThe second is more readable when the output needs to look like a sentence.
Blank lines
print() with nothing inside prints an empty line. Use it
to space out output so it reads more easily:
print("Chapter 1")
print()
print("Chapter 2")Output:
Chapter 1
Chapter 2
A number is not its text
One last thing. These two look the same but are different types:
print(7) # the number seven
print("7") # a string containing the character 7Both display 7, but one is a number and the other text —
different types. Python can sometimes bridge the gap, but do
not count on it. You meet types in the next part. For now, just know a
number and its text version differ even when they look identical. It
matters in the input chapter, where what the user types arrives as
text.
Homework
Homework files are in exercises/09/homework/.
Problem 1 — A stat row
Open exercises/09/homework/01-stat-row.py. Using a
single print with commas, print these on
one line: the string Keiko, the number 7, and
the number 95. Run it and notice the spaces between.
Problem 2 — A polished line
Open exercises/09/homework/02-polished-line.py. Print
this exact line:
Keiko has 95 HP at level 7
using an f-string, with name, hp, and
level from the variables at the top. The spacing must match
exactly.
Problem 3 — Spaced out
Open exercises/09/homework/03-spaced-out.py. Print three
lines of text with one blank line between each, using
print() for the blanks. Five calls total.
Challenge — Receipt
Open exercises/09/homework/04-receipt.py. Three items
with prices sit in variables. Print a receipt: each item on its own line
using an f-string as f"{name}: {price}", a blank line, then
a total line also built with an f-string. Compute the total from the
variables, do not type it as a number.
Stuck or finished? Open the homework solutions page.