在Python中,`%f` 是一个格式化输出浮点数的占位符。当你需要在字符串中插入浮点数,并且希望控制其显示的格式时,可以使用 `%f`。
1. `%f` 原样输出,Python 3 默认小数点后保留 6 位数字(最后一位四舍五入)。
x = 3.
print('The value of x is %f' % x) 输出:The value of x is 3.
2. `%10f` 输出数字总位数为 10 位,小数点也占一位,不足的部分左侧补空格。
x = 123.
print('The value of x is %10f' % x) 输出:The value of x is123.
3. `%09.f` 输出数字总位数为 9 位,小数点后保留 1 位,不足的部分左侧补零。
x = 0.
print('The value of x is %09.f' % x) 输出:The value of x is 000.
注意:`%f` 是 `format` 函数的格式化符号之一,通常与 `print` 函数一起使用。