Learning LibraryCore Coding LibraryKids

f-strings: drop values right into your text

An f-string lets you build a sentence with your values tucked right inside it. Put an f before the quotes, then drop names into { }.

The big idea

An f-string is text with values slotted in using { }.

See it in code

1Start simple

Put an f before the quotes, then drop a value into { }. Python swaps in whatever name holds:

python
name = "Blaze"
print(f"Hi, {name}!")
Run it — the name lands right in the sentence:
Hi, Blaze!

{name} became Blaze. No commas, no gaps — the value just slots in.

2A step further

You can drop in as many values as you like. Here a hero sheet line uses both {name} and {health}:

python
name = "Blaze"
health = 80
print(f"{name} has {health} HP")
Run it — two values, one clean line:
Blaze has 80 HP

Two braces, two values filled in. Next we'll even do a little math right inside them.

3In our world

A full hero sheet. Write {name} and {health} where those values go — and inside the braces you can even compute, like working out the damage:

python
name = "Blaze"
health = 80

sheet = f"{name} has {health} HP"
print(sheet)

print(f"{name} attacks for {health // 4} damage!")
Run it — the values slot right into the sentence:
Blaze has 80 HP
Blaze attacks for 20 damage!

{name} became Blaze and {health} became 80. Inside the braces you can even compute: {health // 4} worked out 20. No commas, no gaps to fix — just a clean sentence.

The same idea, everywhere

f-strings make readable text everywhere. A score line, a greeting, a story sentence, a label on a chart — anywhere you want words and values together, an f-string reads far cleaner than gluing pieces with commas.

Try it yourself

Add a line: f"Welcome, {name}!". Then make a level = 3 box and print f"{name} is level {level}".

The common mistake

Forgetting the f. Without it, "{name} has {health} HP" prints the braces literally — {name} has {health} HP — instead of the values. The little f before the quote is what turns the magic on.

What it unlocks

f-strings dress up print output and show off your variables in a friendly way.

Older, or want more depth? Read the Teens version →