SypherTec
…
PART 1 · 04 OF 073 MIN READUPDATED 19 NOV 2026

f-strings: Putting Values Inside Text

Your report ends with a bare number. This lesson puts it inside a sentence a reader understands, with a naira sign and a comma: Total spent: ₦7,500.

The concept

Here is your tracker as the last lesson left it. Run it:

WHERE YOU LEFT OFF · PYTHON
Tab indents. Press Escape, then Tab, to leave the editor.
CONSOLE
no output

The last line is a number on its own. Now run this:

A NUMBER INSIDE A SENTENCE · PYTHON
Tab indents. Press Escape, then Tab, to leave the editor.
CONSOLE
no output

The sentence and the number came out together. Two things made that happen. There is an f right before the opening quote. And inside the string, total sits between braces: { and }.

A string with an f in front is an f-string. Python reads an f-string before printing it. Every pair of braces is a gap, and Python fills the gap with the value of the name inside.

One string can hold several values:

TWO VALUES IN ONE STRING · PYTHON
Tab indents. Press Escape, then Tab, to leave the editor.
CONSOLE
no output

The apostrophe and the spaces stay exactly where you typed them. Only the braces are replaced.

Money is easier to read with a currency sign and commas between thousands:

A NAIRA SIGN AND THOUSANDS COMMAS · PYTHON
Tab indents. Press Escape, then Tab, to leave the editor.
CONSOLE
no output

The ₦ sign is ordinary text inside the string. Python prints it like any other character. If your keyboard has no ₦ key, copy it from this page. The :, inside the braces did the rest: it showed 1250000 as 1,250,000.

The syntax

f"Total spent: ₦{total:,}"
  • The f goes directly before the opening quote, with no space.
  • Braces hold a variable name. Python replaces the braces and the name with the name's value.
  • :, after the name, inside the braces, shows the number with thousands commas.

The :, changes how the number looks when printed. It does not change what is stored. total still holds 7500. That is the right way round: commas belong in what you show, never in the number you store.

An f-string is still a string, so you can store it in a variable instead of printing it straight away:

owner = "Ada"
title = f"{owner}'s Money Tracker"

A minimal working example

main.py
owner = "Ada"
total = 7500
title = f"{owner}'s Money Tracker"
 
print(title)
print(f"Total spent: ₦{total:,}")

Output: Ada's Money Tracker, then Total spent: ₦7,500. Change owner or total at the top, and both lines follow.

Common mistakes

You forget the f. Run this:

THE F IS MISSING · PYTHON
Tab indents. Press Escape, then Tab, to leave the editor.
CONSOLE
no output

There is no error. The console shows Total spent: {total}, braces and all. Without the f, the braces are ordinary text. If you ever see braces in your output, check for the missing f first.

You forget the closing brace. Run this:

A BRACE THAT NEVER CLOSES · PYTHON
Tab indents. Press Escape, then Tab, to leave the editor.
CONSOLE
no output
SyntaxError: f-string: expecting '}'

Every { in an f-string needs a matching }. Python names the character it was waiting for.

You mistype the name inside the braces. Run this:

A NAME INSIDE THE BRACES THAT DOES NOT EXIST · PYTHON
Tab indents. Press Escape, then Tab, to leave the editor.
CONSOLE
no output
NameError: name 'Total' is not defined. Did you mean: 'total'?

The name inside the braces follows the same rules as a name anywhere else. Capitals count, and Python suggests the close match.

Try it yourself

You will turn the bare number into a sentence, then build your title from your name.

  1. Run the starting code below. Confirm the last line is your total as a bare number.
  2. Change the last line to print(f"Total spent: {total}"). Run it. Confirm it reads Total spent: followed by your total.
  3. Add the naira sign and commas: change it to print(f"Total spent: ₦{total:,}"). Run it. Confirm the total now has a ₦ in front. If your total is 1,000 or more, confirm it shows a comma, for example ₦7,500.
  4. Build your title from your name. Add a new first line: owner = "Your Name", with your own name. Change the title line to title = f"{owner}'s Money Tracker". Run it. Confirm your title looks the same as before.
  5. Break it on purpose: delete the f in front of the "Total spent string. Run it. Confirm the console shows the braces as text, with no error.
  6. Fix it: put the f back. Run it. Confirm the total line shows the amount again.
TRY IT YOURSELF · PYTHON
Tab indents. Press Escape, then Tab, to leave the editor.
CONSOLE
no output

Every amount in your tracker so far was typed by you as a number. Real amounts often arrive as text, copied from a receipt or a form. The next lesson shows what happens when text that looks like a number meets a real one.

QUIZ · 3 QUESTIONSCheck what stuckStart the quiz
PART 1 · 04 OF 07❮❯