11.01 pprint 模块:打印 Python 对象
pprint 模块:打印 Python 对象
pprint 是 pretty printer 的缩写,用来打印 Python 数据结构,与 print 相比,它打印出来的结构更加整齐,便于阅读。
1import pprint
生成一个 Python 对象:
1data = (
2 "this is a string",
3 [1, 2, 3, 4],
4 ("more tuples", 1.0, 2.3, 4.5),
5 "this is yet another string"
6 )
使用普通的 print 函数:
1print data
('this is a string', [1, 2, 3, 4], ('more tuples', 1.0, 2.3, 4.5), 'this is yet another string')
使用 pprint 模块中的 pprint 函数:
1pprint.pprint(data)
('this is a string',
[1, 2, 3, 4],
('more tuples', 1.0, 2.3, 4.5),
'this is yet another string')
可以看到,这样打印出来的公式更加美观。