首页 > 解决方案 > 打印格式化列表(Python)

问题描述

如何打印列表并在每个元素后换行?例如,我想要这个列表: [1, 2, 3]

进行格式化,以便当我运行 print(list) 时,输出为:

[

'1'

'2'

'3'

]

我如何做到这一点?

标签: python

解决方案


使用 json 模块,您可以使用缩进转储列表(任何 json 对象)。

import json

print(json.dumps(["1", "2", "3"], indent=1))

输出:

[
 "1",
 "2",
 "3"
]

推荐阅读