首页 › 参考手册 › 内置函数 › format()

Python format() 函数

描述

按格式说明符把值格式化为字符串,与 f-string 中冒号后的写法相同。

语法

format(value, format_spec='')

参数

参数说明
value要格式化的值
format_spec格式说明,如 .2f、>10、,

返回值

字符串

实例

print(format(3.14159, ".2f"))
print(format(1234567, ","))
print(format(0.256, ".1%"))
print(format("左", "<6") + "|", format("右", ">6") + "|")
以上实例输出结果为:
3.14
1,234,567
25.6%
左     |      右|