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 should be the result of this?

Ok, I created the subject variable using the placeholder and then used the format method to change the placeholder to the name variable. I tried just formatting it, assigning it like subject = subject.format(name), printing it but every time it produces the error "Be sure to use the placeholder {} and the format method". What's wrong?

strings.py
name = 'Igor'

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

2 Answers

Hey Igor,

They want you to assign the formatted string to the variable. If you called the subject variable as your code is written, it would show "Treehouse loves {}".

name = "Igor"

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

Thanks, that works. I did it this way:

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

This would produce the same result and this is the way I would write a real code in terms of readability but the code checker here is too picky.

Nice, glad you got it figured it out!

Kenneth Love
Kenneth Love
Treehouse Guest Teacher

The only reason to write it in two steps like that, though, is if you need subject again later. If, as in this case, subject is only needed once, it's fine, and even preferable, to do it in a single step.

Thanks for finding time to reply me. This seemed hard to read for me because I was not used to Python syntax but after messing with it for a couple of days, it seems quite simple. You are right, it is better to keep it in a single step and this would be something like $subject = "Treehouse loves {$name}"; in PHP. But what I thought was that code checker should have accepted any approach that yields the correct output.

Kenneth Love
Kenneth Love
Treehouse Guest Teacher

Most of the time, yes, the checker only cares that you got the right output. Here, though, the point was to use .format() on a string literal so it kinda has to check for that exact usage.