"Strings are close enough to arrays to borrow their best tricks—but Pythonstris an immutable Unicode sequence, and that plot twist creates the famous+=performance trap. Treat it as merely a character array and it bites."
Underneath, It's an Array
Strings share sequence operations with arrays, so indexing, scanning, slicing, and two-pointer reasoning often transfer. But Python str is a Unicode sequence whose internal representation is not simply a fixed-width character array. Use the language's operation contracts rather than assuming a C-style layout.
The One Twist: Immutability
Here's where strings diverge from lists: in Python, strings are immutable. You cannot change a character in place — s[0] = 'x' is an error. Any "modification" actually builds a brand-new string. That sounds harmless until you do it in a loop, and then it becomes the single most famous performance footgun in beginner Python.
The += Trap
Repeated result += piece may rebuild and copy a growing immutable value, so the general cost model can reach O(n²). CPython can optimize some concatenations depending on reference state, but the language does not promise that behavior. Do not base a large string builder on that implementation accident.
The predictable default is to collect pieces in a list and combine them once with "".join(pieces). A handful of small concatenations is fine; for many pieces, join makes both intent and total-work reasoning clearer.
Why Make Them Immutable At All?
Immutability isn't a punishment — it buys real things. Because a string never changes, it can be hashable (so it can be a dict key or set member — the entire next track depends on this). It's safe to share between parts of a program with no fear someone mutates it underneath you. And identical strings can be interned (stored once, reused). The += trap is the price; hashable, shareable, safe strings are what you bought with it.
Pippa's Confession
report += line in a loop over thousands of rows. On the test file it was instant; on the real export it crawled for a full minute. Dad took one look: "You're rebuilding the whole report every line." I switched to a list and one join at the end — a minute became milliseconds. The lesson stuck harder than any lecture: immutability is invisible until it's quadratic.