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.

Python Python Basics Types and Branching Strings and Operators

I’ve re-watched this video half a dozen times, and I still don’t understand the reassignment part.

Could someone please elaborate? I don't understand the meaning of "reassignment" the way Craig explains it in the video, especially when he reminds us that strings are impossible to modify.

Samuel Tredgett
Samuel Tredgett
11,370 Points

Assignment and reassignment are terms we use for variables when we want to give it a value.

If we assign a string a variable it'd be the first time we see that variable. Like this:

my_word = "I'm a string"

But if we wanted to change the variable, using it for a second time, we'd write:

my_word = "I'm a different string!"

Assign in this context works identically to when we would refer to someone being given a tutor. If you were going to school and they decided you needed the help, you'd be assigned a tutor. But if that first tutor left, then you're be reassigned to a new one. Does that make sense?

Edit: It's important to realise that when we 'reassign' a string in Python, we're creating a new one but with the same name. So we're re-assigning that name to a new value, while when things are not immutable we're reassigning the value to the same name. Functionally they are the same, but in computer memory these are different behaviours. For your sake as a programmer, the above should cover how this is meant in Python.

1 Answer

Andy McDonald
Andy McDonald
5,016 Points

Sam does a good job of explaining it but I'm going to go ahead and try to snake the discussion points anyways.

Consider this block of code:

first = 'this is the first string'

second = 'this is the second string'

if I then one line below this block put:

second = 'there is no string'

You have reassigned the variable to a different string. If you put print(second) it would print: there is no string. Conversely you could assign the same string to multiple other variables:

second = 'there is not string' third = second

fourth = second

All variables second, third, and third, if you printed them, they would print: there is no string