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:
The last line is a number on its own. Now run this:
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:
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:
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
fgoes 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
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:
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:
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:
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.
- Run the starting code below. Confirm the last line is your total as a bare number.
- Change the last line to
print(f"Total spent: {total}"). Run it. Confirm it readsTotal spent:followed by your total. - 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. - Build your title from your name. Add a new first line:
owner = "Your Name", with your own name. Change thetitleline totitle = f"{owner}'s Money Tracker". Run it. Confirm your title looks the same as before. - Break it on purpose: delete the
fin front of the"Total spentstring. Run it. Confirm the console shows the braces as text, with no error. - Fix it: put the
fback. Run it. Confirm the total line shows the amount again.
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.