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

Moe Myint Kyal Sin
Moe Myint Kyal Sin
Courses Plus Student 553 Points

I don't really understand of using "format" and this question.

This is the question : OK! Make a new variable named subject that concatenates (uses the + sign) the string "Treehouse loves" and your name variable. You've probably received an email or two with this subject already! this is my answer : https://teamtreehouse.com/library/python-basics/python-data-types/string-concatenation Please help me figure this out. Thank you.

name = "moe myint"
subject += "Treehouse loves {}" .format(name)

2 Answers

Ari Misha
Ari Misha
19,274 Points

Hiya Moe! Your way is the most used way and a correct way to format strings as well. But the challenge askin' ya to concat the string using good ol' "+" operator. So yeah you dont have to use "format()" method in this challenge, coz challenge explicitly asked you to add strings using "+" operator. So yeah this is how your code should look like:

name = "Moe Myint"
subject = "Treehouse loves" + " " + name

Hey Moe!

There are a few issues here. Let's understand what's going on in the second line of your code. First, when you use the += operand, you are basically saying:

subject = subject + "Treehouse loves ".format(name)

This returns an error because you haven't defined subject yet. To use the += operand, you have to define the variable first. So, yes, this code below output is correct

subject = "Treehouse loves "
subject += name

But the problem here in this question is that the task is explicitly asking you to use the + to concatenate the variable and the string. So you'll have to do something like this:

name = "moe myint"
subject = "Treehouse loves " + name