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

Python Python Basics Types and Branching String Methods

It says string are immutable, however you can just type quote = quote.upper() and it changes it


4 Answers

28,558 Points

Strings are immutable, but a variable holding a string is not.

The upper() method takes the string it is called on and creates a new one which it then returns. It does not modify the string directly. That is why you have to assign the result of it back into the quote variable rather than just being able to call quote.upper() on its own and have that do all the work.

So in other words that means that your code takes the string stored in the quote variable, creates an uppercase version and then assigns that uppercase version back into quote. The original string that was contained in the quote variable was technically speaking never modified, the quote variable was just changed to point to an entirely different string.

Jennifer Nordell
Jennifer Nordell
Treehouse Teacher

Hi there, Nick Evershed! This confused me too, but as I understand it now, everything in Python is an object. On a string object is a method called upper. If I'm not mistaken what is happening here, is that method is being run on the string object which creates an entirely new string object (this time with the characters capitalized), and then overwrites the old one. Essentially, you are taking the old object and running a method on it to create a new object and throwing the old object in garbage collection.

Hope this helps! :sparkles:

Joel Sprunger
Joel Sprunger
5,448 Points

Semantics. I think they just like saying the word immutable. Under the hood a new string is created and new memory is allocated to hold it. This string is given the same label as the former string, and the memory location that held the former string is marked as free.

This is different for numbers (integers, floats, etc.) For a number the number 10 requires the same memory as the number 12, so the actual value stored in memory changes. Strings can be very long or very short. It would not be useful allocating a huge chunk of memory for every "Hello, world" string.

Manal El
Manal El
3,563 Points

Everything in python is an object & those objects have methods, so when we create a variable of a string var ='hello there!' we can apply string methods such var.upper() , those string methods create another string that returns 'HELLO THERE!' , and it does not affect the original string value that the variable holds ('hello there!') , if you want to change this value to upper case you can overwrite it by the value returned by var.upper() : var = var.upper()