Python strings are immutable because the language designers chose safety, hashability and simplicity over in-place editing. An immutable string can be used as a dictionary key, shared freely between variables and threads, and cached by the interpreter, with no risk that one part of a program changes text another part depends on. This answers the question behind python strings immutable behaviour, and the article also shows how to "change" a string in practice.
Examples use Python 3.13 and 3.14.
Once a str object exists, its characters cannot be modified. Assigning to an index fails, and every method that looks like a change returns a new string.
The original s is untouched; upper() built a second string. You can confirm that a new object appears with id():
A dictionary stores keys by their hash. If a key could change after insertion, its hash would change and the dictionary could no longer find it. Immutable objects have a stable hash, so strings are safe keys.
String hashes are randomised per process (hash randomisation, to resist denial-of-service attacks), so never store or compare them across runs.
Because nobody can change a string, two names can point to one object without surprises, and a string passed to a function cannot be altered by that function. Immutable objects are also naturally thread-safe, so multiple threads can read the same string without locks, including in the free-threaded build of Python 3.13 and newer.
CPython interns many identifiers and short constant strings, so equal names share one object, and the compiler can fold constants such as "a" "b". Interning would be unsafe if strings could change. Strings also store a cached hash, which makes repeated dictionary lookups cheap.
A file name or SQL fragment that has been validated cannot be modified afterwards by other code holding a reference to it. That removes a class of time-of-check to time-of-use bugs.
Since you cannot edit in place, build a new string.
| Goal | Approach |
|---|---|
| Replace text | s.replace("a", "b") |
| Change one character | s[:i] + "X" + s[i + 1:] |
| Many edits | Convert to a list, edit, join |
| Many appends | Collect pieces and call "".join(parts) |
| Mutable bytes | Use bytearray |
+= can be slowEach concatenation may copy the whole string. CPython can sometimes optimise s += t when s has no other references, but this is an implementation detail that other interpreters do not share. For building large text, append to a list and join once:
You can also write into an io.StringIO buffer when you need a file-like object.
The str type defines no __setitem__ method, which is why item assignment fails, while list does define it. You can verify this directly:
Hashing works because the value can never change after creation. Custom classes that define __eq__ without __hash__ become unhashable for the opposite reason: Python cannot assume they are stable.
Immutability has a cost: edits copy data, so editing a very large string many times is slower than editing a mutable buffer. Languages such as Java, JavaScript, C# and Go also make strings immutable for similar reasons, while C and C++ (std::string) allow mutation. Python's answer when you need mutation is explicit: lists of characters, bytearray, or io.StringIO.
No. Strings, tuples, ranges and bytes are immutable; lists and bytearrays are mutable.
s += "x" mutate the string?No. It creates a new string and rebinds s, which is why other names that referred to the old string are unaffected.
Not with str. Use a list of characters, a bytearray or io.StringIO, and convert back to str when finished.
str.replace in place?No. It returns a new string and leaves the original unchanged, so you must assign the result: s = s.replace("a", "b").
Python strings are immutable so they can be hashed, shared across variables and threads, interned and optimised without risk of hidden changes. When you need to edit text, create a new string, or collect pieces in a list and join them. Choose a mutable buffer only when profiling shows copying is a real problem.