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

Jason Cohen
Jason Cohen
232 Points

Hi not sure what im missing here, any help on where im going wrong would be appreciated.

its a formating issue, i just cant see the errors. thanks

strings.py
name = "Jason"
subject = "Treehouse loves {}.format()"
Jason Cohen
Jason Cohen
232 Points

question to answer was...

OK, now use .format() on the string "Treehouse loves {}" to put your name into the placeholder. Assign this to the variable subject (so start with subject =). You do not need to print() anything.

3 Answers

Greg Kaleka
Greg Kaleka
39,021 Points

Hi Jason,

format() is a method you call on a string. In your code, you're actually including "format()" as part of the string literal, so python doesn't even see it as a method. All you have to do is move your closing quote:

"Treehouse loves {}".format()

Then for the format method to do anything, you have to pass it a number of arguments equal to the number of {} pairs you have inside your string. In this case, just pass it the name variable.

Let me know if that makes sense!

Cheers :beers:

-Greg

Jason Cohen
Jason Cohen
232 Points

Thanks Greg

its part of the test so I have to do it in the way they want, but thanks for the cheat idea :-) maybe in the future when i get past the basics :-)

Have i got it right here please?

workspace is giving me this error: Bummer! Be sure to use the {} placeholder and the .format() method.

name = "Jason"
loves = "Treehouse loves{}.format(name)"
subject = loves + name
Greg Kaleka
Greg Kaleka
39,021 Points

Not yet; simplify.

You don't need to create a loves variable, and there's no need to do any string addition. You can create the subject in one step.

You're still using the .format inside the string. Don't do that. See in my answer above how I'm calling .format on the string itself?

Jason Cohen
Jason Cohen
232 Points

Thanks Greg for your help. Helped tremendously :-)

I finally got it.

no need for 'loves' =

It was just getting things in order. Learned a lot.

Final output was..

name = "Jason"
subject = "Treehouse loves {}".format(name)
Greg Kaleka
Greg Kaleka
39,021 Points

Perfecto!

Glad you figured it out on your own. Hope it wasn’t too annoying that I didn’t just give you the answer :blush:

Jason Cohen
Jason Cohen
232 Points

Am in any closer?...

name = "Jason"
loves = "Treehouse loves{}.format(name)"
subject = loves + name
Greg Kaleka
Greg Kaleka
39,021 Points

Sort of! You're now passing name into the method, although again it's not being called because it's inside the string. You're also close here in that you could cheat and use string addition by doing this:

name = "Jason"
loves = "Treehouse loves "
subject = loves + name

But we're trying to learn string formatting here, so no cheating :blush:

Jason Cohen
Jason Cohen
232 Points

Hi Greg,

Im glad you didn't - process of learning this new foreign language, It feels like im learning Japanese but 10 times harder,

Frustrating as hell, but getting there bit by bit. :-)

Considering I didnt know anything a week ago, ive come a long way.

Hopefully there will be a tipping point where it all comes together and makes sense :-)

Cheers

Jason

Greg Kaleka
Greg Kaleka
39,021 Points

Awesome! That's a great attitude to have.

I've said this elsewhere, but frustration is a part of programming at ALL levels. You'll definitely have some breakthroughs and many tipping points you cross along the way, but you will never stop being frustrated at least part of the time when programming. You just start dealing with more and more complexity. Watch any of the videos in Kenneth's live coding series, and you'll get a taste of what I mean.

If you learn to love the frustration because you're addicted to that awesome feeling when you finally crack a problem, then you've found the right field :blush:

Embrace the journey!