サマリー:本文總結(jié)了一些簡(jiǎn)單基本的輸出格式化形式,下面話不多說了,來看看詳細(xì)的介紹吧。一、打印字符串>>> print "I'm %s" % ("jihite") I'm jihite二、打印整數(shù)>>> print "I'
本文總結(jié)了一些簡(jiǎn)單基本的輸出格式化形式,下面話不多說了,來看看詳細(xì)的介紹吧。
一、打印字符串
>>> print "I'm %s" % ("jihite") I'm jihite
二、打印整數(shù)
>>> print "I'm %d years old" % (17) I'm 17 years old
三、打印浮點(diǎn)數(shù)
>>> print "π=%f" % (3.1415926) π=3.141593
四、打印浮點(diǎn)數(shù)(指定保留小數(shù)點(diǎn)位數(shù))
>>> 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
六、指定占位符寬度(左對(duì)齊)
>>> print "NAME:%-8s AGE:%-8d WEIGHT:%-8.2f" % ("jihite", 17, 62.2) NAME:jihite AGE:17 WEIGHT:62.20
七、指定占位符(只能用0當(dāng)占位符)
>>> print "NAME:%-8s AGE:%08d WEIGHT:%08.2f" % ("jihite", 17, 62.2) NAME:jihite AGE:00000017 WEIGHT:00062.20
八、科學(xué)計(jì)數(shù)法
>>> format(0.0000023, '.2e') '2.30e-06' >>> format(0.23, '.2e') '2.30e-01'
更多關(guān)于python常見的格式化輸出小結(jié)請(qǐng)關(guān)注PHP中文網(wǎng)(ipnx.cn)其他文章!