M&C6: Misunderstanding immutability

Consider the list my_list = [1,99,10,’Bill’,’a’] and the string funny_string = “1234+PLTW?”

  • If we want to change the first element in the list from 1 to 55, we write my_list[0] = 55.
  • If we want to change the first character in string funny_string from 1 to 7 and write funny_string[0] = “7”, we get an error message.


Why? It is because strings are immutable which means they cannot be changed after they have been created.  A number of operations can be applied to strings (e.g., slicing, +, *) and those operations create new strings. They do no change existing ones.

When students start working with strings in Python, they often make the mistake of wanting to change a string by indexing. However, many will quickly realize how the other operations can be used to achieve what they want.

Why are strings and tuples immutable? The main reason lies in the performance immutability enables. Remember, strings and tuples are immutable in Python. Lists, arrays, and dictionaries are not.