Python 101 : How to change immutable strings


 Strings in python are immutable. Below is an example that illustrates that characteristic of strings:



We get the error mentioned above : "TypeError: 'str' object does not support item assignment".

To be able to change our string "str1", we use a string slice as below - String[start:stop] - :


The "id()" function prints the address in memory of the string "str1".


We notice that our string "str1" points to another new location - after we change its value - that contains now the string "fragon"

Comments

Leave as a comment:

Archive