In Python, there are a few uses of curly brackets. The most common use of curly brackets is to define a dictionary.

d = { "a":1, "b":2 }

print(type(d))

#Output:

You can also use curly brackets to create a non-empty set.

s = {1, 2, 3}

print(type(s))

#Output:

One last common use of curly brackets are when using the str format() function and inserting a value.

x = 3

print("value of x is {}".format(x))

#Output:
value of x is 3

When working in different programming languages, there are many different conventions for syntax which are valuable to understand.

The use of curly brackets, or curly braces, in Python is different than other languages.

In Python, there are a few uses of curly brackets.

The most common use of curly brackets is to define a dictionary.

In Python, dictionaries are a collection of key/value pairs separated by commas.

Below is an example of how to create a dictionary in Python with curly braces.

d = { "a":1, "b":2 }

print(type(d))

#Output:

Using Curly Brackets to Create Set of Elements in Python

Another example of using curly braces is when creating a non-empty set.

In Python, sets are a collection of elements which are unordered and mutable.

Below is an example of how to create a non-empty set in Python with curly braces.

s = {1, 2, 3}

print(type(s))

#Output:

Using Curly Brackets with String format() function in Python

One final use of curly brackets in Python is with the string format() function.

You can use curly brackets to insert the value of a variable and format it with the format() function.

Below shows you how to use curly braces with format() in Python.

x = 3

print("value of x is {}".format(x))

#Output:
value of x is 3

Hopefully this article has been useful for you to learn how to use curly brackets in Python.

Categorized in:

Python,

Last Update: March 11, 2024