【发布时间】:2021-11-28 11:01:22
【问题描述】:
我想知道如何在 python 中使用 f-string 或格式在打印输出的开头添加 4 个空格?
这是我现在用来打印的:
print('{:<10}'.format('Constant'),'{:{width}.{prec}f}'.format(122, width=10, prec=3))
我的输出如下所示:
Constant 122.000
但我想要的是在输出中的常量之前有 4 个空格,例如:
( 4 spaces here )Constant 122.000
有什么想法吗?非常感谢!
【问题讨论】:
-
print(' {:<10}'.format('Constant'), ...? -
我不确定将使用哪种操作系统来运行我的代码。如果我只是在开头添加 4 个空格,Linux 和 Windows 是否会使用我的代码运行相同的结果? @Matthias
-
就python打印而言,无论操作系统如何,我认为空格都应该是空格@AndrewSwainskeren