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

Peter Kerre
Peter Kerre
280 Points

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

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

strings.py
name = "Peter"
subject = "Treehouse " + "loves " + " {} "
print(subject.format(name))

When I test from a Python App on my phone , my answer code passes but in the treehouse class it is being failed

[MOD: edited code block -srh]

3 Answers

Hi Peter,

Try:

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

This assigns a string to the name variable. Then, we create a subject variable that contains the "Treehouse loves {}" piece. After that we use the format method to insert the contents of name where the curly braces are within the subject string.

Does that make sense?

Steve.

I'm not exactly sure why the premise behind your method doesn't work - but the multiple concatenations add nothing to the problem being solved. Also, you aren't assigning the final string back into subject - the problem is not asking for a printed output. Messing with your code gives:

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

This code fails but the output from this looks fine (if you remove the trailing space(s)) so I suspect the test is looking for something too specific. The {} must need to be live, perhaps?

Simplifying further:

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

This also fails. But taking the next logical step and removing the unnecessary subject = subject gives you:

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

That's where the challenge was wanting you to end up so, unsurprisingly, it works!!

Steve.

The Treehouse lesson is expecting your code to return "Treehouse loves Peter" but it's seeing "Treehouse loves Peter"

You've got a space after "loves " and two more spaces in " {} " so you're getting back too many spaces in "loves Peter ". That's not what the quiz program is expecting.

I tried messing with that but couldn't get it past the compiler even with edited spaces. Can you?

I went back to that exercise and tried this...

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

...and it was accepted. Although...

name = "Bob"
subject = "Treehouse loves " + name

...is much simpler, and was also accepted.

Interesting! That definitely didn't work yesterday - very odd!

:+1: