Control exactly what print() writes: several values, custom separators with sep, line endings with end, and a first look at f-strings.
About 25 minFirst steps3 exercisesFree
You will be able to
Print several values in one call and control what goes between them with sep
Control what print() writes at the end of a line with end
Print empty lines where a layout needs them
Put calculated values inside text with an f-string, and show money with two decimal places
Receipts, invoices and reports are judged on their layout: a missing space or an extra line break makes output look careless, and it can break another program that reads your output. So far print() has always added a space between values and a line break at the end. In this lesson you'll take control of both, and meet the most convenient way to mix text and numbers: the f-string.
What print() does by default
When you give print() several values separated by commas, it:
converts each value to text,
writes them with one space between each pair,
finishes with a line break, so the next print() starts on a new line.
Both the space and the line break can be changed. Each has a named setting you can add inside the parentheses: sep (separator) and end. Settings you give by name like this are called keyword arguments.
Put the letter f right before the opening quote, and you can write any calculation inside { } in the string. Python works out each calculation and puts the result into the text. This is an f-string (formatted string).
Calculations inside an f-string
print(f"3 notebooks cost {3*12} SAR")print(f"Half of 75 is {75/2}")print(f"{7} days a week, {7*24} hours")
Try it: Remove the f before the first string and run it. What appears instead of the total?
Without the f, the braces and the calculation are printed as ordinary text. With it, you control every space yourself, which is often easier than juggling commas and sep.
f-strings become far more useful once you can give values names, which you'll do in the next lesson.
Showing money with two decimal places
Prices are normally shown with exactly two decimals: 7.50, not 7.5; 12.00, not 12. Inside the braces of an f-string, add :.2f after the calculation to round it to two decimal places for display.
Formatting with :.2f
print(f"Price: {7.5:.2f} SAR")print(f"Three at 4.25: {3*4.25:.2f} SAR")print(f"Shared by 3: {100/3:.2f} SAR")print(f"Tiny rounding hidden: {0.1+0.2:.2f}")
Try it: Change .2f to .1f and then to .0f. The number after the dot is the number of decimal places.
Read :.2f as "format as a decimal number with 2 places". It only changes how the number is displayed; it rounds to the nearest hundredth. That also hides the tiny float rounding you saw in Numbers and arithmetic.
Guided practice · ●difficulty 1 of 5 · about 3 min
Print a date with sep
An appointment system stores a date as three separate values: day 15, month "03" and year 2026. The starter prints them with spaces:
15 03 2026
Change the program so it prints the date with slashes and no spaces:
15/03/2026
Keep the three values separate and use sep.
Python code editor. Tab indents. Press Escape then Tab to leave the editor. Control or Command plus Enter runs the code; Shift plus Enter checks your answer.
Ctrl+Enter run · Shift+Enter check · your code is saved in this browser as you type
Output
Run your code to see its output, or Check to test it against the requirements.
The solution becomes available after you pass, or after all three hints and two checks.
Challenge · ●●difficulty 2 of 5 · about 6 min
A tile order summary
Sahara Tiles sells boxes of tiles at 85.5 SAR per box. Order 1042 is for 3 boxes. Print this summary:
Order 1042 | Sahara Tiles | 3 boxes
Subtotal: 256.50 SAR
Requirements:
Print the first line with sep=" | ", giving print() the three parts "Order 1042", "Sahara Tiles" and "3 boxes".
Print the second line with an f-string that calculates3 * 85.5 and shows it with two decimal places.
Python code editor. Tab indents. Press Escape then Tab to leave the editor. Control or Command plus Enter runs the code; Shift plus Enter checks your answer.
Ctrl+Enter run · Shift+Enter check · your code is saved in this browser as you type
Output
Run your code to see its output, or Check to test it against the requirements.
The solution becomes available after you pass, or after all three hints and two checks.
Debugging · ●●difficulty 2 of 5 · about 5 min
Fix the boarding countdown
A gate display should show a countdown on one line, then Board now! on its own line:
Boarding in 3...2...1...
Board now!
Instead it prints everything squashed together on one line:
Boarding in3...2...1...Board now!
Fix the sep and end settings so the output matches. Keep the three print() calls.
Python code editor. Tab indents. Press Escape then Tab to leave the editor. Control or Command plus Enter runs the code; Shift plus Enter checks your answer.
Ctrl+Enter run · Shift+Enter check · your code is saved in this browser as you type
Output
Run your code to see its output, or Check to test it against the requirements.
The solution becomes available after you pass, or after all three hints and two checks.
Summary
print() puts sep between values (a space by default) and end after them (a line break by default).
sep="" joins values with nothing between them; end="" keeps the next print on the same line.
print() on its own prints an empty line.
An f-string, f"...{calculation}...", puts calculated values inside text.
{value:.2f} displays a number with exactly two decimal places, ideal for money.