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 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.

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.

2 Answers

Andy McDonald
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Andy McDonald
Python Development Techdegree Graduate 13,801 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

I like to think of the old vhs or tape recording days:-

Imagine one tape is a variable
you record the a-team on that tape so that one tape now contains the a-team
tape_one = "a-team"
But say you record another tv program over the top of it say knightrider
so now tape_one = "knightrider"
That is the basic principle behind reassignment.