Python3中的格式化输入输出之print函数
Python2版本和Python3版本是有区别的???????? Note : "()"这个非常重要
1、Python2版
num = 7.9
ptint "The number is %f" % num
The number is 7.900000
2、Python3版
####字符串 %s
print("My name is %s" % ("Beechan"))
#Output:
My name is Beechan
####整型 %d %i
print("I am %d years old" % (26))
#Output:
I am 26 years old
注意到里面有个“%”符号,这里要对这个符号作特别说明:
%后面可以使用修饰符(若有,则只能使用以下)
%[(name)][flags][width][.precision]typecode
[(name)]:字典的键名
[flags]:“-” 表示左对齐,默认为右对齐 “+”表示包含数字符号,正数也会带“+” "0"零填充
[width]:制定最小宽度的数字
[.]:用于按照精度分割字段的宽度
[precision]:指定要打印字符串中的最大字符个数,浮点数中小数点之后的位数,或者整数的最小位数
eg.
d = {'x' : 12, 'y' : 28.456972, 'z' : 87 }
print ("%(x)-10d %(y)0.3g" % d)