from dict to string python code example

Example 1: convert dict to string python

my_dict = {"Hello" : True, "World" : False}
print(str(my_dict))

Example 2: poython str to dict

>>> D1={'1':1, '2':2, '3':3}
>>> s=str(D1)
>>> import ast
>>> D2=ast.literal_eval(s)
>>> D2
{'1': 1, '2': 2, '3': 3}

Example 3: python get ppretty string representaiton of dictinary

from pprint import pformat

# Sufficiently large to make newline character in new format
d = {"a": {'b': [1, 2, 3]}, "c": [4, 5], 'd': {'e': {'f': [6, 7, 8]},
     'g': [9, 10]}, 'h': 11, 'i': 12}
pformat(d)

Tags:

Misc Example