Welcome to the Treehouse Community
The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.
Gautam Basu206 Points
I keep hearing that Strings are immutable which means they are impossible to change. What does that mean exactly?
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?
Philip Schultz11,413 Points
This 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 = "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 = "a"
This will not work because strings are immutable. I hope this helps!
What 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
variable2 assigned the same string value.
Is it possible to update the value assigned to
variable2 by only manipulating
Not in python.
Hi Guatam, in your example you are not changing the string you are changing the variable value.