Why are strings immutable in Python?

Beqa
4 min readMay 16, 2024

If you’re learning Python (or Java, or JavaScript), you might wonder why we can’t change strings once we create them, unlike arrays or objects. By “changing,” I mean altering the existing value, not reassigning it to a new value, which is perfectly fine.

The reason behind this is something called “String interning” (“String pool” in Java).

Now, lets dive into how it works! once again, turn on your imagination and follow me.

Imagine you’re a teacher on your first day of class. The kids are greeting you and asking for your name. After you write your name on the board, it’s their turn to share theirs. So, one by one, they go to the board and write their names. But soon, you notice that the board is filled with names, and many of them are the same — “George,” “Lisa,” and so on. It’s taking up a lot of space!

You start thinking of a better way to handle this, one that saves space on the board while still allowing all the kids to share their names. Here’s your idea: each kid goes to the board and writes their name. For example, George writes his name. The next student does the same, but if their name is also George, they just make a circle around the existing name. If their name is different, they write it down like George did.

So in the end, you got all the names and saved a lot of space by not writing the same thing over and over again. That’s how string interning works!

Whenever you create a string variable, the value goes into a place in memory called the String Constant Pool. This pool stores all the different strings declared in the code. When you create a new variable with the same value (a student with the same name), it simply points to the location of the value in the String Constant Pool.

We can test this using the id() function, which returns the memory address of the data:

As we can see, the memory address of those two variables is the same, meaning that both variables point to the same memory location.

But why is it a problem to change the string value?

Let’s return to the classroom example:

Imagine that one of the students suddenly realizes they misspelled their name and it’s not “George” but “Geordie”. If they change the name “George” on the board, all the circles are now wrong since other students are indeed named “George”. So, the best option here is for Geordie to write his actual name somewhere else on the board, right?

So what he did here is write a new name on the board instead of changing the existing one to avoid fight with the other students named George.

Now, we can understand what’s going on under the hood whenever we declare string variables with the same values. The identical ones point to the same place in memory, while the different ones point to somewhere else.

If you understand this concept in Python, you pretty much understand it in Java, JavaScript, and any other language that uses it. They all save space and reduce runtime by storing strings in a special place in memory called the String constant pool.

In conclusion, strings are immutable because more than one variable may be pointing to them. Changing a string would affect all other variables pointing to it. So, whenever we want to modify an existing string, we simply create a new string instead.

Thanks for reading the whole thing, I made it all up.

JK.

--

--