Questioning the "Immutability of Strings"

We know that ‘a’ and ‘A’ are not same having different ASCII value.

Immutability of Strings:
Immutable means once a string is created, it can’t be edited or modified.

a='abc'
a=a.upper()

String a changes to ‘ABC’. So the value got changed right ? Help me understand where am I missing.

Thank you.

Good question!

a = ‘abc’ and a = a.upper() are not the same variable. You can check it using the id() command. Try the following:

a = 'abc'
print(id(a))
a = a.upper()
print(id(a))

You will see that the unique identifier is different in both the cases, which means they are pointing to a different location in memory.

Good to know, thanks.
In that case, numbers are also immutable:
i=2
print(id(i))
i=20
print(id(i))

Returns different identity of the variable i.

help(id)
Mentioned that: > This is guaranteed to be unique among simultaneously existing objects.

(CPython uses the object's **memory address**.)

So basically variables are pointers in CPython ?

Thank you.

Variables are not pointers . When you assign to a variable you are binding the name to an object. From that point onwards you can refer to the object by using the name, until that name is rebound.

Please go through the below discussion to know more about the object model of Python:

Can you please simplify it? As I’m not much versed with these lingos.

Thank you.

This can be summed up as follows:

Python does not use pointers. Variables are objects in Python.