Why Python Strings Are Allocated on the Stack

pythonstrings

According to this Python code visualizer, Python strings are allocated on the stack and not on the heap.

Why is this? I thought they would be similar to Java where Strings are allocated on the heap.

Best Answer

That visualizer is not showing the string data on the stack. It is showing the local references to the heap data as part of the call stack. This is very similar to Java where String references are local variables that point to actual String objects on the heap.

The visualizer is free to make any kind of representation simplification it cares to. It does not imply that "Python strings are allocated on the stack" in any given implementation of Python.

Related Topic