How do I print a '%' sign using string formatting?

Eric1989 picture Eric1989 · Feb 5, 2015 · Viewed 70.8k times · Source

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)

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

I'd like the output to be: Percentage: x%

I keep getting "TypeError: not all arguments converted during string formatting"

Answer

bvidal picture bvidal · Feb 5, 2015

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 %