DEV Community

Cover image for Python Strings & Memory: What Every Junior Developer Should Know
Aaron Rose
Aaron Rose

Posted on

Python Strings & Memory: What Every Junior Developer Should Know

You’re getting comfortable with Python. You can slice and dice strings, use .format(), and maybe even an f-string or two. But have you ever stopped to think about what’s happening under the hood when you write name = "Alice"?

Understanding a little bit about memory will make you a better programmer. It helps explain "weird" behaviors and makes you think more critically about your code. Let's break it down.

1. Variables are Labels, Not Boxes

This is the most important concept. A common beginner mental model is that a variable is a box where you put data.

my_name = "Alice"
# Imagine: [ my_name ] -> contains "Alice"
Enter fullscreen mode Exit fullscreen mode

A more accurate model is that the variable is a label or a name tag that you stick to a piece of data.

my_name = "Alice"  # Create the string "Alice" in memory, tag it `my_name`
also_my_name = my_name # Stick another tag (`also_my_name`) to the same "Alice" string.
Enter fullscreen mode Exit fullscreen mode

Why does this matter? It means both my_name and also_my_name point to the exact same string in your computer's memory. This is efficient! Python doesn't waste memory making two identical copies unless you explicitly tell it to.


2. The "Is" vs. "==" Mystery Solved

This leads directly to a classic junior-level interview question: what's the difference between is and ==?

  • == checks for equality. It asks "Do these two variables have the same value?"
  • is checks for identity. It asks "Are these two variables pointing to the exact same object in memory?"

Let's see it in action:

a = "hello"  # A string literal, hardcoded in the program
b = "hello"  # Another string literal with the same value
c = "hello world!"[:5]  # A dynamic expression that creates a new string "hello" at runtime

print(a == b) # True - same value
print(a is b) # True! Wait, why? (We'll explain this next)
print(a == c) # True - same value
print(a is c) # False! They are different objects in memory.
Enter fullscreen mode Exit fullscreen mode

So why was a is b also True? This introduces our next concept.


3. Python's Memory Trick: Interning

To save memory, Python automatically interns small, common strings (and integers). This means it creates only one copy in memory and points all variables to that single copy.

The string "hello" is short and common. When you write b = "hello", Python is smart enough to find the existing "hello" in memory and point b to it. That's why a is b is True.

The string c was created dynamically by slicing a larger string ("hello world!"[:5]). While its value is "hello", Python's interning behavior is less consistent here, so it often becomes a new, separate object in memory. Hence, a is c is False.

⚠️ The Junior Takeaway: Always use == to check string values. Interning is a memory optimization that Python uses for efficiency, but it's not a rule you should rely on for your program's logic. Only use is when you specifically mean to check if it's the same object in memory (which is a much rarer situation).


4. Why Immutability is Your Friend

Strings in Python are immutable. This is a fancy word that means "cannot be changed."

You can't do this:

my_string = "pizza"
my_string[0] = "P" # TypeError! Can't change an existing string.
Enter fullscreen mode Exit fullscreen mode

You can only create new strings:

my_string = "pizza"
my_new_string = "P" + my_string[1:] # Creates a brand new string "Pizza"
# The variable `my_string` is now reassigned to a new object.
# The old string "pizza" is eventually cleaned up by Python.
Enter fullscreen mode Exit fullscreen mode

This relates to memory because it's safe. Since strings can't change, Python can freely share them (through interning) without worrying that changing one variable will accidentally change another.


Your Quick Cheat Sheet

Concept What it Means Why a Junior Should Care
Variables as Labels Variables point to objects, they don't contain them. Explains why assigning variables doesn't copy data.
is vs. == is is identity (same object), == is equality (same value). Always use == for strings. Avoids subtle bugs.
Interning Python reuses memory for small, identical strings. It's a memory optimization, but don't rely on it for logic.
Immutability Strings cannot be changed; operations create new ones. Makes code predictable and safe for sharing data.

Thinking about memory isn't just for senior engineers. It’s the first step to writing efficient, bug-free code. Keep these concepts in mind, and you’ll already be thinking like a pro!


Aaron Rose is a software engineer and technology writer at tech-reader.blog and the author of Think Like a Genius.

Top comments (0)