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 trialJessica Brown
228 PointsTask 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} "
name = "Jessica"
subject = "Treehouse loves Jessica "
2 Answers
james south
Front End Web Development Techdegree Graduate 33,271 Pointsyou 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
228 PointsThank 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.
Ryan Dickinson
775 PointsJessica, 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
228 PointsHi 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.
Debasis Nath
3,316 PointsDebasis Nath
3,316 Pointsyou 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.