1、格式化输出
一、整数的输出
%o —— oct 八进制
%d —— dec 十进制
%x —— hex 十六进制python
print('%o' % 20) 24 print('%d' % 20) 20 print('%x' % 20) 14
二、浮点数输出
(1)格式化输出
%f ——保留小数点后面六位有效数字
%.3f,保留3位小数位
%e ——保留小数点后面六位有效数字,指数形式输出
%.3e,保留3位小数位,使用科学计数法
%g ——在保证六位有效数字的前提下,使用小数方式,不然使用科学计数法
%.3g,保留3位有效数字,使用小数或科学计数法git
>>> 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位数后的值。spa
round()函数只有一个参数,不指定位数的时候,返回一个整数,并且是最靠近的整数,相似于四舍五入,当指定取舍的小数点位数的时候,通常状况也是使用四舍五入的规则,可是碰到.5的状况时,若是要取舍的位数前的小数是奇数,则直接舍弃,若是是偶数则向上取舍。code
注:“.5”这个是一个“坑”,且python2和python3出来的接口有时候是不同的,尽可能避免使用round()函数吧orm
>>> 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 >>>
三、字符串输出
%s
%10s——右对齐,占位符10位
%-10s——左对齐,占位符10位
%.2s——截取2位字符串
%10.2s——10位占位符,截取两位字符串blog
>>> 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
四、 其余接口
字符串格式代码以下字符串
(2)经常使用转义字符以下it
2、format用法
相对基本格式化输出采用‘%’的方法,format()功能更强大,该函数把字符串当成一个模板,经过传入的参数进行格式化,而且使用大括号‘{}’做为特殊字符代替‘%’
使用方法由两种:b.format(a)和format(a,b)。
一、基本用法
(1)不带编号,即“{}”
(2)带数字编号,可调换顺序,即“{1}”、“{2}”
(3)带关键字,即“{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
二、进阶用法
(1)< (默认)左对齐、> 右对齐、^ 中间对齐、= (只用于数字)在小数点后进行补齐
(2)取位数“{:4s}”、"{:.2f}"等
>>> print('{} and {}'.format('hello','world')) # 默认左对齐 hello and world >>> print('{:10s} and {:>10s}'.format('hello','world')) # 取10位左对齐,取10位右对齐 hello and world >>> print('{:^10s} and {:^10s}'.format('hello','world')) # 取10位中间对齐 hello and world >>> print('{} is {:.2f}'.format(1.123,1.123)) # 取2位小数 1.123 is 1.12 >>> print('{0} is {0:>10.2f}'.format(1.123)) # 取2位小数,右对齐,取10位 1.123 is 1.12
三、多个格式化
>>> 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% >>>