Theory

F-strings make string formatting easy and readable.

Basic syntax - put f before the string and use {} for variables:

name = "Alice"
age = 25
print(f"My name is {name}") # My name is Alice
print(f"I am {age} years old") # I am 25 years old

You can do calculations inside {}:

a = 5
b = 3
print(f"{a} + {b} = {a + b}") # 5 + 3 = 8

Call methods inside {}:

word = "python"
print(f"Upper: {word.upper()}") # Upper: PYTHON

Exercises in this lesson

Solve in the playground
Next lesson: While Loops Pro →