
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Convert dictionary object into string in Python
For data manipulation in python we may come across situation to convert a dictionary object into a string object. This can be achieved in the following ways.
with str()
In this straight forward method we simple apply the str() by passing the dictionary object as a parameter. We can check the type of the objects using the type() before and after conversion.
Example
DictA = {"Mon": "2 pm","Wed": "9 am","Fri": "11 am"} print("Given dictionary : \n", DictA) print("Type : ", type(DictA)) # using str res = str(DictA) # Print result print("Result as string:\n", res) print("Type of Result: ", type(res))
Output
Running the above code gives us the following result −
Given dictionary : {'Mon': '2 pm', 'Wed': '9 am', 'Fri': '11 am'} Type : Result as string: {'Mon': '2 pm', 'Wed': '9 am', 'Fri': '11 am'} Type of Result:
With json.dumps
The json module gives us dumps method. Through this method the dictionary object is directly converted into string.
Example
import json DictA = {"Mon": "2 pm","Wed": "9 am","Fri": "11 am"} print("Given dictionary : \n", DictA) print("Type : ", type(DictA)) # using str res = json.dumps(DictA) # Print result print("Result as string:\n", res) print("Type of Result: ", type(res))
Output
Running the above code gives us the following result −
Given dictionary : {'Mon': '2 pm', 'Wed': '9 am', 'Fri': '11 am'} Type : Result as string: {"Mon": "2 pm", "Wed": "9 am", "Fri": "11 am"} Type of Result:
- Related Articles
- Convert string dictionary to dictionary in Python
- Python Convert nested dictionary into flattened dictionary?
- Python - Convert flattened dictionary into nested dictionary
- Python - Convert a set into dictionary
- Convert two lists into a dictionary in Python
- How to convert a string to dictionary in Python?
- How I can convert a Python Tuple into Dictionary?
- Python - Convert list of nested dictionary into Pandas Dataframe
- Convert byteString key:value pair of dictionary to String in Python
- How to convert the string representation of a dictionary to a dictionary in python?
- How to convert a String representation of a Dictionary to a dictionary in Python?
- Python program to convert a list of tuples into Dictionary
- Convert a pandas Timedelta object into a Python timedelta object
- How to convert hex string into int in Python?
- How to convert a JSON string into a JavaScript object?

Advertisements