Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Python Tutorial
  • Interview Questions
  • Python Quiz
  • Python Glossary
  • Python Projects
  • Practice Python
  • Data Science With Python
  • Python Web Dev
  • DSA with Python
  • Python OOPs
Open In App
Next Article:
Convert Two Lists into a Dictionary - Python
Next article icon

Python – Convert Dictionary Object into String

Last Updated : 11 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

In Python, there are situations where we need to convert a dictionary into a string format. For example, given the dictionary {‘a’ : 1, ‘b’ : 2} the objective is to convert it into a string like “{‘a’ : 1, ‘b’ : 2}”.
Let’s discuss different methods to achieve this:

Using str

The simplest way to convert a dictionary to a string is by using the built-in function “str()” for string conversion.

Python
a = {'a': 1, 'b': 2}  b = str(a)  print(b, type(b)) 

Output
{'a': 1, 'b': 2} <class 'str'> 

Explanation: str() function converts the dictionary into its string representation.

Using json.dumps

Another method is to use the dumps function from the json module for converting a dictionary into a JSON-formatted string.

Python
import json  a = {'a': 1, 'b': 2}  b = json.dumps(a)  print(b) 

Output
{"a": 1, "b": 2} 

Explanation:

  • json.dumps converts the dictionary to a string following JSON format, useful when working with APIs or web applications.
  • It adds double quotes around keys and values if they are string values for JSON formatting.

Using repr

This method uses repr() function and converts the dictionary into a string representation that can be evaluated back into a dictionary.

Python
a = {'a': 1, 'b': 2}  b = repr(a)  print(b) 

Output
{'a': 1, 'b': 2} 

Explanation:

  • repr() function produces a detailed string representation of the dictionary similar to str.
  • Useful when we need the output as a valid Python expression.

Using String Concatenation

This method manually constructs the string representation of the dictionary by concatenating its key-value pairs.

Python
a = {'a': 1, 'b': 2}  b = "{" + ", ".join(f"'{k}': {v}" for k, v in a.items()) + "}"  print(b) 

Output
{'a': 1, 'b': 2} 

Explanation:

  • join() function Joins key-value pairs as strings to create a custom output format.
  • This method offers flexibility in creating custom formats but is less efficient than built-in functions.


Next Article
Convert Two Lists into a Dictionary - Python
author
garg_ak0109
Improve
Article Tags :
  • Python
  • Python dictionary-programs
  • python-dict
Practice Tags :
  • python
  • python-dict

Similar Reads

  • Convert nested Python dictionary to object
    Let us see how to convert a given nested dictionary into an object Method 1 : Using the json module. We can solve this particular problem by importing the json module and use a custom object hook in the json.loads() method. C/C++ Code # importing the module import json # declaringa a class class obj
    2 min read
  • Python - Converting list string to dictionary
    Converting a list string to a dictionary in Python involves mapping elements from the list to key-value pairs. A common approach is pairing consecutive elements, where one element becomes the key and the next becomes the value. This results in a dictionary where each pair is represented as a key-val
    3 min read
  • Convert JSON to dictionary in Python
    JSON stands for JavaScript Object Notation. It means that a script (executable) file which is made of text in a programming language, is used to store and transfer the data. Python supports JSON through a built-in package called JSON. To use this feature, we import the Python JSON package into Pytho
    4 min read
  • Convert Object to String in Python
    Python provides built-in type conversion functions to easily transform one data type into another. This article explores the process of converting objects into strings which is a basic aspect of Python programming. Since every element in Python is an object, we can use the built-in str() and repr()
    2 min read
  • Convert Two Lists into a Dictionary - Python
    We are given two lists, we need to convert both of the list into dictionary. For example we are given two lists a = ["name", "age", "city"], b = ["Geeks", 30,"Delhi"], we need to convert these two list into a form of dictionary so that the output should be like {'name': 'Geeks', 'age': 30, 'city': '
    3 min read
  • Convert Unicode String to Dictionary in Python
    Python's versatility shines in its ability to handle diverse data types, with Unicode strings playing a crucial role in managing text data spanning multiple languages and scripts. When faced with a Unicode string and the need to organize it for effective data manipulation, the common task is convert
    2 min read
  • Convert Lists to Nested Dictionary - Python
    The task of converting lists to a nested dictionary in Python involves mapping elements from multiple lists into key-value pairs, where each key is associated with a nested dictionary. For example, given the lists a = ["gfg", "is", "best"], b = ["ratings", "price", "score"], and c = [5, 6, 7], the g
    3 min read
  • How To Convert Generator Object To Dictionary In Python
    We are given a generator object we need to convert that object to dictionary. For example, a = (1, 2, 3), b = ('a', 'b', 'c') we need to convert this to dictionary so that the output should be {1: 'a', 2: 'b', 3: 'c'}. Using a Generator ExpressionA generator expression can be used to generate key-va
    3 min read
  • Convert a list of Tuples into Dictionary - Python
    Converting a list of tuples into a dictionary involves transforming each tuple, where the first element serves as the key and the second as the corresponding value. For example, given a list of tuples a = [("a", 1), ("b", 2), ("c", 3)], we need to convert it into a dictionary. Since each key-value p
    3 min read
  • How To Convert Python Dictionary To JSON?
    In Python, a dictionary stores information using key-value pairs. But if we want to save this data to a file, share it with others, or send it over the internet then we need to convert it into a format that computers can easily understand. JSON (JavaScript Object Notation) is a simple format used fo
    7 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences