Python /格式-列表和宽度

发布于 2021-01-29 17:08:30

到目前为止,这是我的列表格式的工作方式:

print('{:>15} {:>15} {:>15}{:>15}'.format(*i_liste_l))

我对python非常陌生,我无法弄清楚如何通过动态设置动态宽度

len_total = int(len(str(mi_f)))
if len_total >= 15:
    width = int(len_total)
else: width = int(15)
print('{:>width} {:>width} {:>width}{:>width}'.format(*i_liste_l))

这可能吗 ?我是否必须设置列表中所有项目的宽度?

关注者
0
被浏览
49
1 个回答
  • 面试哥
    面试哥 2021-01-29
    为面试而生,有面试问题,就找面试哥。

    您可以如下指定宽度(通过传递关键字arguments width):

    >>> '{:>{width}} {:>{width}}'.format(1, 2, width=5)
    '    1     2'
    


知识点
面圈网VIP题库

面圈网VIP题库全新上线,海量真题题库资源。 90大类考试,超10万份考试真题开放下载啦

去下载看看