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

Not sure what's wrong with my format statement

I'm not sure what's wrong with my format statement: name = "{}.format(bill)" subject = "Treehouse loves {}.format(name)"

strings.py
name = "{}.format(bill)"
subject = "Treehouse loves {}.format(name)"
Julian Perry
Julian Perry
2,186 Points

You may have overthought it with the name variable.

name = "Bill"

and then

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

Using {} in our subject variable is simply setting up a placeholder for our name variable.

Hope this helps :)

1 Answer

Hi there, format is a method (internal function) of the String class, so to use it, you put .format() on the end of the string you want to use it with, not inside it. At the moment your code just tells python to ignore it as part of the string. You need to add it after the string, then tell it what to put in the string, so

name = "{}".format("bill")

Would work.



The reason is we have our string:

"Hello"

So to add to it we'll use the .format() method:

"Hello".format()

First we give our format function a clue where to insert something:

"Hello {}".format()

Then we tell it what to insert, in this case a string

"Hello {}".format("World")