Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Python Python Basics (2015) Python Data Types String Formatting

john herron
john herron
2,485 Points

Im on the .format() code challenge on python and Im completely lost on what to do

name = "john" subject = "treehouse loves {}" .format("john")

can anyone point out what I'm doing wrong?

strings.py
name = "john"
subject = "treehouse loves {}"
subject.format("john")

2 Answers

Hi John so for this challenge, the marker is expecting a variable name to be passed to the format method. The way you have written the code, it makes the variable 'name' redundant. When i say redundant i mean you assigned your name to the variable 'name' but then passed a static string to the format method.

so if you pass the variable name to the format method the code would pass.

name = "john"
subject = "treehouse loves {}"
subject.format(name)
Jason Anders
MOD
Jason Anders
Treehouse Moderator 145,624 Points

Hey John,

You're on the right track. There are just a couple of things.

  • When you use the format() method, it has to be used directly on the string being formatted. So, you can't use a second declaration of the variable.

  • You will want to pass in the variable name into the format method and not a hard coded string

  • Finally, and this one is just because the code checker for the challenges are very picky and specific, "Treehouse loves..." needs a capital "T".

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

Hope this helps. Keep Coding! :)