Skip to content

Python format dictionary print | Example code

  • by

Use format() function to format dictionary print in Python. Its in-built String function is used for the purpose of formatting strings according to the position.

Python Dictionary can also be passed to format() function as a value to be formatted.


Example format dictionary print in Python

The simple example code key is passed to the {} and the format() function is used to replace the key by its value, respectively. Thus, Python “**” operator is used to unpack the dictionary.

dict1 = {"a": 100, "b": 200, "c": 300}

res = "{a} {b}".format(**dict1)


Python format dictionary print

Python format dictionary pretty

import json

d = {'a': 2, 'b': {'x': 3, 'y': {'t1': 4, 't2': 5}}}
res = json.dumps(d, sort_keys=True, indent=4)



    "a": 2,
    "b": {
        "x": 3,
        "y": {
            "t1": 4,
            "t2": 5

Do comment if you have any doubts or suggestions on this Python dictionary topic.

Note: IDE: PyCharm 2021.3.3 (Community Edition)

Windows 10

Python 3.10.1

All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.