F-strings
An f-string is a string literal prefixed with f or F that interpolates expressions inside {...}. It is the usual way to build a readable string from variables in modern Python.
Detailed Explanation
"Hello, " + name + "!" works and gets ugly fast. "Hello, {}!".format(name) is better. f"Hello, {name}!" is what you should write now. The f tells the parser to treat {...} as an expression whose result is inserted into the string. Added in Python 3.6, f-strings are evaluated at runtime each time the literal runs.
The braces can hold more than a name. {user.email}, {items[0]}, {cents / 100:.2f} are all legal. After a colon comes a format spec: :.2f for two decimal places, :04d for a zero-padded integer, :%Y-%m-%d for a datetime. !r calls repr, !s calls str, !a calls ascii. {name=} (3.8+) prints name='ada', which is useful in debug logs.
Expressions inside braces are regular Python expressions with a few limits. You cannot use a backslash inside the braces, so you cannot put a nested f-string that needs escapes, or a lambda that needs \ for a newline, without binding a name first. Nested quotes must differ from the outer quotes, or you use the other quote style.
F-strings are not lazy. f"user={lookup()}" calls lookup even if you never print the string. If building the message is expensive and the log level would drop it, pass a lazy format to the logger (logger.debug("user=%s", user)) instead of an f-string.
Brace escaping is {{ and }}. An f-string that must contain a literal { writes f"{{id}}" and produces {id}. Mixing this with real interpolations is readable if you keep the literal braces short.
For very large templates, or templates stored outside the code, f-strings are the wrong tool. They cannot be stored as data and filled later; the expressions are compiled into the function. Use str.format or string.Template when the template is user-supplied. Never eval an f-string-like pattern from a user.
Code Examples
name = "ada"
cents = 2500
print(f"hello {name}") # hello ada
print(f"total ${cents / 100:.2f}") # total $25.00
print(f"{name=}, {cents=}") # name='ada', cents=2500from datetime import date
shipped = date(2026, 4, 1)
print(f"{shipped:%d %b %Y}") # 01 Apr 2026
print(f"{name!r}") # 'ada' <- repr, with quotes
print(f"{{name}} is {name}") # {name} is ada# Bind first when the expression cannot live in the braces.
lines = "ada\nlinus"
print(f"first={lines.splitlines()[0]}") # first=adaCommon Gotchas
F-strings evaluate immediately
a debug f-string in a hot loop still runs the expressions when logging is disabled. Use lazy `%s` / `{}` logging for optional messages.
User input is not a format string you should trust
do not build an f-string from untrusted text. F-strings can call methods and access attributes.
No backslash inside `{...}`
`f"{hello\n}"` is a `SyntaxError`. Bind the value on the previous line.
Quotes must nest
`f"hello {title or "guest"}"` is a syntax error on older versions. Use the other quote, or bind `title or "guest"` first.