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

Niels Daniel Knudsen
Niels Daniel Knudsen
1,297 Points

I am using the `{}` placeholder and the `.format()` method. but it will not work. Where am I wrong?

I works in idle

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

3 Answers

Jason Anders
MOD
Jason Anders
Treehouse Moderator 145,860 Points

Niels Daniel Knudsen

To provide you with a why your code didn't work...

Both snippets of code are correct in syntax, but the first one fails the challenge because it is not what the challenge is asking for. The instructions specifically say to apply the format() to the string, but you applied it to the variable. So, while this technically works, it isn't what is asked, and it can also lead to very confusing code. When using format(), it is always best to directly use it where the formatting is being applied.

William Möller, thank you for participating in the Community. :thumbsup: If you could, however, in the future, provide an explanation with your answer. "Copy / Paste" answers with no explanations as to "why" don't provide a strong learning experience, so is not really an accepted practice in the Community.

Keep Coding! :) :dizzy:

William Möller
William Möller
622 Points

Sorry about that, next time i wont just give a straight answer. Anyways thank you for the tip it really helped me think more about how i give people "help". So thank you! :)

name = "jacques" subject = "Treehouse loves {} ".format(name) I test my code using the print function on the python shell, it works fine, but according to workspace it's not the correct answer

it works fine on workspace

name = "jacques"

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