0
0
PythonConceptBeginner · 3 min read

What is String Interning in Python: Explanation and Example

In Python, 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.

python
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
Output
False True
🎯

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.

Key Takeaways

String interning saves memory by storing only one copy of identical strings.
Python automatically interns some strings but you can manually intern others with sys.intern().
Interned strings make equality checks faster by comparing object identities.
Use interning when handling many repeated strings or optimizing dictionary/set keys.
Interning is a useful technique for improving performance in text-heavy applications.