python - 关于 python 3.3 的对齐数字

标签 python python-3.x python-3.3

千克磅

1              2.2
3              6.6
…                …
197          433.4
199          437.8

我想要显示像对齐数字那样的 s 的公斤对齐 1 3 和磅 s 对齐 2 6

代码:

print("Kilograms   Pounds")

for i in range(1,199+1,2):
    kg = i * 202
    print(i,format(kg,">15.1f"))

我该怎么办

最佳答案

还可以使用 str.format 格式化 i :

print("Kilograms   Pounds")

for i in range(1,199+1,2):
    kg = i * 202
    print('{:<3} {:>15.1f}'.format(i, kg))

关于python - 关于 python 3.3 的对齐数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20865579/

相关文章:

python - 从具有不同长度的列表生成数据框

python-3.x - 识别板上可翻转和旋转的 “default” 位置的最有效方法是什么?

Python:在创建新对象之前检查对象是否已经存在

string - PySerial 和 readline() 返回二进制字符串 - 转换为常规字母数字字符串?

python - 在其类 block 之外定义对象方法,有什么理由不这样做?

python - 使用 Google Cloud Run 时如何访问已装载的 secret ?

python - 即使在 VS Code 中运行,代码也未在 hackerrank 中运行

python - 在 Cygwin 上安装 Python 3.3

python - 使文本看起来像是在 Python Shell 中输入的

python - Python 中的 try-else 有什么用?