Python String Interning Internals — ELI5

Imagine a library where every time someone wants to read “Harry Potter,” the librarian prints a brand new copy. That’s wasteful — hundreds of identical books!

A smarter library keeps just one copy on the shelf and lets everyone borrow the same one.

Python does this with strings. It’s called string interning.

The memory problem

Your code uses the same words over and over. Variable names, dictionary keys, error messages — the word “name” might appear thousands of times. Making a separate copy each time wastes memory.

Python’s clever trick

When Python sees a string it’s seen before (especially short, simple ones), it doesn’t make a new copy. It points to the one it already has in memory. Every part of your program that uses that same string shares the exact same object.

Why this is cool

  • Less memory used, because duplicates don’t exist
  • Faster comparisons: instead of checking every character, Python checks if two variables point to the same object (one quick check vs many)

What gets interned automatically?

Python automatically interns strings that look like they could be variable names or keywords — simple strings with just letters, digits, and underscores. Long strings, strings with spaces, or strings built dynamically at runtime usually don’t get interned unless you ask.

Why should you care?

You probably don’t need to think about it day to day. But if you ever wonder why comparing strings with is sometimes works and sometimes doesn’t — interning is the reason.

One Thing to Remember

String interning is Python’s way of keeping one shared copy of frequently-used strings instead of making duplicates — it saves memory and speeds up comparisons behind the scenes.

pythonstringsinterningmemoryinternals

See Also

  • Python Rope Data Structure Learn how the rope data structure handles huge texts efficiently — like organizing a book with sticky notes instead of rewriting every page.
  • Ci Cd Why big apps can ship updates every day without turning your phone into a glitchy mess — CI/CD is the behind-the-scenes quality gate and delivery truck.
  • Containerization Why does software that works on your computer break on everyone else's? Containers fix that — and they're why Netflix can deploy 100 updates a day without the site going down.
  • Python 310 New Features Python 3.10 gave programmers a shape-sorting machine, friendlier error messages, and cleaner ways to say 'this or that' in type hints.
  • Python 311 New Features Python 3.11 made everything faster, error messages smarter, and let you catch several mistakes at once instead of stopping at the first one.