What does it mean by rebinding a string object raising an exception?

+1 vote
asked Sep 13, 2017 by tim

From Python in a Nutshell

String objects (byte strings, as well as text, AKA Unicode, ones) are immutable: attempting to rebind or delete an item or slice of a string raises an exception.

What does it mean by rebinding a string object raising an exception?

>>> a="hello"
>>> a="world"
>>> 

Did I just successfully rebind variable a from string object 'hello' to 'world'?

Thanks.

1 Answer

+3 votes
answered Mar 11, 2018 by rlee827

What you did was delete the "hello" string object and replace it with the "world" string object. The documentation means something like this:

>>> a="hello"
>>> a[3]="t" # does not produce helto
Traceback (most recent call last): File "<stdin>", line 1, in <module>
TypeError: 'str' object does not support item assignment
>>>
Welcome to Q&A, where you can ask questions and receive answers from other members of the community.
Website Online Counter

...