• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / Python / What Curly Brackets Used for in Python

What Curly Brackets Used for in Python

August 29, 2022 Leave a Comment

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:
<class 'dict'>

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

s = {1, 2, 3}

print(type(s))

#Output:
<class 'set'>

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:
<class 'dict'>

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:
<class 'set'>

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.

Other Articles You'll Also Like:

  • 1.  Python isfinite() Function – Check if Number is Finite with math.isfinite()
  • 2.  How to Rotate a List in Python
  • 3.  Convert timedelta to Seconds in Python
  • 4.  Using Python to Count Number of False in List
  • 5.  Check if Variable is String in Python
  • 6.  Remove Brackets from String Using Python
  • 7.  Python issuperset() Function – Check if Set is Superset of Another Set
  • 8.  Get Username in Python using os module
  • 9.  pandas startswith() – Check if String Starts With Certain Characters
  • 10.  Python Get Operating System Information with os and platform Modules

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy