如何漂亮地打印 f 弦?

Has*_*tax 5 python pprint f-string

尝试从 f 字符串打印字典:

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}")
Run Code Online (Sandbox Code Playgroud)

输出(不漂亮):

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

有没有办法让这个在 f 字符串中工作,这样我就不必这样做pp.pprint(my_dict)

AKX*_*AKX 7

并不真地。您可以做的最接近的是 use pformat,它pprint不带print.

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