How to convert a dictionary to a string in Python

Convert a dictionary to a string for storage in a file or printing purposes. For example, {“a”:1} is converted to “{“a”:1}”.

Solution for How to convert a dictionary to a string in Python : You can use str() to convert a dictionary to a string Use str(object) with a dictionary as object to return a string representation of a dictionary.

Other solutions

Use json.dumps() to store a dictionary

  • This solution is preferred for storing large amounts of data. However, it requires use of the json library.
  • Further reading: See documentation for json


how-to-convert-a-dictionary-to-a-string-in-python