Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial

Zahkyi Ferguson
504 PointsWhen will strings become immutable?
As it stands now we are able to go back and edit our strings, how long will this last for?
2 Answers

Steven Parker
242,284 PointsStrings are always immutable, but the variables that hold them can be re-assigned with a new value.
For examples:
s = "string"
s = "test" # re-assignment is ok
s[2] = "x" # but this will cause an error because the string is immutable

Andy Hughes
8,479 PointsNot sure what you mean but as far as I know, strings are immutable unless you use concatenation, splitting etc, but you'd still need to create a new object from the original.
treedweller
125 Pointstreedweller
125 PointsHi, I'm having trouble understanding this response.
What is the function of
[2]
in the code you provided?
The language on this is very confusing to me, because it seems to me the claim being made is that strings cannot be changed (because they are immutable) and yet in this very lesson we see several examples of the string being changed by appending new data to it. So, the string does change. Or, by your answer, it's not that the string is changing, but rather the data within the variable that contains the string is being changed. What's the difference? I'm completely lost on the semantics here.
Steven Parker
242,284 PointsSteven Parker
242,284 Pointstreedweller — Strings can be treated as an array of individual characters, so
s[2]
would refer to the third character in a sting (of 3 or more characters). Since that line is attempting to modify the string by changing one of the characters, it fails because the string is immutable.You forgot to show an example of where you saw a string being appended, but I suspect if you look again what's probably happening is that a variable that is pointing to a string is being reassigned to a new string that is being created by appending something new to an existing string. Reassigning the variable doesn't modify the string, it just associates a new one with the variable name.
treedweller
125 Pointstreedweller
125 PointsIn the video, Craig performs the following:
dessert = "chocolate" + " and marshmallows" dessert = dessert + " and graham crackers"
Each line appending new text to the existing string. Is this not changing the string? I guess I'm just getting hung up on the semantics of saying it's the variable that points at a new string and not the string itself being changed. I've tried to think about it in a different way, in that the string itself is one whole object rather than a series of characters, so while it's possible to add things to the string, there are no reference points to change or remove a specific part of the string once it's been created. So I couldn't write something like "change every 5th word in this string to 'this word'" in code?
Would that be an accurate statement?
Although now I'm curious if it is possible to extract characters from a string. For instance, could I say something like:
If that's possible, could I extract entire words? Maybe using the space as the delimiter in a filter function (if that's a thing)?
Steven Parker
242,284 PointsSteven Parker
242,284 PointsIf you watch that part of the video where Craig shows the example:
dessert = dessert + " and graham crackers"
He also explains what it does:
So you can write code to "change every 5th word in this string to 'this word'", but it doesn't actually change the original strong. Instead, it creates a new string that has your desired contents.
Keep progressing in the lessons and later ones will show you ways to extract individual characters and whole words from strings.
treedweller
125 Pointstreedweller
125 PointsHey Steven, I really appreciate your replies! I guess what I've really been trying to figure out is why that distinction even matters, but I think I've come to understand it now based on your original comment.
s[2] = "x"
Because strings are immutable, we can't change just that one character in the string. But, since the variable can be reassigned to a new string, what we could do is extract s[0-1] and s[3*] (however that's accomplished), then concatenate a new string with the change we want to make.
So, the distinction is necessary because it emphasizes the restriction you have with manipulating strings using syntax that I'm assumimg can be used in other kinds of operations.
Do I have that right? Thanks again!