Python中字符串格式化的细节

 1 >>> print("%.2f %s are worth $%d"%(4.5560,"Argentine Pesos",1))
 2 4.56 Argentine Pesos are worth $1
 3 >>> template = "%.2f %s are worth $%d"
 4 >>> templat % (4.5560,"Argentine Pesos",1)
 5 Traceback (most recent call last):
 6   File "<pyshell#230>", line 1, in <module>
 7     templat % (4.5560,"Argentine Pesos",1)
 8 NameError: name 'templat' is not defined
 9 >>> template = "%.2f %s are worth $%d"
10 >>> template % (4.5560,"Argentine Pesos",1)
11 '4.56 Argentine Pesos are worth $1'

Python3中,一段代码压缩成一物理行print可直接生成输出结果,如line1,line2。

物理行分解,在字符串格式定义好之后不使用print也可输出结果。

posted @ 2016-06-18 11:19  dmdoge  阅读(238)  评论(0编辑  收藏  举报