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 Formatting

why isn't it name=zack subject="Treehouse loves {}" print(subject.format(name))

can't figure it out

strings.py
name="zack"
subject="treehouse loves {}"
print(subject)

2 Answers

Hi Zack,

The first part of this challenge, you've done. That was creating a variable called name containing a string. Good work!

Next, you want to create another variable called subject which contains the text "Treehouse loves NAME", where the contents of your name variable are inserted instead of NAME. The {} help with this. They indicate the position the value held within name will be inserted. The .format() method does the inserting. So, append (insert after) the method using dot notation after your string, and pass your variable name into the method as a parameter.

That has the effect of placing the contents of name in the string where the {} braces are. It looks like:

name = "My Name"
subject = "Treehouse loves {}".format(name)

In the above, the variable subject contains "Treehouse loves My Name". That's because the variable name contains the value "My Name" and we've inserted that value inside the Treehouse loves ... string where the {} braces are. Make sense?

I hope that helps,

Steve.

format is a method of the str type, so thats probably why you have to call on the string.

Your question makes sense to students a little more advanced, but keep in mind that you are answering to beginners, and they don't have any prior experience at all with programming. You should try explaining as if you are explaining to someone brand-new to this whole programming thing.

This will enable new students to become a good programmer :thumbsup: