1. Formatted output
1. Integer output
%o - oct octal
%d - dec decimal
%x - hex hex
usage:
>>> print('%o' % 20)
24
>>> print('%d' % 20)
20
>>> print('%x' % 20)
14
Incorrect usage:
>>> print('%o',20)
%o 20
2. Floating point output
(1) Formatted output
%f - retain six significant digits after the decimal point. Example: %.3f, 3 decimal places are
reserved %e - 6 significant digits after the decimal point are reserved, and the output is in exponential form. Example: %.3e, keep 3 decimal places, use scientific notation
%g - on the premise of guaranteeing six significant figures, use the decimal method, otherwise use the scientific notation method. Example: %.3g, keep 3 significant figures, use decimal or scientific notation
>>> print('%f' % 1.11) # 默认保留6位小数
1.110000
>>> print('%.1f' % 1.11) # 取1位小数
1.1
>>> print('%e' % 1.11) # 默认6位小数,用科学计数法
1.110000e+00
>>> print('%.3e' % 1.11) # 取3位小数,用科学计数法
1.110e+00
>>> print('%g' % 1111.1111) # 默认6位有效数字
1111.11
>>> print('%.7g' % 1111.1111) # 取7位有效数字
1111.111
>>> print('%.2g' % 1111.1111) # 取2位有效数字,自动转换为科学计数法
1.1e+03
A more common usage is:
print("a/b="+str("%.2f\n" %(a/b))#一定要用str,不然会提示类型不同不能相加