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

Chris Jones
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Chris Jones
Java Web Development Techdegree Graduate 23,933 Points

Why isn't this code the correct answer?

Why isn't this code the correct answer to the quiz on Formatting Strings? I know there's a more straight forward way than this (subject = "Treehouse loves {}".format(name))), but I just wanted to try other ways of making this work. I put the code below into a workspace and it worked just fine. Are the quizzes just looking for pre-determined code and not necessarily evaluating the code for the correct string?

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

2 Answers

You are doing a great job! The only problem is that you have to call "format" directly onto the string, not onto the variable. Instead of doing two lines like this:

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

Where you called format on the subject variable, you have to it directly like this:

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

Hope that helps! :dizzy:

Chris Jones
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Chris Jones
Java Web Development Techdegree Graduate 23,933 Points

Thanks, Alexander! Why can't I use the .format() method on the variable? The variable is a string object, correct? From looking at the format method, it looks like the .format() method just needs a string as the object to manipulate, right? Maybe I'm thinking about that wrong. Here's what I found from typing help(str.format):

Help on method_descriptor:                                                                                                                           

format(...)                                                                                                                                          
    S.format(*args, **kwargs) -> str                                                                                                                 

    Return a formatted version of S, using substitutions from args and kwargs.                                                                       
    The substitutions are identified by braces ('{' and '}').                                                                                        

Thanks for your help!

Yes, you could do format on the variable, but code challenges are picky. The code challenge expects you to do it on a string.

Hope that makes sense! :dizzy:

Chris Jones
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Chris Jones
Java Web Development Techdegree Graduate 23,933 Points

Ah, yes, that makes sense. I was wondering if the code challenges' correct answers are specific code or if it evaluates the code. I guess they're just looking for specific code.

Thanks for your help, Alexander:)!

anil rahman
anil rahman
7,786 Points

I don't know python but this worked?

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

That won't work. I tried it in the code challenge. If you get rid of the

subject = "Treehouse loves {}"

Line then it will work. :dizzy: