# FAQ: Introduction to Strings - Strings are Immutable

This community-built FAQ covers the “Strings are Immutable” exercise from the lesson “Introduction to Strings”.

Paths and Courses
This exercise can be found in the following Codecademy content:

## Join the Discussion. Help a fellow learner on their journey.

Agree with a comment or answer? Like () to up-vote the contribution!

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

Hi. Can somebody please tell me why Python makes “Strings” immutable?
Thank you!

Why can’t we just change the typo instead of this round about way of doing it. Also there is no example of how to do this making step 2 very unclear of what is actually expected. I tried to .slice the string. Can we please have more detailed explanation of what we are doing and why. Half the time is feels like I stumbling in the dark for a solution and the other half feels like I am only given half the information and how I would actually use this and when.

1 Like

Hello @dendroman!

This exercise is meant to reinforce the fact that strings are immutable, meaning they cannot be changed.

For example, in the following, we can change a list, but not a string since strings are immutable.

``````my_list = [1, 2, 3, 4, 5]
my_list.pop()
print(my_list) # prints [1, 2, 3, 4]

my_string = "Race"
my_string[0] = "P" # results in a TypeError
``````

Although in this case, we could just change the typo, there are countless situations in which we cannot just edit a string directly in the code editor. This will become more clear as you move on to the subject of string methods.

For example, what if we had a list of full names and we wanted to replace each full name with just the first name and the last initial? If this list was long, it would be impractical to manually change each string and it would make more sense to use string slicing and concatenation to accomplish this.

This is where string mutability comes into play since we can’t change the string itself. Every time we slice or concatenate strings, or otherwise change them, we are creating a new string.

Step 2 of this exercise is asking to you replace the first letter in `first_name` with `"R"`. Since it isn’t possible to change the string itself, we need to create a new variable, `fixed_first_name`, and give it the value of `"R"` concatenated with a slice of `first_name`. This slice should include everything except the first letter since we will be replacing it with `"R"`.

3 Likes

Why is this strings are immutable assignment not showing my equal sign when I type it in the workspace?? I know it is not my keyboard because only in certain spots of the workspace will the equal sign not show. It is unfortunate that for position of my coding, the concept review will not let me progress because whenever I type my equal sign that I need it to be placed will not show up in the workflow. Just heads up. I typed my equal sign across the whole workflow and it did, ===== ====== ==== ===== ======.
Thank you.

fixed_first_name = “R” + first_name[len(first_name)-2:]

For those who are stuck on this.

fixed_first_name = “R” + first_name[-2:]

1 Like

fixed_first_name =first_name[1:] + “R”

fixed_first_name = “R” + first_name[1:]

Why would the first one be incorrect?
Would it not be the same either way?

Are the following two strings the same?

``````word = "abc"

print("Z" + word)
# "Zabc"

print(word + "Z")
# "abcZ"
``````

No they aren’t. I see my mistake. Thanks

The immutability of strings in Python is a design choice that comes with several advantages. Here are some reasons why strings are made immutable in Python:

1. Hashing:
• Strings in Python are hashable because they are immutable. Hashing is a process of mapping data of arbitrary size to fixed-size values (hash codes). Immutable objects have a constant hash value throughout their lifetime, making them suitable for use as keys in dictionaries or elements in sets.
1. Performance:
• Since strings are immutable, operations that concatenate or manipulate strings create new strings rather than modifying existing ones. This avoids the need for copying the entire string when changes are made, resulting in more efficient memory usage and potentially faster performance for certain operations.
1. Safety and Predictability:
• Immutability makes strings safer to use in situations where you want to ensure that the content of a string does not change unexpectedly. This can lead to more predictable behavior in programs, as you don’t have to worry about unintended modifications to strings.
1 Like

first_name = “Bob”

last_name = “Daily”

fixed_first_name = “R”+first_name[1:]

print(fixed_first_name)