Python String Interning — ELI5

Imagine a library that gets a donation of 50 copies of the same book. Instead of shelving all 50, the librarian keeps just one copy and puts 50 cards in the catalog that all point to the same shelf. Anyone who wants that book gets directed to the single copy.

Python does something similar with strings. When your program creates the same string in multiple places — say the word "hello" appears in ten different spots — Python sometimes stores only one actual copy in memory. Every variable that holds "hello" just points to that single copy.

This is called string interning, and Python does it automatically for strings that look like identifiers (variable names, function names, dictionary keys). Since these strings appear everywhere in code and rarely change, reusing one copy saves a lot of memory.

The bonus? Comparing two interned strings is lightning fast. Instead of checking every character one by one (“Is h equal to h? Is e equal to e?”), Python just checks if both variables point to the same place in memory. It’s like checking if two library cards have the same shelf number instead of comparing every page of the books.

You don’t need to think about this most of the time — Python handles it quietly. But knowing it exists helps explain some quirky behavior you might stumble into, like why "hello" is "hello" sometimes returns True even though is checks identity, not equality.

The one thing to remember: Python automatically reuses copies of common strings behind the scenes, saving memory and making comparisons faster — like a smart librarian who never shelves the same book twice.

pythonperformancememoryoptimization

See Also