F-strings are string literals prefixed with f that evaluate Python expressions written inside curly braces and insert the results into the text. For example, f"Hello, {name}!" replaces {name} with the value of the variable name. Python f-strings are the standard way to build formatted text in modern Python: they are shorter than % formatting, clearer than str.format(), and usually faster.
F-strings were added in Python 3.6 and became more flexible in Python 3.12. The examples here target Python 3.13 and 3.14. You will learn the basic syntax, expressions, the = debug specifier, conversion flags, escaping braces, multi-line strings and the cases where another tool is better.
Put f or F before the opening quote, then write any expression inside {}.
The expression is evaluated when the line runs, and the result is converted to text with format(). You can use variables, arithmetic, method calls, function calls, indexing and attribute access.
Because it runs at execution time, an f-string reflects the current value of each variable. It is not a template you fill in later.
Adding = after an expression prints the expression text, an equals sign and its value. It was added in Python 3.8 and is handy for quick debugging.
By default = shows the repr() of the value, so strings appear with quotes: f"{name=}" gives name='Ada'.
After the expression you can add a conversion flag and a format spec. The conversion comes first, starting with !, and the format spec follows a colon.
| Part | Syntax | Effect |
|---|---|---|
| Conversion | !s, !r, !a | Apply str(), repr() or ascii() first |
| Format spec | :spec | Control width, alignment, precision, type |
The spec controls alignment, padding and number formats; the full list is covered in the companion question on formatting numbers. Any object can define its own spec through its __format__ method, which is how datetime supports f"{now:%Y-%m-%d}".
To print a literal brace, double it. A single brace always starts or ends an expression.
Before Python 3.12 you could not reuse the same quote type inside the braces, which forced code like f"{d['key']}". Since Python 3.12 (PEP 701) f-strings are parsed by the normal grammar, so the same quote is allowed. The expression can also contain backslashes, comments and span several lines.
If your code must run on 3.11 or older, keep using different quote types for the outer string and the inner expression.
Triple-quoted f-strings keep line breaks and evaluate expressions on every line. To avoid manual \n you can also concatenate adjacent literals.
An rf"..." string combines raw text with expressions, which is useful for patterns: rf"\b{re.escape(word)}\b". Remember to double braces there if the regular expression itself uses quantifiers such as {2}: write {{2}}.
F-strings are evaluated immediately, so they are not the best fit in every case:
logger.info("user %s logged in", user) so the message is only built when that log level is enabled.str.format() or string.Template can take values later.Python 3.14 adds template strings (t-strings, PEP 750) with a t prefix. They look like f-strings but produce a template object that libraries can process, for example to escape HTML or SQL safely. They do not replace f-strings for plain output.
Python 3.6 or later. The = specifier needs 3.8, and nested same-type quotes need 3.12.
Usually yes, because the expressions are compiled into direct operations. The difference is small, so choose f-strings mainly for readability.
Yes. Nest braces: f"{value:{width}.{precision}f}" reads width and precision from variables.
Use \n in the literal text. Before Python 3.12, a backslash was not allowed inside the braces, so assign nl = "\n" first if you must support older versions.
An f-string starts with f and evaluates the expressions in {} at runtime. Use = for quick debugging, !r for the repr() form, and :spec for alignment, padding and number formats. Double the braces to print them, rely on the 3.12 rules for nested quotes only when you control the Python version, and prefer parameterized queries and lazy logging over f-strings for SQL and logs.