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

What is the exact formatting?

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

name='kenneth' subject="treehouse loves {}" print(subject.format('name'))

subject='Treehouse loves {}'
print(subject.format(name))
Treehouse loves kenneth
This is what I typed in the emulator and it worked fine as you can see, but the quiz I think just blows. There is probably a better way to get feedback on it.

In the quiz I have tried with the print, without the print, and several variations, what the heck am I doing wrong.

strings.py
name='kenneth'
subject="treehouse loves {}"
print(subject.format('name'))

2 Answers

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,423 Points

Your code is very close. The challenge grader is very picky and wants to see the .format() on the same line as the subject string. The other issue is the format method argument. By putting name in quotes you are presenting a string. What you want is the variable name so the quotes need to be removed. Last, the print isn't necessary.

name='kenneth'
subject="treehouse loves {}".format(name)

Thanks man, I super appreciate it. It would be cool if, after you have tried x number of times, the hints became more apparent to the answer or word the question a different manner.

Chris Freeman
Chris Freeman
Treehouse Moderator 68,423 Points

Yes. the "Bummer!", is a bummer. It's the current style of most of the challenges. They are working to add more specific feedback when it's obvious. Part of the learning is investigating your own solution (like real world SW dev). It's harsh tough love. As these roads have been well traveled, there is a lot of help in the forum. Searching is bound to find others with similar issues and solutions. Once your question is posted, if posted from a challenge, a link to all related questions can be found in the "bread crumb" links at the top of this page. Check out the link above String Formatting. You'll see you are not alone. You are the 28th person to ask about this challenge since is the Python Basics course was overhauled in Dec '15.

Melanie Villela
Melanie Villela
3,048 Points

I have a question. If you wanted to print that string would the way Kenneth wrote his code be the correct way? I also wrote my code like him after watching the tutorial.

Chris Freeman
Chris Freeman
Treehouse Moderator 68,423 Points

Hi Melanie, printing is one area of Python were there are definitely more than one way to code it. The context of the code may make one way better suited than another. Which way was did you and Kenneth use? Which video?

Melanie Villela
Melanie Villela
3,048 Points

Hi Chris,

Sorry I meant Kenneth Strohm from this post lol. I did my code the exact way he did, writing the third line as: print(subject.format(name)) and received the same error message saying it was wrong. I changed the second line of code to: subject="treehouse loves {}".format(name) and it was correct but was the original way I did it with the print line also an acceptable way?

Chris Freeman
Chris Freeman
Treehouse Moderator 68,423 Points

Yes. The way you coded is also correct. The grader was looking for a specific method. There is a challenge later in the Python series where a string is used as a formatting template similar to what you and Kenneth S. have done. Keep up the good work!

Chris Freeman
Chris Freeman
Treehouse Moderator 68,423 Points

Also the print is not needed in the challenge. The grader can inspect the code and evaluate variables.