1

I have a question regarding String concept in Python. From my understanding Strings are an immutable part of the language. For instance:

a = 'Emiliano'
a[0] = 'r'
print(a)

Result:

Type Error: 'str' object does not support item assignment

So, I know that I need to create another variable, but why in the following case doesn't Python retrieve any error if I am changing the whole string of the variable?

a = 'Emiliano'
a = 'David'
print(a)

Result: David

BSMP
  • 4,596
  • 8
  • 33
  • 44
  • 1
    you aren't changing anything in existing string "Emiliano", while you are just reassigning the reference of string to another object. – SMA Apr 14 '18 at 12:55

1 Answers1

1

it's assignment, a point to the memory where Emiliano is, so a=some other string it just change the place where a point to

changing strings is mostly done by copy the origin string and manipulate the copy process, thus returning a new string object

shahaf
  • 4,750
  • 2
  • 29
  • 32