Do string methods permanently change the string they’re used on?

Question

Do string methods permanently change the string they’re used on?

Answer

Unless you assign the variable to be itself with a string method modifying it, then it will remain the same. Using a string method simply performs the action (upper, lower, etc.) and then moves on to the next line without changing the actual string stored in memory. For example, if you print parrot.upper( ) and then print parrot on the next line, you’ll see it’s lowercase again! However, if we actually assign parrot to the uppercase version of itself, it will make a change to the actual string stored in the variable: parrot = parrot.upper( ) followed by print parrot will show you the uppercased string.

6 Likes