python格式化输出【转】
今天写代码时,需要统一化输出格式进行,一时想不起具体细节,用了最笨的方法,现在讲常见的方法进行一个总结。
一、格式化输出
1、整数的输出
直接使用'%d'代替可输入十进制数字:
- >>> print 'i am %d years old'%25
- i am 25 years old
%x —— hex 十六进制
%d —— dec 十进制
%o —— oct 八进制
- >>> num=10
- >>> print'dec=%d, oct=%o, hex=%x'%(num,num,num)
- dec=10, oct=12, hex=a
2、浮点数输出
(1)格式化输出
正常情况下,与整数的输出一样,使用‘%f’替换,默认情况下,保留小数点后面6位有效数字,如:
- >>> f=3.1415
- >>> print 'pi=%f'%f
- pi=3.141500
- >>> f=3.141500000
- >>> print 'pi=%f'%f
- pi=3.141500
除‘%f’外,还可以用‘%e’、‘%g’表示浮点数,‘%e’用指数形式输出,‘%g’根据实际情况输出,默认情况下最多保留6位有效数字。
具体使用如下所示:
- >>> numb1=0.000033333
- >>> print '%f, %e, %g'%(numb1,numb1,numb1)
- 0.000033, 3.333300e-05, 3.3333e-05
- >>> numb2=0.3333
- >>> print '%f, %e, %g'%(numb2,numb2,numb2)
- 0.333300, 3.333000e-01, 0.3333
上图可知,‘%f’和‘%e’默认情况下都会保留小数点后面六位有效数字,‘%g’在保证六位有效数字的前提下,使用小数方式,否则使用科学计数法。
另外上述几种输出方式都可以自定义精度,
- >>> numb1=0.000003333333
- >>> print '%.3f, %.3e, %.3g'%(numb1,numb1,numb1)
- 0.000, 3.333e-06, 3.33e-06
- >>> numb3=1234567.1234567
- >>> print '%f, %e, %g'%(numb3,numb3,numb3)
- 1234567.123457, 1.234567e+06, 1.23457e+06
- >>> print '%.3f, %.3e, %.3g'%(numb3,numb3,numb3)
- 1234567.123, 1.235e+06, 1.23e+06
(2)内置round()
除了上面所示的格式化输出外,还可以使用内置函数round()控制浮点数的精度,如下图所示:
- >>> round(2.3)
- 2.0
- >>> round(2.5)
- 3.0
- >>> round(2.7)
- 3.0
- >>> round(2.567)
- 3.0
round()函数如果只有一个参数,不指定位数的时候,返回的是一个整数,而且是最靠近的整数,类似于四舍五入,当指定取舍的小数点位数的时候,一般情况也是使用四舍五入的规则,但是碰到.5的情况时,如果要取舍的位数前的小数是奇数,则直接舍弃,如果是偶数则向上取舍,如下图所示:
- >>> round(2.5555,3)
- 2.555
- >>> round(2.456,2)
- 2.46
- >>> round(2.665,2)
- 2.67
- >>> round(2.675,2)
- 2.67
- >>> round(2.655,2)
- 2.65
- >>> round(2.635,2)
- 2.63
3、字符串输出
字符串可用'%s'输出,如:
- >>> print 'i love %s'%'python'
- i love python
另外也对对字符串进行截取,只输出部分,如:
- >>> print 'i love %.2s'%'python'
- i love py
有时候在输出字符串时希望可以输出格式进行调整,此时可以利用占位符对格式进行调整,如:
- >>> print 'i love %s'%'python'
- i love python
- >>> print 'i love %.2s'%'python'
- i love py
- >>> print 'i love %3.2s'%'python'
- i love py
- >>> print 'i love %10.2s'%'python'
- i love py
- >>> print 'i love %-10.2s'%'python'
- i love py
- >>> print 'i love %-10.2s !'%'python'
- i love py !
单独采用‘%s’时,直接输出字符串,‘%10s’表示在字符串前面添加占位符,并且字符串采用右对齐的方式,‘%-10s’同样道理,不过字符串采用左对齐的方式。
- >>> fmt='%10s%10s%10s'
- >>> print fmt%('name','age','sex')
- name age sex
- >>> fmt='%-10s%-10s%-10s'
- >>> print fmt%('name','age','sex')
- name age sex
- >>> print fmt%('sqxu',25,'boy')
- sqxu 25 boy
上述为基本的格式化输出,其中
%s %c %d %i %u %o %x %X %e 浮点数格式1
%E 浮点数格式2
%f 浮点数格式3
%g 浮点数格式4
%G 浮点数格式5
%% 文字%
另外在格式转换时,可能会用到转移字符,常用的转移字符如下所示:
转义字符 \(在行尾时) \\ \' \" \a \b \e \000 \n \v \t \r \f \oyy \xyy \other
二、format()函数相对基本格式化输出采用‘%’的方法,format()功能更强大,该函数把字符串当成一个模板,通过传入的参数进行格式化,并且使用大括号‘{}’作为特殊字符代替‘%’。(1)通过位置替换
- >>> print '{0} {1}'.format('hello','world')
- hello world
- >>> print '{} {}'.format('hello','world')
- hello world
- >>> print '{0} {1} {0}'.format('hello','world')
- hello world hello
print 'i love {python}'.format(python='you')
- i love you
(3)其他使用方法如:可以指定输出长度和输出的对齐方式,其中对齐方式有一下几种:< (默认)左对齐> 右对齐
- >>> print format('string','2s')
- string
- >>> print format(3.14151617,'.5f')
- 3.14152
- >>> print '{0:>10}'.format('sqxu') #10个占位符,右对齐
- sqxu
- >>> print '{0:4.2f}'.format(3.141516)
- 3.14
- >>> print '{0:6.2f}'.format(3.141516)
- 3.14
- >>> print '{0:>6.2f}'.format(3.141516)
- 3.14
- >>> print '{1:<10},{0:<15}'.format('sqxu','USTC')
- USTC ,sqxu
- >>> print 'name={name},age={age}'.format(name='sqxu',age=25)
- name=sqxu,age=25
同上述格式化输出一样,也可以通过格式化指示符来控制格式。例如,浮点数可以被格式化为一般格式或用幂来表示。'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 '{0:c}'.format(30)
- >>> print '{0:d}'.format(3)
- 3
- >>> print '{0:o}'.format(10)
- 12
- >>> print '{0:x}'.format(30)
- 1e
- >>> print '{0:e}'.format(3)
- 3.000000e+00
- >>> print '{0:f}'.format(3)
- 3.000000
- >>> print '{0:g}'.format(3)
- 3
- >>> print '{0:n}'.format(3)
- 3
- >>> print '{0:n}'.format(3.1415)
- 3.1415
- >>> print '{0:%}'.format(3.15)
- 315.000000%
format()函数还有其他一些应用,使用起来也很方便,在此不一一赘述。转自python格式化输出 - CSDN博客 http://blog.csdn.net/wchoclate/article/details/42297173
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
2017-01-11 简析TCP的三次握手与四次分手【转】
2017-01-11 TCP协议中的三次握手和四次挥手(图解)【转】