python格式化输出【转】

时间:2023-03-09 04:03:42
python格式化输出【转】
今天写代码时,需要统一化输出格式进行,一时想不起具体细节,用了最笨的方法,现在讲常见的方法进行一个总结。
一、格式化输出
1、整数的输出
直接使用'%d'代替可输入十进制数字:
  1. >>> print 'i am %d years old'%25
  2. i am 25 years old
python格式化输出【转】
%x —— hex 十六进制
%d —— dec 十进制
%o —— oct 八进制
python格式化输出【转】
  1. >>> num=10
  2. >>> print'dec=%d, oct=%o, hex=%x'%(num,num,num)
  3. dec=10, oct=12, hex=a

2、浮点数输出

(1)格式化输出
正常情况下,与整数的输出一样,使用‘%f’替换,默认情况下,保留小数点后面6位有效数字,如:
  1. >>> f=3.1415
  2. >>> print 'pi=%f'%f
  3. pi=3.141500
  4. >>> f=3.141500000
  5. >>> print 'pi=%f'%f
  6. pi=3.141500
python格式化输出【转】
除‘%f’外,还可以用‘%e’、‘%g’表示浮点数,‘%e’用指数形式输出,‘%g’根据实际情况输出,默认情况下最多保留6位有效数字。
具体使用如下所示:
  1. >>> numb1=0.000033333
  2. >>> print '%f, %e, %g'%(numb1,numb1,numb1)
  3. 0.000033, 3.333300e-05, 3.3333e-05
  4. >>> numb2=0.3333
  5. >>> print '%f, %e, %g'%(numb2,numb2,numb2)
  6. 0.333300, 3.333000e-01, 0.3333
python格式化输出【转】
上图可知,‘%f’和‘%e’默认情况下都会保留小数点后面六位有效数字,‘%g’在保证六位有效数字的前提下,使用小数方式,否则使用科学计数法。
另外上述几种输出方式都可以自定义精度,
python格式化输出【转】
  1. >>> numb1=0.000003333333
  2. >>> print '%.3f, %.3e, %.3g'%(numb1,numb1,numb1)
  3. 0.000, 3.333e-06, 3.33e-06
  4. >>> numb3=1234567.1234567
  5. >>> print '%f, %e, %g'%(numb3,numb3,numb3)
  6. 1234567.123457, 1.234567e+06, 1.23457e+06
  7. >>> print '%.3f, %.3e, %.3g'%(numb3,numb3,numb3)
  8. 1234567.123, 1.235e+06, 1.23e+06
python格式化输出【转】
(2)内置round()
除了上面所示的格式化输出外,还可以使用内置函数round()控制浮点数的精度,如下图所示:
  1. >>> round(2.3)
  2. 2.0
  3. >>> round(2.5)
  4. 3.0
  5. >>> round(2.7)
  6. 3.0
  7. >>> round(2.567)
  8. 3.0
python格式化输出【转】
round()函数如果只有一个参数,不指定位数的时候,返回的是一个整数,而且是最靠近的整数,类似于四舍五入,当指定取舍的小数点位数的时候,一般情况也是使用四舍五入的规则,但是碰到.5的情况时,如果要取舍的位数前的小数是奇数,则直接舍弃,如果是偶数则向上取舍,如下图所示:
python格式化输出【转】
  1. >>> round(2.5555,3)
  2. 2.555
  3. >>> round(2.456,2)
  4. 2.46
  5. >>> round(2.665,2)
  6. 2.67
  7. >>> round(2.675,2)
  8. 2.67
  9. >>> round(2.655,2)
  10. 2.65
  11. >>> round(2.635,2)
  12. 2.63
3、字符串输出
字符串可用'%s'输出,如:
  1. >>> print 'i love %s'%'python'
  2. i love python
python格式化输出【转】
另外也对对字符串进行截取,只输出部分,如:
  1. >>> print 'i love %.2s'%'python'
  2. i love py
python格式化输出【转】
有时候在输出字符串时希望可以输出格式进行调整,此时可以利用占位符对格式进行调整,如:
  1. >>> print 'i love %s'%'python'
  2. i love python
  3. >>> print 'i love %.2s'%'python'
  4. i love py
  5. >>> print 'i love %3.2s'%'python'
  6. i love  py
  7. >>> print 'i love %10.2s'%'python'
  8. i love         py
  9. >>> print 'i love %-10.2s'%'python'
  10. i love py
  11. >>> print 'i love %-10.2s !'%'python'
  12. i love py         !
python格式化输出【转】
单独采用‘%s’时,直接输出字符串,‘%10s’表示在字符串前面添加占位符,并且字符串采用右对齐的方式,‘%-10s’同样道理,不过字符串采用左对齐的方式。
python格式化输出【转】
  1. >>> fmt='%10s%10s%10s'
  2. >>> print fmt%('name','age','sex')
  3. name       age       sex
  4. >>> fmt='%-10s%-10s%-10s'
  5. >>> print fmt%('name','age','sex')
  6. name      age       sex
  7. >>> print fmt%('sqxu',25,'boy')
  8. 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)通过位置替换
  1. >>> print '{0} {1}'.format('hello','world')
  2. hello world
  3. >>> print '{} {}'.format('hello','world')
  4. hello world
  5. >>> print '{0} {1} {0}'.format('hello','world')
  6. hello world hello
print 'i love {python}'.format(python='you')
  • i love you
(3)其他使用方法如:
可以指定输出长度和输出的对齐方式,其中对齐方式有一下几种:
< (默认)左对齐
> 右对齐
  1. >>> print format('string','2s')
  2. string
  3. >>> print format(3.14151617,'.5f')
  4. 3.14152
  5. >>> print '{0:>10}'.format('sqxu')    #10个占位符,右对齐
  6. sqxu
  7. >>> print '{0:4.2f}'.format(3.141516)
  8. 3.14
  9. >>> print '{0:6.2f}'.format(3.141516)
  10. 3.14
  11. >>> print '{0:>6.2f}'.format(3.141516)
  12. 3.14
  13. >>> print '{1:<10},{0:<15}'.format('sqxu','USTC')
  14. USTC      ,sqxu
  15. >>> print 'name={name},age={age}'.format(name='sqxu',age=25)
  16. 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')格式打印,值后面会有一个百分号。
  1. >>> print '{0:b}'.format(3)
  2. 11
  3. >>> print '{0:c}'.format(30)
  4. >>> print '{0:d}'.format(3)
  5. 3
  6. >>> print '{0:o}'.format(10)
  7. 12
  8. >>> print '{0:x}'.format(30)
  9. 1e
  10. >>> print '{0:e}'.format(3)
  11. 3.000000e+00
  12. >>> print '{0:f}'.format(3)
  13. 3.000000
  14. >>> print '{0:g}'.format(3)
  15. 3
  16. >>> print '{0:n}'.format(3)
  17. 3
  18. >>> print '{0:n}'.format(3.1415)
  19. 3.1415
  20. >>> print '{0:%}'.format(3.15)
  21. 315.000000%
format()函数还有其他一些应用,使用起来也很方便,在此不一一赘述。
转自
python格式化输出 - ****博客 http://blog.****.net/wchoclate/article/details/42297173