Python 字符串格式化
Python 字符串格式化的例子
格式化字符串常见用法
# Filename : example.py
# DateTime : 2020-07-20
# author by : www.lidihuo.com
print("i am {0},age {1}".format("tom",18))
# 保留小数点后两位
print("{:.2f}".format(3.1415926))
# 带符号保留小数点后两位
print("{:+.2f}".format(-1))
# 不带小数位
print("{:.0f}".format(2.718))
# 整数补零,填充左边, 宽度为3
print("{:0>3d}".format(5))
# 以逗号分隔的数字格式
print("{:,}".format(10241024))
# 百分比格式
print("{:.2%}".format(0.718))
# 指数记法
print("{:.2e}".format(10241024))
运行结果:
i am lidihuo,age 18
3.14
-1.00
3
005
10,241,024
71.80%
1.02e+07