Command Palette

Search for a command to run...

Hectal
Back to the lesson: Topic 1.9 — Printing and f-strings
Python · Example 1 of 4

print options and f-string format specs

Change the widths and the number of decimals and watch the output change.

print shows values on the screen and can be tuned with sep and end. An f-string, written f"...{value}...", builds text by putting values into {} slots, and a format spec such as :.2f or :>8 controls how each value looks.

Change the code and press Run (Ctrl+Enter). Predict what will happen first, then break it on purpose and read what changes. Your edits are saved and match the lesson page.

Ready for more? This phase has 6 hands-on exercises with automatic checks.

Practice questions

Write the code in the editor, run it, then open the model answer to compare.

01

Set item = "Notebook", price = 45.5, qty = 3, and print Notebook: 3 x 45.50 = 136.50 with an f-string.

02

Print the number 1234567.891 with commas and one decimal place.

03

Print the three words red, green, blue on one line separated by | .

Explain it without notes

01

How do sep and end change what print does?

02

What is an f-string, and why is it better than joining strings with +?

03

Explain {value:>8}, {value:.2f} and {value:,}.

04

What is the difference between str(x) and repr(x)? When would you use repr?

print options and f-string format specs
Sign in to run this example in your browser.

Expected output

a b c
a-b-c
loading...done
xy

Name	Score
1 2.5 True None
Name: Asha
Score: 91.46
1,234,567
|    Asha|Asha    |  Asha  |
25.6%
007 11111111 ff    3.142
{braces} and Asha