python输出格式小结

141L     # print 碰到逗号会输出一个空格

144L   # 同

 

print('1+10=', 1+10)

输出: ('1+10=', 11)

以上这些都是普通输出

下面来看格式化输出:

>>> print 'hello world'    # 这个是普通输出
hello world
>>> 
>>> 'hello, %s' % 'world'
'hello, world'
>>> print 'hello, %s' % 'world'
hello, world
>>> 'Hi, %s, you have $%d.' % ('Michael',10000)
'Hi, Michael, you have $10000.'
>>> 

%运算符就是用来格式化字符串的。在字符串内部,%s表示用字符串替换,%d表示用整数替换,有几个%?占位符,后面就跟几个变量或者值,顺序要对应好。如果只有一个%?,括号可以省略

 

下面为如何输出一个百分数?


last = 71.0
now = 89.0
var = (now - last)/last*100
print var

print ("成绩提升百分比为: %.1f%%") % var

结果:

             25.3521126761
             成绩提升百分比为: 25.4%

 

相关文章: