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

What does this do again {} ?

So i am learning python but i see craig use this alot of time " {} " and as far as i remember he called it a placeholder. But what does it do?

1 Answer

Cooper Runstein
Cooper Runstein
11,850 Points

Most likely you're seeing it used in situations like this:

string = "Hello {}".format("world")

The {} serves as a placeholder for the text that you insert with the format function. So right now, if you printed out the variable string, it would return "Hello world".

string2 = "Hello {}".format("frederik")

Here string2 becomes "Hello frederik"

Kailash Seshadri
Kailash Seshadri
3,087 Points

Unless you specify world, or frederick, as a variable e.g:

world = "Bob"
string = "Hello {}".format(world)

In this case, a print (string) will output Hello Bob

NB: Notice that in the .format brackets, the word world now doesn't have a "". This is so that the format does not treat the world as a string, and instead as a variable. So:

world = "Bob"
string = "Hello {}".format("world")

In this case, a print(string) will output Hello world, because the world is now treated as a string, not as a variable.