网络编程
位置:首页>> 网络编程>> Python编程>> python常见的格式化输出小结

python常见的格式化输出小结

作者:daisy  发布时间:2022-12-29 20:23:31 

标签:python,格式化,输出

本文总结了一些简单基本的输出格式化形式,下面话不多说了,来看看详细的介绍吧。

一、打印字符串


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

二、打印整数


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

三、打印浮点数


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

四、打印浮点数(指定保留小数点位数)


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

五、指定占位符宽度


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

六、指定占位符宽度(左对齐)


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

七、指定占位符(只能用0当占位符)


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

八、科学计数法


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

总结

0
投稿

猜你喜欢

手机版 网络编程 asp之家 www.aspxhome.com