Python f-Strings

In the last lesson you learned Getting User Input, where the program asks a person to type something and stores it in a variable. So now you have a name. Or an age. Or a price. They are all sitting in variables. The next thing you almost always want to do is drop those values into a sentence and print them. That is exactly what f-strings are for.

๐Ÿค” Why f-Strings?

Here is the pain. You have a name in a variable and you want to print a friendly greeting. The old way is to glue pieces of text together with the + sign. And it gets ugly fast.

Look at this older style first. We have a name and we try to build a sentence from it.

name = "Riya"
print("Hi " + name + ", welcome back!")

It works. But see the problem? You have to remember the spaces inside the quotes. You have to break the sentence into little pieces. And the moment one of your values is a number, it breaks completely:

age = 25
# โŒ Avoid: mixing text and a number with + crashes
print("You are " + age + " years old")

That line gives you an error. Python will not glue text and a number together with +. You would have to wrap the number in str(...) every single time. Tiring, right?

f-strings fix all of this. You write one normal sentence and just slot your values right where they belong.

๐Ÿงฉ What an f-String Is

An f-string is a normal string with the letter f written just before the opening quote. Inside it, anything you put in curly braces { } gets replaced with the value of that variable or expression.

Think of it like filling in a form. The sentence is printed on the paper already. The blanks are the curly braces. Python reads each blank, looks up the value, and writes it in for you.

Here is the same greeting, now as an f-string. Notice the f before the quote and the name sitting inside the braces.

name = "Riya"
print(f"Hi {name}, welcome back!")

Output

Hi Riya, welcome back!

No + signs. No counting spaces. You read the line and it looks almost exactly like the sentence you wanted. That is the whole idea.

You can drop in as many values as you like. Each one just needs its own pair of braces.

name = "Arjun"
age = 30
print(f"Hi {name}, you are {age} years old.")

Output

Hi Arjun, you are 30 years old.

And notice the second thing here. The age is a number, and the f-string handled it without any complaint. No str(...) needed. Python turns it into text for you automatically inside the braces.

Tip

If you forget the f before the quote, Python will not complain, but it also will not replace anything. You will literally see Hi {name} printed with the braces. So when your variables show up as raw text, check for that missing f first.

๐Ÿงฎ Running Expressions Inside the Braces

Here is where f-strings get genuinely useful. The thing inside the curly braces does not have to be a plain variable. It can be a small calculation. And Python works it out right there.

Say you are building a bill. You have a price for one item and a quantity. You want the total. You can do the multiplication straight inside the braces.

price = 50
qty = 3
print(f"Total: {price * qty}")

Output

Total: 150

Python sees {price * qty}, multiplies the two numbers, gets 150, and writes that into the sentence. You did not need a separate total variable at all.

That said, do not stuff a giant calculation inside the braces. A small multiplication or addition is fine and easy to read. Anything bigger, give it its own variable first so your sentence stays clean.

๐Ÿ’ฐ Formatting Numbers to Two Decimals

Now a real problem with money. When you do math with prices, Python often hands you a long ugly number with many decimals. Nobody wants to see a bill that says 66.66666666666667.

Watch what happens with a plain f-string here. Three people split a bill of 200.

total = 200
people = 3
print(f"Each person pays: {total / people}")

Output

Each person pays: 66.66666666666667

That is correct, but it looks terrible on a bill. You want two decimal places, like real money. f-strings let you ask for that with a small instruction after a colon inside the braces.

The rule is {value:.2f}. The :.2f part means โ€œshow this as a number with two digits after the decimal pointโ€. Here it is in action.

total = 200
people = 3
print(f"Each person pays: {total / people:.2f}")

Output

Each person pays: 66.67

Much better. The .2f rounded it to two decimals and printed it like proper money. You can change the 2 to any number of decimals you want. .0f gives you no decimals. .3f gives you three.

Note

The colon inside the braces is the start of a formatting instruction. So {value} just prints the value, but {value:.2f} prints it with two decimal places. The variable name always comes first, then the colon, then the format.

Let us put it together into a small bill, the kind of thing you would actually print for a customer.

item = "Coffee"
price = 4.5
qty = 2
print(f"{qty} x {item}")
print(f"Total: ${price * qty:.2f}")

Output

2 x Coffee Total: $9.00

See how readable that is? The math happens inside the braces, the .2f keeps the money neat, and the whole thing reads like the receipt you wanted.

๐Ÿ“‹ Quick Reference

Here are the f-string moves you will reach for most often.

You write What it does Result
f"Hi {name}" Puts the value of name into the text Hi Riya
f"{price * qty}" Runs the calculation, then prints the result 150
f"{value:.2f}" Shows the number with two decimal places 66.67
f"{value:.0f}" Shows the number with no decimals 67

โš ๏ธ Common Mistakes

A few small slips trip people up with f-strings. Watch for these. The first one is forgetting the f before the quote. Without it, the braces print as plain text and nothing gets replaced.

name = "Alex"
# โŒ Avoid: no f, so the braces are printed literally
print("Hi {name}")
# โœ… Good: the f turns the braces into a slot
print(f"Hi {name}")

The next one is putting the colon and format in the wrong order. The variable or expression comes first, then the colon, then the format code.

.2f
price = 4.5
# โŒ Avoid: format code before the value
print(f"{:.2f price}")
print(f"{price:.2f}")

The last one is writing a bare { or } in the text when you actually want to print a curly brace. Inside an f-string, a single brace is treated as a slot. If you genuinely want to print a brace, double it: {{ prints one {.

โœ… Best Practices

Keep these habits and your f-strings will stay clean and readable.

  • Reach for f-strings by default. They are the modern, clear way to build text in Python 3, so prefer them over + gluing and over older formatting styles.
  • Keep what is inside the braces small. A variable or a short calculation is great. A long formula should get its own variable first.
  • Always format money with :.2f so prices show as real amounts and not long decimal tails.
  • Let f-strings handle numbers for you instead of wrapping things in str(...). The braces convert values to text automatically.

๐Ÿงฉ What Youโ€™ve Learned

โœ… An f-string is a string with f before the quote, and { } marks the spots where values get filled in.

โœ… You can drop variables straight into the text, and numbers get turned into text for you, no str(...) needed.

โœ… You can run a small calculation inside the braces, like {price * qty}, and Python prints the result.

โœ… You can format numbers with {value:.2f} to show a clean two-decimal amount, which is perfect for money.

โœ… f-strings are cleaner and safer than gluing text together with +.

Check Your Knowledge

Test what you learned. Pick an answer for each question, then click Check.

  1. 1

    What does the f before the opening quote in f"Hi {name}" do?

    Why: The f marks the string as an f-string, so Python replaces each { } with the value of the variable or expression inside it.

  2. 2

    What does f"Total: {price * qty}" print when price is 50 and qty is 3?

    Why: Python runs the calculation inside the braces, gets 150, and writes that result into the text.

  3. 3

    Which format shows a number with exactly two decimal places?

    Why: The :.2f instruction after the value means show it as a number with two digits after the decimal point.

  4. 4

    Why is f"You are {age} years old" better than "You are " + age + " years old"?

    Why: An f-string turns the number into text for you, but gluing text and a number with + raises an error unless you wrap it in str().

๐Ÿš€ Whatโ€™s Next?

Now you can build clean, readable text from any values you have. Next we will step back and look at the different kinds of values themselves, like text, numbers, and true/false, so you know what you are actually working with.

Python Data Types Overview

Share & Connect