如何使用字符串格式打印“%"符号? [英] How do I print a '%' sign using string formatting?

查看:91
本文介绍了如何使用字符串格式打印“%"符号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了一个计算百分比的小脚本;但是,我希望在打印的消息中实际包含 %...

I've made a little script to calculator percent; however, I wish to actually include the % within the message printed...

一开始就试过了 - 没用...

Tried this at the start - didn't work...

oFile.write("Percentage: %s%"\n" % percent)

然后我尝试了 "Percentage: %s"%"\n"% 百分比" 无效.

I then tried "Percentage: %s"%"\n" % percent" which didn't work.

我希望输出为:

Percentage: x%

我一直在接受

TypeError: not all arguments converted during string formatting

推荐答案

要打印 % 符号,您需要使用另一个 % 符号转义"它:

To print the % sign you need to 'escape' it with another % sign:

percent = 12
print "Percentage: %s %%\n" % percent  # Note the double % sign
>>> Percentage: 12 %

这篇关于如何使用字符串格式打印“%"符号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆