pythonf-stringpprint

How to prettyprint f-Strings?


Attempting to pprint a dictionary from an f-String:

import pprint as pp

my_dict = {
    "key_a": ["here", "are", "the", "things"]
    , "key_b": ["that", "i", "want"]
    , "key_b": ["to", "share", "with", "the", "worlddddddddddddddddddddd"]
}

pp.pprint(f"Let's talk about all of these things:\n\n{my_dict}")

Output (not ppretty):

("Let's talk about all of these things:\n"
 '\n'
 "{'key_a': ['here', 'are', 'the', 'things'], 'key_b': ['to', 'share', 'with', "
 "'the', 'worlddddddddddddddddddddd']}")

Is there a way to make this work in f-strings, so I don't have to pp.pprint(my_dict)?


Solution

  • Not really. The closest you can do is use pformat, which is pprint without the print.

    print(f"Let's talk about all of these things:\n\n{pp.pformat(my_dict)}")