Skip to content

String Interning

Question:

What is the output of the following code?

Python
1
2
3
a = 'hello' * 2  
b = 'hello' + 'hello'  
print(a is b)

Answer Choices:

  • True
  • False
  • It Depends on the Python Version
  • It Depends on the Environment

My Answer:

The question doesn’t mention a specific Python version or execution environment, so we can’t assume either. That rules out both True and False as definitive answers.

The option "It Depends on the Python Version" is partially accurate, but it’s too narrow. Version alone doesn’t determine the result — the execution environment and CPython’s optimizations also play a major role.

➡️ Most accurate answer:
✅ It Depends on the Environment


If You’re Thinking Like a Genius, the Real Answer Is:

💡 It depends on the Python Environment, Version, and Interning Behavior.


🧠 Why? Let’s Break It Down:

🔹 Environment:

  • Script file (.py): Python will often fold constants (like 'hello'*2 and 'hello'+'hello') during compilation, especially in Python 3.7+. This leads to interned strings, and a is b could be True.

  • REPL (e.g., Jupyter, Python shell, Google Colab): Expressions are evaluated at runtime, so 'hello'*2 and 'hello'+'hello' may create two different string objects, making a is b return False.

⚠️ Even in REPL, identifier-like strings (like 'hellohello') might be interned, leading to True occasionally.


🔹 Version:

  • Before Python 3.7: The peephole optimizer didn’t aggressively fold strings, so a is b was more likely False.

  • Python 3.7 and beyond: Introduction of the AST optimizer increased chances of folding expressions like 'hello' * 2 and 'hello' + 'hello' into the same object — especially in scripts — making True more likely.


🔹 Interning Behavior:

  • Python automatically interns certain strings — usually identifier-like ones (alphanumeric, no spaces or special chars).

  • Non-identifier strings (e.g., 'hello world') are less likely to be interned, especially in dynamic environments like Colab or Jupyter.

Different Python implementations (like PyPy, IronPython) may handle string interning differently — this behavior is a CPython-specific optimization, not a language guarantee.