Skip to content

Python · style

prefer-fstring-over-concat

python:prefer-fstring-over-concat

Build short strings with f-strings instead of concatenating literals and known strings.

Code
SARJ068
Default
error
Fix
none
Languages
python

Why

F-strings keep interpolation and spacing visible and avoid redundant string coercion.

Fix

Replace the concatenation with one f-string; use `str.join` when many operands form a sequence.

Before / after

Executed by this rule’s unit tests.

Before

Known string is joined to literals

src/render.py · focus
def greeting(name: str) -> str:
    return "Hello, " + name + "!"

After

Interpolation uses an f-string

src/render.py · focus
def greeting(name: str) -> str:
    return f"Hello, {name}!"

Limits

  • Runtime operands must have concrete string evidence.
  • Logging, SQL, lazy strings, ORM expressions, templates, and generated or skill utility files are excluded.