Python-格式化输出
目录:
- Python-格式化输出
- 引言
- 整数
- 浮点数
- 格式化输出
- 内置函数round()
- 字符串输出
- 字符串格式代码
- 常用转义字符
- format用法
- 格式转换
引言
今天做复习题的时候格式化输出这里忘得有点干净…写一遍博客,记下来方便以后查阅也当做复习一遍啦
整数
- %o:oct八进制
- %d:dec十进制
- %x:hex十六进制
# 类似于将十进制的数进行进制转换
print('%o' % 150)
print('%d' % 150)
print('%x' % 150)
# 输出结果如下
226
150
96
浮点数
格式化输出
- %f:保留小数点后面六位有效数字
%.3f:保留三位小数位 - %e:保留小数点后面六位有效数字,指数形式输出
%.3e:保留三位小数位,使用科学计数法 - %g:在保证六位有效数字的前提下,使用小数方式,否则使用科学计数法
%.3g:保留三位有效数字,使用小数或科学计数法
print('①%f' % 1.11) # 默认保留6位小数
print('②%.1f' % 1.11) # 取1位小数
print('③%e' % 1.11) # 默认取6位小数,用科学计数法
print('④%.3e' % 1.11) # 取3位小数,用科学计数法
print('⑤%g' % 1111.1111) # 默认6位有效数字
print('⑥%.7g' % 1111.1111) # 取7位有效数字
print('⑦%.2g' % 1111.1111) # 取2位有效数字,自动转换为科学计数法
# 输出结果如下
①1.110000
②1.1
③1.110000e+00
④1.110e+00
⑤1111.11
⑥1111.111
⑦1.1e+03
内置函数round()
round() 方法返回浮点数x的四舍五入值。
语法:round( x [, n] )
参数:
- x – 数值表达式。
- n – 数值表达式,表示从小数点位数。
print(round(1.1125)) # 四舍五入,不指定位数,取整
print(round(1.1135, 3)) # 取3位小数,由于3位奇数,则会向下舍
print(round(1.1125, 3)) # 取3位小数,由于2是偶数,则向上入
print(round(1.5))
print(round(2.5))
print(round(1.675, 2))
print(round(2.675, 2))
# 输出结果如下
1
1.113
1.113
2
2
1.68
2.67
但是在实际应用中round函数并不总是符合四舍五入。如下情况是因为该函数对于返回的浮点数并不是按照四舍五入的规则来计算,而会收到计算机表示精度的影响。
>>> round(2.355, 2)
2.35
字符串输出
%s
%10s:右对齐,占位符10位
%-10s:左对齐,占位符10位
%.2s:截取2位字符串
%10.2s:10位占位符,截取两位字符串
print('%s' % 'hello world') # 字符串输出
print('%20s' % 'hello world') # 右对齐,取20位,不够则补位
print('%-20s' % 'hello world') # 左对齐,取20位,不够则补位
print('%.2s' % 'hello world') # 取2位
print('%10.2s' % 'hello world') # 右对齐,取2位
print('%-10.2s' % 'hello world') # 左对齐,取2位
# 输出结果如下
hello world
hello world
hello world (左边空白有占位符)
he
he
he (左边空白有占位符)
字符串格式代码
符号 | 说明 |
%s | 格式化字符串 |
%c | 格式化字符及其ASCII码 |
%d | 格式化十进制(整数) |
%i | 格式化整数 |
%u | 格式化无符号整数 |
%o | 格式化无符号八进制整数 |
%x | 格式化无符号十六进制整数 |
%X | 格式化无符号十六进制数(大写) |
%f | 格式化浮点数字,可指定小数点后的精度 |
%e | 用科学计数法格式化浮点数 |
%E | 作用同%e,用科学计数法格式化浮点数 |
%g | %f和%e的简写 |
%G | %f和%E的简写 |
%p | 用十六进制数格式化变量的地址 |
常用转义字符
转义字符 | 描述 |
\(在行尾时) | 续行符 |
\ | 反斜杠符号 |
\ ’ | 单引号 |
\ " | 双引号 |
\a | 响铃 |
\b | 退格(Backspace) |
\e | 转义 |
\000 | 空 |
\n | 换行 |
\v | 纵向制表符 |
\t | 横向制表符 |
\r | 回车 |
\f | 换页 |
\oyy | 八进制数yy代表的字符,例如:\o12代表换行 |
\xyy | 十进制数yy代表的字符,例如:\x0a代表换行 |
\other | 其他的字符以普通格式输出 |
format用法
格式化字符串的函数 str.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’)格式打印,值后面会有一个百分号。
>>> print('{0:b}'.format(3))
11
>>> print('{:c}'.format(20))
>>> print('{:d}'.format(20))
20
>>> print('{:o}'.format(20))
24
>>> print('{:x}'.format(20))
14
>>> print('{:e}'.format(20))
2.000000e+01
>>> print('{:g}'.format(20.1))
20.1
>>> print('{:f}'.format(20))
20.000000
>>> print('{:n}'.format(20))
20
>>> print('{:%}'.format(20))
2000.000000%
>>>
小v的第十篇…