5

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)?

Kermit
  • 4,922
  • 4
  • 42
  • 74

1 Answers1

7

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)}")
AKX
  • 152,115
  • 15
  • 115
  • 172