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

Use .format on a string. I'm not sure what the question is asking. I just did that!

The question says: OK, so now use .format() on the string "Treehouse loves {}" to put your name into the placeholder. Assign this to the variable subject again.

My answer is: name = "Sauce" subject = "Treehouse loves {}" subject.format(name)

The question is unclear and I'm not sure what else it wants me to do. It didn't say to print or anything. Can anyone re-word this question so I know what it is asking?

1 Answer

Jason Anders
MOD
Jason Anders
Treehouse Moderator 145,860 Points

Hey Greg,

You're code is almost correct. But, format() needs to be attached directly to the sting that is being formatted. Right now, you seem to have it on another line trying to format a variable (which will not work). If you attach the format method directly to the string, you'll be good to go. Below is the corrected code for your reference. Hope it all makes sense.

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

:dizzy:

nomarch
nomarch
950 Points

Thanks - was stuck on this one as well. Noob error on my part... I still didn't get it even after reading the answer a couple of times 0.o. But I'll get there :)

Thanks, Jason. I'm still just a bit confused, though. Why do I need to attach .format directly to the literal? If I assign the literal to the variable name 'subject' like: subject = "Treehouse loves {}"

Then why am I not able to just do: subject.format(name) ?

Jason Anders
Jason Anders
Treehouse Moderator 145,860 Points

Hi Greg,

format() is build into the String Object, and therefore only available to that object. Honestly, it has to do with the 'behind-the-scenes' of Python, which I'm not that familiar with. But it's like most languages, where certain types of objects (Integers, Strings, Arrays, etc) have certain classes, methods, etc that are available within them. These are there for us to use, but have rules on how and where to use them.

With that in mind, when you try to use format() on a variable, you're not actually using it on the object, as the variable only holds a reference to the actual object (and not the object itself).

I hope this helps make at little more sense, but like I said, I'm not that well versed in Python (especially its build-in functions, ect). :)