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

Rafael Ruiz
Rafael Ruiz
268 Points

.format() Challenge Task 2 of 2

I am trying to figure out the correct answer under Challenge Task 2 of 2 for strings.py where I am asked to use .format() to add my name to "Treehouse loves" but am getting an error saying it is incorrect.

I have Python installed on my laptop and when I put in the following code, I get the correct answer: name = 'Rafael' subject = 'Treehouse loves {}' print(subject.format(name))

The result is Treehouse loves Rafael

So why is that code incorrect? I am not sure what the correct code is suppose to be? I want to make sure I have it correct as the course wants it written.

Thank you,

Rafael

strings.py
name = 'Rafael'
subject = "Treehouse loves {}"
print(subject.format(name))

2 Answers

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,426 Points

While the print statement produces the text you want, it does not set subject to the correct final value. instead of printing, reassign to subject:

# Correct code, but challenge wants more
name = 'Rafael'
subject = "Treehouse loves {}"
subject = subject.format(name)
# This specific challenge wants the format on the same line as the string
name = 'Rafael'
subject = "Treehouse loves {}".format(name)
Rafael Ruiz
Rafael Ruiz
268 Points

Thank you Chris, that was very helpful and exactly what Treehouse was looking for in that section.

I'm very new to programming, Python, and Treehouse and I'm trying to make it through as much as I can on my own, but your help was greatly appreciated.

Rafael Ruiz