gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 08:58:35 27 4
gpt4 key购买 nike

我想在一行中以特定的对齐方式打印不同的变量。但是, 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
问题是什么?

最佳答案

print("{:^20} {:^20} {:^40}\n".format('chkitem', 'Count', 'Coordinate'))
冒号前的零指定使用零索引格式参数,如果省略它们,它将默认使用提供的下一个格式参数。
您也可以指定 0: , 1: , 2:等等,但最佳实践是仅在您想多次打印参数时才指定索引。

关于python - 虽然我插入了不同的字符串,但 str.format 打印了相同的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63681736/

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