Python f-strings: Embed Variables in Text Output
An f-string in Python lets you embed variables directly inside text. Replace clunky str() and concatenation with clean, readable syntax like f"{name}: ${amount:.2f}". This is now the standard way to format strings.
So we've been using plus signs to build strings. We have format_sale working with name + ": $" + str(amount). It works, but when I look at it... there are all these plus signs and str() calls everywhere. It feels messy, honestly.
I'm glad you noticed. That's the exact frustration Python solved five years ago. Imagine you needed to format a line with six fields — name, amount, region, date, category, status. You'd have a return statement with twelve plus signs and six str() calls. Look at this instead:
# Old way (Days 3-4)
return name + ": $" + str(amount)
# New way (f-strings)
return f"{name}: ${amount:.2f}"
That's an f-string. The f before the quote tells Python "I'm going to embed variables inside this string." You put the variable name in curly braces — {name} — and Python swaps it in automatically.
Wait, that's it? Just put an f and then the variable name in curly braces?
Exactly. But here's the part that will blow your mind. See that :.2f after amount? That's a format specifier. It says "show this number with exactly 2 decimal places." So 1250.0 becomes 1250.00, and 340.5 becomes 340.50. No more mismatched formatting.
print(f"{1250.0:.2f}") # Output: 1250.00
print(f"{340.5:.2f}") # Output: 340.50
So the format specifier is like a mini instruction? "Hey Python, I want this number to show exactly 2 decimal places?" And it just... does it?
That's perfect. The : separates the variable from the instruction. Before the colon is the variable; after it is how to format it. For money, .2f means "two decimal places, floating-point number." A lot of people think f-strings are an advanced feature — they were added in Python 3.6 — but they're actually the standard now. Even beginners should use them from day one. They're cleaner, faster, and they handle formatting automatically.
So I can just rewrite format_sale to use f-strings and it will automatically show the right number of decimal places? That solves the $1250.0 vs $1250.00 problem from Day 4?
Exactly right. Day 4 you added .strip() to clean the whitespace. Now we add an f-string with the :.2f format specifier. One line does both jobs — it cleans the name and formats the amount:
def format_sale(name, amount):
clean_name = name.strip()
return f"{clean_name}: ${amount:.2f}"
print(format_sale("Alice Chen", 1250.00)) # Alice Chen: $1250.00
print(format_sale("Bob Kumar", 340.50)) # Bob Kumar: $340.50
print(format_sale(" Carol Santos ", 890.5)) # Carol Santos: $890.50
Notice how even 890.5 gets padded to 890.50. The format specifier enforces it — every amount shows exactly two decimal places, no exceptions.
I can see why this is better than str() and plus signs. But what if I need to add more fields? Like if Diane also wants the date or the region in the output?
You just add more variables to the f-string. Want to include a region? Add it to the function and embed it:
def format_sale(name, amount, region):
clean_name = name.strip()
return f"{clean_name} ({region}): ${amount:.2f}"
The format specifier : works on any value in the braces. Numbers can use .2f for decimals. Strings stay as-is. You can even do math inside the braces: f"{amount * 0.1:.2f}" calculates 10% of the amount and formats it. Right now format_sale treats every sale the same way. But what if a sale over $5,000 should show up differently — flagged, highlighted, formatted as VIP? Tomorrow you'll give your function the ability to make decisions. You'll use an if statement inside format_sale to check the amount and return different outputs. That's when your function becomes truly powerful.
Practice your skills
Sign up to write and run code in this lesson.
Python f-strings: Embed Variables in Text Output
An f-string in Python lets you embed variables directly inside text. Replace clunky str() and concatenation with clean, readable syntax like f"{name}: ${amount:.2f}". This is now the standard way to format strings.
So we've been using plus signs to build strings. We have format_sale working with name + ": $" + str(amount). It works, but when I look at it... there are all these plus signs and str() calls everywhere. It feels messy, honestly.
I'm glad you noticed. That's the exact frustration Python solved five years ago. Imagine you needed to format a line with six fields — name, amount, region, date, category, status. You'd have a return statement with twelve plus signs and six str() calls. Look at this instead:
# Old way (Days 3-4)
return name + ": $" + str(amount)
# New way (f-strings)
return f"{name}: ${amount:.2f}"
That's an f-string. The f before the quote tells Python "I'm going to embed variables inside this string." You put the variable name in curly braces — {name} — and Python swaps it in automatically.
Wait, that's it? Just put an f and then the variable name in curly braces?
Exactly. But here's the part that will blow your mind. See that :.2f after amount? That's a format specifier. It says "show this number with exactly 2 decimal places." So 1250.0 becomes 1250.00, and 340.5 becomes 340.50. No more mismatched formatting.
print(f"{1250.0:.2f}") # Output: 1250.00
print(f"{340.5:.2f}") # Output: 340.50
So the format specifier is like a mini instruction? "Hey Python, I want this number to show exactly 2 decimal places?" And it just... does it?
That's perfect. The : separates the variable from the instruction. Before the colon is the variable; after it is how to format it. For money, .2f means "two decimal places, floating-point number." A lot of people think f-strings are an advanced feature — they were added in Python 3.6 — but they're actually the standard now. Even beginners should use them from day one. They're cleaner, faster, and they handle formatting automatically.
So I can just rewrite format_sale to use f-strings and it will automatically show the right number of decimal places? That solves the $1250.0 vs $1250.00 problem from Day 4?
Exactly right. Day 4 you added .strip() to clean the whitespace. Now we add an f-string with the :.2f format specifier. One line does both jobs — it cleans the name and formats the amount:
def format_sale(name, amount):
clean_name = name.strip()
return f"{clean_name}: ${amount:.2f}"
print(format_sale("Alice Chen", 1250.00)) # Alice Chen: $1250.00
print(format_sale("Bob Kumar", 340.50)) # Bob Kumar: $340.50
print(format_sale(" Carol Santos ", 890.5)) # Carol Santos: $890.50
Notice how even 890.5 gets padded to 890.50. The format specifier enforces it — every amount shows exactly two decimal places, no exceptions.
I can see why this is better than str() and plus signs. But what if I need to add more fields? Like if Diane also wants the date or the region in the output?
You just add more variables to the f-string. Want to include a region? Add it to the function and embed it:
def format_sale(name, amount, region):
clean_name = name.strip()
return f"{clean_name} ({region}): ${amount:.2f}"
The format specifier : works on any value in the braces. Numbers can use .2f for decimals. Strings stay as-is. You can even do math inside the braces: f"{amount * 0.1:.2f}" calculates 10% of the amount and formats it. Right now format_sale treats every sale the same way. But what if a sale over $5,000 should show up differently — flagged, highlighted, formatted as VIP? Tomorrow you'll give your function the ability to make decisions. You'll use an if statement inside format_sale to check the amount and return different outputs. That's when your function becomes truly powerful.