虽然我插入了不同的字符串,str.format打印相同的字符串

2024-10-08 18:25:18 发布

您现在位置:Python中文网/ 问答频道 /正文

我想用特定的对齐方式在一行中打印不同的变量。但是,str.format打印相同的字符串

Here is the code what i did and output

>>> print("{0:^20} {0:^20} {0:^40}\n".format('chkitem', 'Count', 'Coordinate'))
      chkitem              chkitem                        chkitem       

     

我想打印

>>> print("{0:^20} {0:^20} {0:^40}\n".format('chkitem', 'Count', 'Coordinate'))
      chkitem              Count                        Coordinate       

有什么问题


Tags: andthe字符串formatcoordinatehereiscount
2条回答
print("{:^20} {:^20} {:^40}\n".format('chkitem', 'Count', 'Coordinate'))

冒号前的零指定使用零索引格式参数,如果省略它们,将默认使用提供的下一个格式参数

您还可以指定0:1:2:等等,但最佳实践是,如果要多次打印参数,则只指定索引

{0:^20}-0是项在格式参数中的位置。你必须增加它

print("{0:^20} {1:^20} {2:^40}\n".format('chkitem', 'Count', 'Coordinate'))

相关问题 更多 >

    热门问题