Skip to content

prefer-fstring-over-concat

Prefer f-strings for short human-readable interpolation when they make the result clearer.

Why

For prose-like strings, f-strings keep interpolated values and surrounding text visible together.

Fix

Use one f-string when it preserves the required conversion and operator semantics. Preserve an explicit str(value) conversion as {value!s} and repr(value) as {value!r} when those conversion semantics matter. Retain required custom concatenation or formatting behavior; a str annotation alone does not prove equivalence.

Examples

Before — flagged Known string is joined to literals
src/render.py
def greeting(name: str) -> str:
return "Hello, " + name + "!"
After — preferred Interpolation uses an f-string
src/render.py
def greeting(name: str) -> str:
return f"Hello, {name}!"