Skip to main content
eLearner.app
Module 1 · Lesson 4 of 44/36 in the course~8 min
Module lessons (4/4)

Printing and f-strings

To communicate something to the user (or to see what is happening inside our program) we use the built-in print(...) function. To compose text from multiple values, the modern and preferred syntax is the f-string.

print(...)

Python
print("Ciao, mondo!")           # Ciao, mondo!
print("a", "b", "c")            # a b c       (separati da spazio)
print(2 + 2)                    # 4
print("riga 1", "riga 2")       # riga 1 riga 2

print can take as many arguments as you want: it joins them with a space (by default) and appends a newline at the end. You can change both with the sep and end parameters:

Python
print("a", "b", "c", sep="-")   # a-b-c
print("senza newline", end="")  # niente \n dopo

f-string: modern interpolation

An f-string is a string prefixed by f: inside the braces {...} you can insert any Python expression, which will be evaluated and inserted into the result:

Python
nome = "Ada"
anni = 30
print(f"Mi chiamo {nome} e ho {anni} anni.")
# Mi chiamo Ada e ho 30 anni.

Inside the braces you can put calls, operations, object accesses — it is Python code in all respects:

Python
prezzo = 12.5
quantita = 3
print(f"Totale: {prezzo * quantita:.2f} €")
# Totale: 37.50 €

The :.2f after the colon is a format spec: in this case "two decimal digits, fixed-point". The most useful format specs:

  • :.Nf — N decimal digits (f"{pi:.4f}"3.1416).
  • :N / :>N / :<N / :^N — width N, right/left/center alignment.
  • :_ or :, — thousands separator (f"{1000000:,}"1,000,000).

Alternative syntaxes (for reference)

Before f-strings (introduced in Python 3.6) two forms were used that you'll still encounter in older code:

Python
# stile .format() — verboso ma esplicito
"{} ha {} anni".format("Ada", 30)

# stile %-formatting — molto vecchio
"%s ha %d anni" % ("Ada", 30)

For new code, always use f-strings: shorter, more readable, faster.

Advanced formatting with f-strings

F-strings support rich formatting specifications directly inside the curly braces. For instance, to format a decimal number with exactly two decimal places, you can write:

Python
price = 19.99
print(f"Price: {price:.2f} $")

The :.2f specifier indicates that we want to represent the float with exactly 2 decimal places.

Try it yourself

Exercise#python.m1.l4.e1
Attempts: 0Loading…

Given `name = 'Ada'` and `age = 30`, build the string 'Ada ha 30 anni' with an f-string and assign it to `line`. Evaluate `line`.

Loading editor…
Show hint

Syntax: f"something {variable} something else"

Solution available after 3 attempts

Review exercise

Exercise#python.m1.l4.e2
Attempts: 0Loading…

Print with print() the string 'pi greco vale 3.14' starting from `pi = 3.14159`, using an f-string with a format spec of 2 decimals.

Loading editor…
Show hint

The format spec goes after the colon inside the braces: {pi:.2f}.

Solution available after 3 attempts

Additional challenge

Exercise#python.m1.l4.e3
Attempts: 0Loading…

Given a variable `price` set to `19.99`, use an f-string to construct the string `'Price: 19.99 €'` and store it in `receipt_line`. Finally, evaluate the variable.

Loading editor…
Show hint

Use f"Price: {price} €" and assign the result to receipt_line.

Solution available after 3 attempts