PYTHON BASICSChapter 1 · Python Basics
Formatted strings
An f-string is a string with an f before the opening quote. Anything inside {curly braces} is worked out and dropped into the text, so you never glue pieces together with +. You can even format numbers: {price:.2f} always shows two decimal places.
Worked example
How it reads
- The f before the quote switches on the braces
- Braces can hold any expression, like
{stars * 2} :.2fafter a value formats it with two decimal places
Common mistakes
- Forgetting the
f: Python then prints the braces literally. - Using the same quote inside the braces as around the string. Use
'inside a string wrapped in".

Cloud tip: f-strings convert numbers to text for you, so there is no need for
str(stars) when mixing words and numbers.

