8.4. String Immutability

If an object cannot be changed, we say that it is immutable. Strings are immutable, which means we cannot change the individual characters within a string. While we can access individual characters using bracket notation, trying to change individual characters simply does not work.

Example

1
2
3
title = 'The princess Bride'

title[4] = 'P'

Console Output

Traceback (most recent call last):
line 3
title[4] = 'P'
TypeError: 'str' object does not support item assignment

On line 3, we tried to change the character at index 4 from 'p' to 'P' by using an assignment statement along with bracket notation. However, the attempt caused an error and crashed the program.

8.4.1. Reassign Variables

It is important to notice that immutability applies to string values and not string variables.

Example

We can assign a different value to a variable containing a string.

1
2
3
4
5
title = 'The princess Bride'

title = 'The Princess Bride'

print(title)

Console Output

The Princess Bride

In this example, the change made on line 3 works. The difference between this example and the first is that here we assign a new value to the variable. We are not trying to change the original string itself. If we picture a variable as a label that points to a value, the second example has the following effect:

A variable, ``title``, pointing at "The Princess Bride" with an uppercase P.

When the value of a variable storing a string is changed, the variable then points to a new value, with the old value remaining unchanged.

Note

Reassigning the value of a variable might look like we are changing the string, but we are not. Instead, we just replace the old, unchanged string with a different one.

1
2
3
4
5
6
7
8
9
pet = 'dog'
print(pet + 's')  # Create and print a new string.
print(pet)        # The original string remains unchanged.

pet = 'cat'       # Assign a new, different string to the variable pet.
print(pet)        # Print the new value. 'dog' was replaced, NOT changed.

pet += 's'        # This LOOKS like we are adding 's' to 'cat', but instead
print(pet)        # we are replacing 'cat' with the new string 'cats'.

Console Output

dogs
dog
cat
cats

8.4.2. Check Your Understanding

Question

Given pet = 'cat', why do the statements print(pet + 's') and pet += 's' NOT violate the immutability of strings?