Are strings actually immutable?

I just want to comment that the lesson starts off by saying that strings are immutable, but then the solution to section 12 review involves changing a string by successively adding characters to it. That doesn’t sound very immutable to me!

Isaac, the definition of “same” or “different” has to do with the identity of an object. When a Python object is created, it resides in a certain memory address, and that address is its identity. If I claim that the object is mutable, I say that the object at that address can be changed. If immutable, it cannot be changed.

Every time you “change” a string by adding or substituting characters, you are actually creating a new string. It may well be that you assign that new string to the variable name that you were previously using, but it is a new string , i.e., a different object, residing in a different memory location, nonetheless.

By contrast, when you append an element to a list, the resulting list occupies the same memory address as the first.

(The function id() gives you an object identification (the actual memory address in the most common Python implementation)).

my_str = 'abc'
print(my_str, id(my_str))
my_str += "x"      #  same as my_str = my_str + "x". The variable name is reassigned here.
print(my_str, id(my_str))
print()
my_lst = [1,2,3]
print(my_lst, id(my_lst))
my_lst.append("x")    # "mutating" the list
print(my_lst, id(my_lst))

Output:

abc 59852000
abcx 59853792     # same variable name, different string value, different object id 

[1, 2, 3] 56919744
[1, 2, 3, 'x'] 56919744   # same variable name, different list elements, same object id 
26 Likes

Patrick, thanks for taking the time to detail how this works behind the scenes.
I hope CodeAcademy amend this bit of the course to avoid confusion, either by making explicit what you have said, or just removing the existing reference to the immutability of strings

5 Likes

I wish they would make these things more explicit, also, Isaac, but Codecademy is apparently commited, in Python, at least, to an instruction style that strongly resists looking “beneath the hood” at the way things work.

That said, one must come to terms early on with mutability vs immutability, one way or another.

4 Likes

Everything in Python is an object . You have to understand that Python represents all its data as objects. An object’s mutability is determined by its type. Some of these objects like lists and dictionaries are mutable , meaning you can change their content without changing their identity. Other objects like integers, floats, strings and tuples are objects that can not be changed.

Strings are Immutable

Strings are immutable in Python, which means you cannot change an existing string. The best you can do is create a new string that is a variation on the original.

1 Like

Patrickd314, you’re such a good answerer. Thank’s again for your great response and your time!

I understood that we can check this that new string is created with id but why the same string give different id even when we aren’t reassigning it. for example

a = "abc"
print(id(a))  // This print different value/id each time we run the program.

what does it mean…on executing each time the same string is giving different id…why so.?

1 Like

lol somehow i really like the way you reply the respond :smiley:

@abhishekk492 - Here is my unprofessional opinion:

If you are running the code within the Code Academy interface, it is quite likely that each time you press Run, it is running in a totally separate process on the Code Academy server, and therefore in a different area of memory. You might more easily replicate the examples shown if you use the interactive Python terminal on your own machine.

I’ve done that and have listed it below. In the example shown, when you see me exit the interactive Python shell and re-enter it, that is very similar to what is happening each time we press Run in the Code Academy interface.

$ python3
>>> a = "abc"
>>> print(id(a))
140116549411248
>>> print(id(a))
140116549411248
>>> exit()

$ python3
>>> a = "abc"
>>> print(id(a))
140014491795888