f-string
PronunciationEF-string
Definition
An f-string is a Python string that starts with f and evaluates the expressions inside {} to build the final text.
Where you hear it
In Python tutorials and code reviews that replace older string formatting.
Examples
f"Hello, {name}!"puts the name into the greeting.f"{price:,.2f}"formats a number with commas and two decimals.Build the log line with an f-string that includes the number of rows.
Common mistake
Building log or SQL text with an f-string from user input. For SQL, always use parameters.
Don't confuse with
A plain string, which has no placeholders and never evaluates what is in braces.
Say it at work
- In a meeting or chat
Use an f-string instead of concatenating with plus signs.
- In an email or ticket
Format the amount in the f-string with two decimals.
Keep exploring
More in Programming Fundamentals