0
如何在打印功能中打印出字符「%」。以下行失敗。打印特殊字符
print "The result is %s out of %s i.e. %d %" % (nominator, denominator, percentage)
如何在打印功能中打印出字符「%」。以下行失敗。打印特殊字符
print "The result is %s out of %s i.e. %d %" % (nominator, denominator, percentage)
您必須通過執行%%
來逃脫%
。因此,在你的榜樣,做:
print "The result is %s out of %s i.e. %d %%" % (nominator, denominator, percentage)
# ^extra % to escape the one after
考慮使用format:
>>> 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%'
只是想知道,爲什麼不'\'%在工作這個情況? –
@SteveP。我實際上沒有線索,我想這只是'%'來轉義'%'和'\\'爲撇號 – TerryA