What are f-strings in Python?
Learn Python f-strings: how the f prefix embeds expressions in {}, format specifiers like {value:.2f}, the {x=} debug syntax, and why they beat older methods.
Expected Interview Answer
An f-string (formatted string literal) is a string prefixed with f or F that lets you embed Python expressions directly inside curly braces, which are evaluated at runtime and inserted into the string.
Introduced in Python 3.6, f-strings replace clumsier approaches like %-formatting and str.format(). Inside the braces you can put variables, arithmetic, method calls, or any expression, and add format specifiers after a colon (e.g. {price:.2f}). They are faster and more readable than older methods, and since Python 3.8 the {expr=} syntax prints both the expression and its value, which is handy for debugging.
- More readable than %-formatting or str.format()
- Faster at runtime than older formatting methods
- Supports inline expressions and method calls
- Rich format specifiers for numbers, dates, and alignment
- Self-documenting debug output with {expr=}
AI Mentor Explanation
A live scoreboard has fixed labels like 'Score:' and 'Overs:' with blank slots that fill automatically from the match feed as runs and wickets change. You write the template once and the numbers slot in live. An f-string is that scoreboard template: fixed text with braces where Python drops in the current values of your variables at runtime.
Step-by-Step Explanation
Step 1
Prefix the string
Put f or F before the opening quote, e.g. f"Hello", to make it a formatted string literal.
Step 2
Embed expressions
Place any Python expression inside curly braces: f"Total is {price * qty}".
Step 3
Add format specifiers
After a colon, control formatting: f"{value:.2f}" for two decimals or f"{n:,}" for thousands separators.
Step 4
Use debug syntax
In Python 3.8+, f"{x=}" prints both the expression text and its value for quick debugging.
Step 5
Escape braces if needed
Write {{ and }} to include literal curly braces in the output.
What Interviewer Expects
- Knowing the f/F prefix creates the literal
- Understanding expressions are evaluated at runtime
- Familiarity with format specifiers after the colon
- Awareness that f-strings arrived in Python 3.6
- Comparison with %-formatting and str.format()
Common Mistakes
- Forgetting the f prefix so braces print literally
- Thinking f-strings work before Python 3.6
- Confusing format specifiers with regular text
- Using single braces when a literal brace {{ }} is needed
- Putting a backslash inside the braces (not allowed before 3.12)
Best Answer (HR Friendly)
“An f-string is a Python string with an f in front that lets you drop variables and small calculations straight into the text using curly braces. It makes building readable, dynamic messages simple and is faster than the older formatting styles.”
Code Example
name = "Asha"
price = 1234.5
qty = 3
# Embed variables and expressions
print(f"Hi {name}, your total is {price * qty}")
# Format specifiers: 2 decimals and thousands separator
print(f"Formatted: {price:,.2f}")
# Call methods inline
print(f"Upper: {name.upper()}")
# Debug syntax (Python 3.8+)
print(f"{qty=}") # prints: qty=3
# Literal braces need doubling
print(f"{{not a variable}}") # prints: {not a variable}Follow-up Questions
- How do f-strings differ from str.format() and %-formatting?
- What does the {value:.2f} format specifier do?
- What is the {expr=} debug syntax and when was it added?
- Are f-strings evaluated at compile time or runtime?
- How do you include a literal curly brace in an f-string?
MCQ Practice
1. What prefix marks a string as an f-string?
An f or F before the opening quote turns the literal into a formatted string that evaluates embedded expressions.
2. What does f"{price:.2f}" produce for price = 3.5?
The .2f format specifier formats the number as a float with exactly two decimal places, giving 3.50.
3. In which Python version were f-strings introduced?
Formatted string literals (f-strings) were added in Python 3.6 via PEP 498.
Flash Cards
What is an f-string? — A string prefixed with f/F that evaluates expressions inside {} at runtime and inserts their values.
When were f-strings added? — Python 3.6 (PEP 498).
How do you format two decimals? — Use a format specifier after a colon: f"{value:.2f}".
What does f"{x=}" do? — Python 3.8+ debug syntax: prints both the expression text and its value, e.g. x=5.
How do you print a literal { in an f-string? — Double it: write {{ and }} to output single braces.