Skip to content

no-string-concat-in-loop

Avoid repeatedly growing a proven string accumulator across a loop backedge.

Why

Repeated immutable-string growth can copy the accumulated value on each iteration and become quadratic.

Fix

Collect fragments and join once, or use io.StringIO when incremental writes are required.

Examples

Before — flagged String accumulator grown on every iteration
app/render.py
def render(items):
result = ""
for item in items:
result += str(item)
return result
After — preferred Fragments joined after the loop
app/render.py
def render(items):
return "".join(str(item) for item in items)

Formerly: inefficient-string-concat-in-loop