0

如何在打印功能中打印出字符“%”。以下行失败。

print "The result is %s out of %s i.e. %d %" % (nominator, denominator, percentage)
4

2 回答 2

7

%你必须通过做来逃避%%。因此,在您的示例中,请执行以下操作:

print "The result is %s out of %s i.e. %d %%" % (nominator, denominator, percentage)
#                                         ^ extra % to escape the one after
于 2013-11-02T03:43:01.923 回答
0

考虑使用格式

>>> n=23.2
>>> d=1550
>>> "The result is {:.2f} out of {:.2f} i.e. {:.2%}".format(n,d,n/d)
'The result is 23.20 out of 1550.00 i.e. 1.50%'


>>> "The result is {:,.2f} out of {:,.2f} i.e. {:.2%}".format(n,d,n/d)
'The result is 23.20 out of 1,550.00 i.e. 1.50%'

如果您的参数是字符串:

>>> "The result is {:,.2f} out of {} i.e. {:.2%}".format(n,str(d),n/d)
'The result is 23.20 out of 1550 i.e. 1.50%'
于 2013-11-02T03:56:13.590 回答