七种常见的字符串格式化方法

1、保留小数点后两位

>>> print("{:.2f}".format(3.1415))
3.14

2、带符号保留小数点后两位

>>> print("{:+.2f}".format(-3.1415))
-3.14

3、不带小数点

>>> print('{:.0f}'.format(2.7))
3

4、整数补零,填充左边, 宽度为3

>>> print('{:0>3d}'.format(4))
004

5、以逗号分隔的数字格式

>>> print('{:,}'.format(123456789))
123,456,789

6、百分比格式

>>> print('{:.2%}'.format(0.98))
98.00%

7、指数记法

>>> print('{:.2e}'.format(123456789))
1.23e+08
原文地址:https://www.cnblogs.com/shiyixirui/p/14313809.html