Skip to the solution
EasyDSA11 min read

What are f-strings?

474 views

The problem

Question presented to candidate: "What are f-strings, and why do most Python teams prefer them over % formatting and str.format? Show me a few format specs you use day to day β€” and tell me one place where you would not use an f-string."

What a strong answer should cover:

  • An f-string (formatted string literal, PEP 498, Python 3.6) is a string literal with an f prefix whose {...} fields hold Python expressions, evaluated at run time where the literal is written.
  • Field anatomy: {expression!conversion:format_spec} β€” !r/!s/!a pick repr/str/ascii, the spec after : is the format mini-language (.2f, ,, >10, 08b, %), and = (3.8) prints the expression text for debugging.
  • Each field calls format(value, spec), i.e. type(value).__format__, so types like datetime and your own classes define their own specs; braces are escaped by doubling.
  • f-strings are eager: evaluated once, immediately β€” not a reusable template, and wasted work in logging calls that are filtered out (use %s arguments there).
  • Version facts: PEP 701 (3.12) allows reusing the same quotes, backslashes and comments inside the braces; t-strings (PEP 750, 3.14) are the template version. Never build SQL or shell commands with f-strings.

Code / implementation expected: Yes β€” a few lines showing expressions, a numeric spec, !r and the = debug form.

  • fstrings

Try it yourself

Sketch your approach before you read the solution. That is what the interview tests.

Stuck? Get a hint.

A short nudge toward the approach. It will not give the answer away.

Solution

The solution is hidden so you can try first

11 min read Β· 11 sections

Run the code

Edit the code and run it here. Each console line with an expected value becomes a test.

PythonExpressions, format specs, conversions, the = debug form and nested fields
PythonEager evaluation, bytecode, __format__, logging cost and the 3.11 limits
PythonPython 3.12+ f-strings (PEP 701): same quotes, backslashes, comments
# Python 3.12+ only (PEP 701). On 3.11 this whole file is a SyntaxError.
songs = ["Take Me Back", "Rain", "Lucy"]
user = {"name": "Ada", "langs": ["Python", "Go"]}

# 1) Reuse the same quote character inside the braces.
print(f"Hello {user["name"]}, you know {", ".join(user["langs"])}")

# 2) Backslashes are allowed in the expression part.
print(f"Playlist:\n{"\n".join(f"  - {s}" for s in songs)}")

# 3) An expression may span lines and hold comments.
total = f"{
    len(songs)  # how many songs
    * 3         # minutes each
} minutes"
print(total)

# 4) Nesting depth is no longer limited by the four quote styles.
print(f"{f"{f"{f"{f"{1 + 1}"}"}"}"}")

Discussion

Other approaches, follow-ups and how it went in real interviews.

Sign in to join the discussion.

No comments yet. Share how you would answer it.

Question 22 of 139 in Python

Keep your momentum going.

Mark this question solved to track your progress, then take the next one while it is fresh.

All Python questions
Up nextEasy

What is the difference between __repr__ and __str__?

Continue