码迷,mamicode.com
首页 > 编程语言 > 详细

python格式化输出

时间:2017-11-06 13:58:55      阅读:220      评论:0      收藏:0      [点我收藏+]

标签:浮点   对齐   http   字符   label   浮点数   hit   sheng   print   

1.打印字符串

>>> print "I‘m %s" % ("jihite")
I‘m jihite

2.打印整数

>>> print "I‘m %d years old" % (17)
I‘m 17 years old

3.打印浮点数

>>> print "π=%f" % (3.1415926)
π=3.141593

4.打印浮点数(指定保留小数点位数)

>>> print "π=%.3f" % (3.1415926)
π=3.142

5.指定占位符宽度

>>> print "NAME:%8s AGE:%8d WEIGHT:%8.2f" % ("jihite", 17, 62.2)
NAME:  jihite AGE:      17 WEIGHT:   62.20

6.指定占位符宽度(左对齐)

>>> print "NAME:%-8s AGE:%-8d WEIGHT:%-8.2f" % ("jihite", 17, 62.2)
NAME:jihite   AGE:17       WEIGHT:62.20 

7.指定占位符(只能用0当占位符)

>>> print "NAME:%-8s AGE:%08d WEIGHT:%08.2f" % ("jihite", 17, 62.2)
NAME:jihite   AGE:00000017 WEIGHT:00062.20

8.科学计数法

>>> format(0.0000023, ‘.2e‘)
‘2.30e-06‘
>>> format(0.23, ‘.2e‘)
‘2.30e-01‘

转载:http://www.cnblogs.com/kaituorensheng/p/5545650.html

python格式化输出

标签:浮点   对齐   http   字符   label   浮点数   hit   sheng   print   

原文地址:http://www.cnblogs.com/mologa-jie/p/7792635.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!