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 (2015) Python Data Types String concatenation

In challenge task 2 of 2, step one is, graded as correct then in step two, it states that step one isn't correct.

Step one, make a variable name, make it equal to your name. name="Cheri" the check for this answer come back as correct. Step two, make a variable subject, make it equal to "Treehouse loves " and then add the variable. subject="Treehouse loves " += name Now it tells me that the step one answer is no longer correct. Why?

strings.py
name="Cheri"
subject="Treehouse loves " += name

6 Answers

Steven Parker
Steven Parker
231,271 Points

The "+=" is the addition assignment operator, you would not use it when creating a new variable but it can be handy when adding to one that already exists.

And the "+" operator is used for simple concatenation.

Here are examples of using both, the final result is the same either way:

language = "Python"
job = "I write " + language 
language = "Python"
job = "I write "
job += language
Steven Parker
Steven Parker
231,271 Points

That can be confusing until you get used to the fact that any syntax error invalidates the entire script and causes the re-validations to fail.

In this case it's caused by having two assignment operators on the second line. You probably intended to join the strings together with "+", but you have "+=" instead.

Daniel Marin
Daniel Marin
8,021 Points

Hi, It needs fixing you are using += you should use only the + sign:

name="Cheri"
subject="Treehouse loves " + name

Thank you, but the video states that we are supposed to use the += when combining. I tried just the plus and when I ask it to be checked it states that it is wrong. I guess, I'm still lost.

Ok, I just tried it again and now it says it's correct. This still leaves me confused because previously when I tried that same thing (the first go round for that challenge it kept coming back as wrong, so then I tried several variations and then rewatched the video (a few times) and that's where I saw he says it's supposed to be the += but now, it's not. So, I guess I'll go back and rewatch the video again because now I'm still confused with how it's supposed to go.

Ok, I think I understand that, thank you.