%用法
1、整数的输出
%o
—— oct 八进制
%d
—— dec 十进制
%x
—— hex 十六进制
>>> print('%o' % 20)
24
>>> print('%d' % 20)
20
>>> print('%x' % 20)
14
2、浮点数输出
(1)格式化输出
%f
—— 保留小数点后面 六位 有效数字
%.3f
,保留 3位 小数位
%e
—— 保留小数点后面 六位 有效数字,指数形式输出
%.3e
,保留 3位 小数位,使用科学计数法
%g
—— 在保证 六位 有效数字的前提下,使用小数方式,否则使用科学计数法
%.3g
,保留 3位 有效数字,使用小数或科学计数法
>>> 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
(2)内置 round()
round(number[, ndigits])
参数:
- number - 这是一个数字表达式。
- ndigits - 表示从小数点到最后四舍五入的位数。默认值为 0。
返回值: 该方法返回 x 的小数点舍入为 n 位数后的值。
round()
函数只有一个参数,不指定位数的时候,返回一个整数,而且是最靠近的整数,类似于四舍五入,当指定取舍的小数点位数的时候,一般情况也是使用四舍五入的规则,但是碰到 .5 的情况时,如果要取舍的位数前的小数是奇数,则直接舍弃,如果是偶数则向上取舍。
注: “.5” 这个是一个 “坑”,且 python2 和 python3 出来的接口有时候是不一样的,尽量避免使用 round()
函数吧
>>> round(1.1125) # 四舍五入,不指定位数,取整
1
>>> round(1.1135,3) # 取 3 位小数,由于 3 为奇数,则向下“舍”
1.113
>>> round(1.1125,3) # 取 3 位小数,由于 2 为偶数,则向上“入”
1.113
>>> round(1.5) # 无法理解,查阅一些资料是说 python 会对数据进行截断,没有深究
2
>>> round(2.5) # 无法理解
2
>>> round(1.675,2) # 无法理解
1.68
>>> round(2.675,2) # 无法理解
2.67
>>>
3、字符串输出
%s
%10s
—— 右对齐,占位符 10 位
%-10s
—— 左对齐,占位符 10 位
%.2s
—— 截取 2 位字符串
%10.2s
—— 10 位占位符,截取 两位 字符串
>>> print('%s' % 'hello world') # 字符串输出
hello world
>>> print('%20s' % 'hello world') # 右对齐,取20位,不够则补位
hello world
>>> print('%-20s' % 'hello world') # 左对齐,取20位,不够则补位
hello world
>>> print('%.2s' % 'hello world') # 取2位
he
>>> print('%10.2s' % 'hello world') # 右对齐,取2位
he
>>> print('%-10.2s' % 'hello world') # 左对齐,取2位
he
4、 其他
(1)字符串格式代码
(2)常用转义字符
format 用法
相对基本格式化输出采用 %
的方法,format()
功能 更强大,该函数把字符串当成一个模板,通过传入的参数进行格式化,并且使用大括号 {}
作为特殊字符代替 %
位置匹配
- 不带编号,即
{}
- 带数字编号,可调换顺序,即
{1}
、{2}
- 带关键字,即
{a}
、{tom}
>>> print('{} {}'.format('hello','world')) # 不带字段
hello world
>>> print('{0} {1}'.format('hello','world')) # 带数字编号
hello world
>>> print('{0} {1} {0}'.format('hello','world')) # 打乱顺序
hello world hello
>>> print('{1} {1} {0}'.format('hello','world'))
world world hello
>>> print('{a} {tom} {a}'.format(tom='hello',a='world')) # 带关键字
world hello world
通过位置匹配
>>> '{0}, {1}, {2}'.format('a', 'b', 'c')
'a, b, c'
>>> '{}, {}, {}'.format('a', 'b', 'c') # 3.1+版本支持
'a, b, c'
>>> '{2}, {1}, {0}'.format('a', 'b', 'c')
'c, b, a'
>>> '{2}, {1}, {0}'.format(*'abc') # 可打乱顺序
'c, b, a'
>>> '{0}{1}{0}'.format('abra', 'cad') # 可重复
'abracadabra'
通过名字匹配
>>> 'Coordinates: {latitude}, {longitude}'.format(latitude='37.24N', longitude='-115.81W')
'Coordinates: 37.24N, -115.81W'
>>> coord = {'latitude': '37.24N', 'longitude': '-115.81W'}
>>> 'Coordinates: {latitude}, {longitude}'.format(**coord)
'Coordinates: 37.24N, -115.81W'
通过对象属性匹配
>>> c = 3-5j
>>> ('The complex number {0} is formed from the real part {0.real} '
... 'and the imaginary part {0.imag}.').format(c)
'The complex number (3-5j) is formed from the real part 3.0 and the imaginary part -5.0.'
>>> class Point:
... def __init__(self, x, y):
... self.x, self.y = x, y
... def __str__(self):
... return 'Point({self.x}, {self.y})'.format(self=self)
...
>>> str(Point(4, 2))
'Point(4, 2)'
通过下标或 key 匹配参数
>>> coord = (3, 5)
>>> 'X: {0[0]}; Y: {0[1]}'.format(coord)
'X: 3; Y: 5'
>>> a = {'a': 'test_a', 'b': 'test_b'}
>>> 'X: {0[a]}; Y: {0[b]}'.format(a)
'X: test_a; Y: test_b'
格式转换
b
—— 二进制。将数字以 2 为基数进行输出。c
—— 字符。在打印之前将整数转换成对应的 Unicode 字符串。d
—— 十进制整数。将数字以 10 为基数进行输出。o
—— 八进制。将数字以 8 为基数进行输出。x
—— 十六进制。将数字以 16 为基数进行输出,9 以上的位数用小写字母。e
—— 幂符号。用科学计数法打印数字。用e
表示幂。g
—— 一般格式。将数值以 fixed-point 格式输出。当数值特别大的时候,用幂形式打印。n
—— 数字。当值为整数时和d
相同,值为浮点数时和g
相同。不同的是它会根据区域设置插入数字分隔符。%
—— 百分数。将数值乘以 100 然后以 fixed-point('f') 格式打印,值后面会有一个百分号。
1 >>> print('{0:b}'.format(3))
2 11
3 >>> print('{:c}'.format(20))
4
5 >>> print('{:d}'.format(20))
6 20
7 >>> print('{:o}'.format(20))
8 24
9 >>> print('{:x}'.format(20))
10 14
11 >>> print('{:e}'.format(20))
12 2.000000e+01
13 >>> print('{:g}'.format(20.1))
14 20.1
15 >>> print('{:f}'.format(20))
16 20.000000
17 >>> print('{:n}'.format(20))
18 20
19 >>> print('{:%}'.format(20))
20 2000.000000%
21 >>>