Why would we need to store the result of lower( ) back into the same variable?


#1

Question

Why would we need to store the result of lower( ) back into the same variable?

Answer

f we want to change the actual value stored in the variable, rather than just performing the lower() method and moving on, we need to store that result back into the same variable. Take a look at the code below and try it out if you like:

my_string = “EVOLVING STRING!”
my_string.lower()
print my_string()  # This will still print the original, all-caps string!

This is an important lesson to learn early on, because it’ll save you from headaches later. Most methods do not change the data in memory, they make a copy and do something with that.
That’s why we have to assign the value of the altered copy to overwrite what’s currently in the variable. Otherwise it just runs the method’s code, alters a copy, and moves on to the next line in the program.


FAQ: Learn Python - Pyglatin - Word Up