我正在尝试复制此字符串:
这就是我的尝试,您可以看到值未正确对齐。我知道我需要使用某种类型的填充,但我所做的一切都失败了。
这是我的代码:
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]
)
如何设置文本格式以使其看起来像第一张图片?
最佳答案
我认为这是 str
的 ljust
方法的任务,请考虑以下示例:
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/