python print的用法

第一种是格式化的

strHello = "the length of (%s) is %d" %('Hello World',len('Hello World'))
import math
#default
print "PI = %f" % math.pi
#width = 10,precise = 3,align = left
print "PI = %10.3f" % math.pi
#width = 10,precise = 3,align = rigth
print "PI = %-10.3f" % math.pi
#前面填充字符
print "PI = %06d" % int(math.pi)
 
#输出结果
#PI = 3.141593
#PI =      3.142
#PI = 3.142
#PI = 000003
#浮点数的格式化,精度、度和

refer to: 

http://www.pythonclub.org/python-basic/print

另外一种是用字符串的格式化

print('We are the {} who say "{}!"'.format('knights', 'Ni'))

refer to:

http://www.pythondoc.com/pythontutorial3/inputoutput.html

原文地址:https://www.cnblogs.com/qingyuanjushi/p/8408777.html