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

Jessica Brown
Jessica Brown
228 Points

Task 2 on strings formatting

How do we use format () on the string "Treehouse loves {} " to put my name into the placeholder. Then to the variable subject? I have tried everything

example: "Treehouse loves {Jessica} "

strings.py
name = "Jessica"
subject = "Treehouse loves Jessica "
Debasis Nath
Debasis Nath
3,316 Points

you can do it by just declaring your name with variable "name". like, name="jessica" then call subject variable subject="Treehouse loves {}".format(name) you need to call the format method after you specify the subject value with a place holder inside quote marks.

2 Answers

james south
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
james south
Front End Web Development Techdegree Graduate 33,271 Points

you call the format method on a template string with the argument of what goes in the blanks in the template. here the template is 'treehouse loves {}'. to call a method on an object you put object.method(argument). so after that template string, put a dot and format ( .format()) and in the parens of the format method where the argument goes, put the variable holding your name. the format method will return the template string formatted with the argument given, in this case your name, and store that in the second variable.

Jessica Brown
Jessica Brown
228 Points

Thank you for responding to my question James. I think the problem is, that I don't understand enough of this new format method to even understand your response. I am really starting to struggle.

Jessica, Did you ever figure out what the task was actually wanting you to do? I'm stuck where you are. I think I understand how .format() works, but I can't figure out what the task actually wants.

name = 'Ryan'

subject 'Treehouse loves {}'

print(subject.format(name))

It doesn't work and doesn't say it wants anything printed anyway. I've tried a lot of things, and nothing works.

EDIT: You just use the .format() method directly on the string itself.

name = 'Ryan'

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

It would have been useful if we were told we could use .format() that way at some point before the task. I hope the rest of Treehouse is better at teaching than this considering this is the VERY beginning.

Jessica Brown
Jessica Brown
228 Points

Hi Ryan.....I didn't yet. I am glad I am not the only one feeling like this part of the course was tough to understand. I have tried everything.