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

标签 python string format padding

我正在尝试复制此字符串:

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/

相关文章:

python - 如何在Python中循环另一个循环

python - 在 Python 中创建顺序目录

java - 有没有办法得到丢失的字符串?

arrays - 在uilabel多行swift中拆分字符串

.net - 使用 {0 :C}? 格式化小于 1.00 的值

html - 网站格式问题

python - django 模型的字段包括其他对象的属性

python - 备用 python 实现版本号是否暗示它们提供相同的语法?

javascript - 与 DOM 相关的变量

我的二进制程序中的 java.lang.NumberFormatException