gpt4 book ai didi

python - 使用填充格式化字符串

转载 作者:太空宇宙 更新时间:2023-11-04 00:01:41 27 4
gpt4 key购买 nike

我正在尝试复制这个字符串:

enter image description here

这就是我的尝试,如您所见,值未正确对齐。我知道我需要使用某种类型的填充,但我所做的一切都失败了。

enter image description here

这是我的代码:

individual_text = '''
Highest Individual Question Scores
• Leaders
• {}{:.2f}
• {}{:.2f}
• {}{:.2f}
• Colleagues
• {}{:.2f}
• {}{:.2f}
• {}{:.2f}
Lowest Individual Question Scores
• Leaders
• {}{:.2f}
• {}{:.2f}
• {}{:.2f}
• Colleagues
• {}{:.2f}
• {}{:.2f}
• {}{:.2f}
'''.format(top_3_leader.index[0], top_3_leader.values[0],
top_3_leader.index[1], top_3_leader.values[1],
top_3_leader.index[2], top_3_leader.values[2],
top_3_colleague.index[0], top_3_colleague.values[0],
top_3_colleague.index[1], top_3_colleague.values[1],
top_3_colleague.index[2], top_3_colleague.values[2],
bottom_3_leader.index[0], bottom_3_leader.values[0],
bottom_3_leader.index[1], bottom_3_leader.values[1],
bottom_3_leader.index[2], bottom_3_leader.values[2],
bottom_3_colleague.index[0], bottom_3_colleague.values[0],
bottom_3_colleague.index[1], bottom_3_colleague.values[1],
bottom_3_colleague.index[2], bottom_3_colleague.values[2]
)

如何设置我的文本格式使其看起来像第一个图像?

最佳答案

我认为这是 strljust 方法的任务,请考虑以下示例:

x = [('text',1.14),('another text',7.96),('yet another text',9.53)]
for i in x:
print(i[0].ljust(25)+"{:.2f}".format(i[1]))

输出:

text                     1.14
another text 7.96
yet another text 9.53

也存在rjust在开头而不是结尾添加空格。

关于python - 使用填充格式化字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55541478/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com