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 trialGautam Basu
206 PointsI keep hearing that Strings are immutable which means they are impossible to change. What does that mean exactly?
for example,
I say greet = "Hello"
then if I say greet = "Bye"
Value assigned to variable greet is being changed, how is the String immutable. It's confusing me. Can anyone help?
3 Answers
Philip Schultz
11,437 PointsThis really confused me at first also. Basically in your example you are reassigning the variable to a different string, not necessary changing the existing string. Notice how when you are working with a list, you can access any index and change the value that is in it. However, you cannot do that with strings. Strings are indexed liked list and you can iterate through them, but you can't change the value of a single index.
For example, This is how you can change the value of a list because it is mutable.
some_list = ["this", "is", "some", "list"]
print(some_list)
some_list[2] = "hello" # this will change the value that is in index from 'some' to list'
print(some_list)
You can try to do the same with a string, but it doesn't work For example, lets try to change the the value of index 2 in a string
some_string = "This is some string"
some_string[2] = "a"
This will not work because strings are immutable. I hope this helps!
ian izaguirre
3,220 PointsThis is a great answer, thank you!
Luis Marsano
21,425 PointsWhat does that mean exactly?
You've already said exactly what it means (impossible to change), so I'll clarify: the programming language does not allow immutable values to change. A variable is like a name tag: you can stick it on whatever you please. If I reattach my name tag onto you, though, I haven't actually changed myself into you. Whether you or I have changed (eg, get a tattoo, grow a tail) is an entirely different question; the name tag has nothing to do with it. The same distinction applies between a variable and the value assigned to it. Variables (unlike values) are names that can be (re)assigned. Can we change a value itself rather than a variable assignment?
This distinction matters when multiple variables are assigned to the same value. When a value itself changes, all variables assigned to it are still assigned to the updated value: no variable reassignments are needed to "propagate" the update. This differs from tracking down every single variable and reassigning its value.
Consider the code
variable1 = 'string'
variable2 = variable1
with both variable1
and variable2
assigned the same string value.
Is it possible to update the value assigned to variable2
by only manipulating variable1
?
Not in python.
Chris Troy
Courses Plus Student 7,395 PointsHi Guatam, in your example you are not changing the string you are changing the variable value.
azeem arsad
223 Pointsazeem arsad
223 Pointssaw your code care fully your both string are not matching with each other.