Convert dictionary object into string in Python

In Python data manipulation, you may need to convert a dictionary object into a string. This can be achieved using two main approaches: the built-in str() function and the json.dumps() method.

Using str()

The most straightforward method is to apply str() by passing the dictionary as a parameter. This preserves Python's dictionary representation with single quotes ?

Example

schedule = {"Mon": "2 pm", "Wed": "9 am", "Fri": "11 am"}
print("Given dictionary:", schedule)
print("Type:", type(schedule))

# Using str()
result = str(schedule)
print("Result as string:", result)
print("Type of Result:", type(result))

The output of the above code is ?

Given dictionary: {'Mon': '2 pm', 'Wed': '9 am', 'Fri': '11 am'}
Type: <class 'dict'>
Result as string: {'Mon': '2 pm', 'Wed': '9 am', 'Fri': '11 am'}
Type of Result: <class 'str'>

Using json.dumps()

The json.dumps() method from the json module converts the dictionary into a JSON-formatted string with double quotes, making it suitable for data exchange ?

Example

import json

schedule = {"Mon": "2 pm", "Wed": "9 am", "Fri": "11 am"}
print("Given dictionary:", schedule)
print("Type:", type(schedule))

# Using json.dumps()
result = json.dumps(schedule)
print("Result as string:", result)
print("Type of Result:", type(result))

The output of the above code is ?

Given dictionary: {'Mon': '2 pm', 'Wed': '9 am', 'Fri': '11 am'}
Type: <class 'dict'>
Result as string: {"Mon": "2 pm", "Wed": "9 am", "Fri": "11 am"}
Type of Result: <class 'str'>

Comparison

Method Quote Style Best For
str() Single quotes Python-specific usage
json.dumps() Double quotes JSON format, data exchange

Conclusion

Use str() for simple Python string conversion with single quotes. Use json.dumps() when you need JSON-compatible formatting with double quotes for data exchange or API communication.

Updated on: 2026-03-15T17:58:02+05:30

410 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements