JuniorCodeOccasionalNot answered yet
How do you print a literal {} inside an f-string?
An empty {} in an f-string is not allowed. Fix the line so it prints the literal text Curly brackets: {}.
print(f'Curly brackets: {}') # raises a SyntaxError as written
Write the corrected line.
Double the braces: print(f'Curly brackets: {{}}') prints Curly brackets: {}. In an f-string {{ is an escaped literal { and }} a literal }; a single {...} is a replacement field that must contain an expression, so an empty {} raises a SyntaxError at compile time.
- ✗Trying to escape braces with a backslash instead of doubling them
- ✗Thinking an empty
{}is allowed and silently skipped - ✗Confusing f-string
{{}}escaping withstr.formatdifferences
- →How would you embed
{x}literally next to an interpolatedx? - →When is the
SyntaxErrorfor an empty{}raised — at compile or run time?