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

Moe Myint Kyal Sin
PLUS
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.

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

2 Answers

Ari Misha
Ari Misha
19,323 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

Cheers!