What is String Interning in Python: Explanation and Example
string interning is a method of storing only one copy of identical immutable strings to save memory and speed up comparisons. When strings are interned, Python reuses the same object for equal strings instead of creating new ones.How It Works
Imagine you have many copies of the same book in a library. Instead of keeping each copy separately, you keep just one and let everyone read from it. String interning in Python works similarly by storing only one copy of each unique string in memory.
This means when Python creates a string, it checks if an identical string already exists. If it does, Python uses the existing string object instead of making a new one. This saves memory and makes comparing strings faster because Python can just check if two variables point to the same object.
Python automatically interns some strings, like short strings or identifiers, but you can also manually intern strings using the sys.intern() function.
Example
This example shows how two identical strings can share the same memory address when interned, making comparison faster.
import sys str1 = 'hello world' str2 = 'hello world' # Without interning print(str1 is str2) # Might be True or False depending on Python optimization # Using sys.intern to force interning str3 = sys.intern('hello world') str4 = sys.intern('hello world') print(str3 is str4) # Always True because both refer to the same interned string
When to Use
Use string interning when you have many identical strings in your program and want to save memory or speed up string comparisons. This is common in programs that process lots of text data, like compilers, interpreters, or data analysis tools.
Interning is especially helpful when strings are used as keys in dictionaries or sets, because Python can quickly check if keys are the same by comparing their memory addresses instead of their content.
Key Points
- String interning stores one copy of identical strings to save memory.
- Python automatically interns some strings like short or identifier strings.
- You can manually intern strings using
sys.intern(). - Interned strings speed up equality checks by comparing memory addresses.
- Useful in programs with many repeated strings or heavy dictionary/set usage.