Answers:
该pprint模块有一个名为命令pformat,只是这个目的。
从文档中:
以字符串形式返回对象的格式化表示形式。缩进,宽度和深度将作为格式参数传递给PrettyPrinter构造函数。
例:
>>> import pprint
>>> people = [
... {"first": "Brian", "last": "Kernighan"},
... {"first": "Dennis", "last": "Richie"},
... ]
>>> pprint.pformat(people, indent=4)
"[ { 'first': 'Brian', 'last': 'Kernighan'},\n { 'first': 'Dennis', 'last': 'Richie'}]"
假设您确实pprint从pretty-print库中获取了意思,那么您需要该pprint.pformat方法。
如果您只是想说 print,那么您想要str()