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

Windy Elliott
Windy Elliott
776 Points

I don't understand the .format method syntax

I keep getting syntax errors when trying to use str.format()

Here's my code name = "my name" subject = str.format("treehouse loves {name}")

strings.py

1 Answer

Hi Windy,

You want to call the .format method on that string, not pass the format string into the format method. Then you pass in your name variable as an argument to the format method. The curly braces will be replaced with the value of the name variable.

Also, treehouse should be capitalized.

subject = "Treehouse loves {}".format(name)
Windy Elliott
Windy Elliott
776 Points

well, I wasn't trying for correct grammar, but thank you. That makes more sense. If I had set the string as a variable could I have used subject = variable.format(name)?

Yes, that would work outside the challenge. This particular challenge is requiring you to do it all on one line though.

The only reason I mention capitalizing treehouse is because a lot of challenges look for exact output. You can't pass with "treehouse"