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

name = "margaret" subject = "Treehouse loves + name"

In python I want to concatenate the subject "Treehouse loves" + name, so I wrote

name = "margaret" subject = "Treehouse loves" print (subject + name)

Obviously this is wrong but I'm not sure why

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

I don't get it. Don't I have enough spaces in there? I added an extra space on both sides of the subject. So, should it be

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

6 Answers

subject = "Treehouse loves + Margaret"

I feel like a blockhead! once more

name = "Margaret" subject = "Treehouse loves" print (subject + name)

Brendan Whiting
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Brendan Whiting
Front End Web Development Techdegree Graduate 84,735 Points

Everything that's inside the quotation marks will end up as part of the string of characters that's inside the variable. The whitespace (aka spaces) outside of the quotation marks will not end up inside the variable.

So you have this variable:

name = "margaret"

If you concatenate (chain, add, attach) them together with the string "Treehouse loves" you're going to end up with the string "Treehouse lovesmargaret" because there is no space at the end of "Treehouse loves" or at the beginning of "margaret". So what you need to do is add a space into "Treehouse loves_" there where I put the underscore should be a SPACE. So the whole expression would look like this:

subject = "Treehouse loves " + name

Don't worry, once you get it, you won't forget it :)

I sure won't So... it's name = "Margaret" subject = "treehouse loves " + name

now I'm tripping up on the next challenge .format Great! I really need to look at a reference book but then I won't learn it so well as this is so tedious it's painful.

I will prevail!