educative.io

Python-String Immutability

str1 = “hello”

print(id(str1))

str1 = “bye”

print(id(str1))
Notice, when we assign a new value to str1 (at line 4 ) its identity changes not the value.

identity means memory here, the above means every time we assign a value will it create new memory?


Course: Educative: Interactive Courses for Software Developers
Lesson: Educative: Interactive Courses for Software Developers

Hello @sree_latha,
An immutable string’s value cannot be changed. So when you try to change its value, a new object is created. So yes it creates a new memory.
Hope this helps