Krishna Kumar Krishna Kumar - 1 year ago 41
Python Question

What does {} mean in python and why do we use it?

i'm new to python and i want to know what does the {} (curly braces) mean in python and what are they used for ?
Currently i'm trying to figure out its meaning.
thank you in advance.

'{2}, {1}, {0}'.format('a', 'b', 'c')

here what does it signifies?

Answer Source

It depends (like almost always) on the context.

There is the literal syntax for creating dictionary and sets:

adict = {1: 1, 2: 2} # Creating a dictionary
aset = {1, 2, 3}     # Creating a set

and in strings they represent placeholder that can be filled with format:

astring = '{0}, {1}, {a}'
formattedstring = astring.format(100, 200, a=50)
# '100, 200, 50'

these will be replaced by the arguments given to format. {0} by the first argument (100), {1} with the second argument (200), {a} with the keyword argument a (50).

If you want to know more, here are two links to the Python documentation: