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

bruce leong
bruce leong
2,521 Points

Getting an error with this, not sure why.... name = "bruce" subject = "Treehouse loves {}" subject.format(name)

Hi, I'm getting an error with this exercise -

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

Why is this wrong?

strings.py
name = "bruce"
subject = "Treehouse loves {}"
subject.format("bruce")

3 Answers

Jason Anders
MOD
Jason Anders
Treehouse Moderator 145,860 Points

Hey Bruce,

There are a couple of things. You are on the right track :thumbsup: but I feel you may have misread the instructions. Remember, challenges are very specific and extremely picky.

First, the parameter for theformat() method should be passed the variable, not a hard-coded string.
Second, the instructions say to use the format() method on the string, but you are using it on the variable.

Give it another shot with this in mind. I'm sure you'll get it.

Keep coding! :) :dizzy:

AJ Salmon
AJ Salmon
5,675 Points

The challenge wants you to assign the string "Treehouse loves bruce" to the variable subject. Here, you're using .format(), but that doesn't actually change the variable subject to the desired string. So in your case, subject is still equal to "Treehouse loves {}". You can use .format() directly after your string, like this:

>>> name = 'Kevin'
>>> sentence = "{} loves tacos".format(name)
>>> sentence
>>> 'Kevin loves tacos'

That way, the format function is performed and assigned to the variable all on one line. Hope this helps!

bruce leong
bruce leong
2,521 Points

Got it! I just wasn't reading the directions correctly. Thank you everyone.