How do I print a '%' sign using string formatting?
To print the %
sign you need to 'escape' it with another %
sign:
percent = 12print "Percentage: %s %%\n" % percent # Note the double % sign>>> Percentage: 12 %
Or use format()
function, which is more elegant.
percent = 12print "Percentage: {}%".format(percent)
4 years later edit
Now In Python3x print()
requires parenthesis.
percent = 12print ("Percentage: {}%".format(percent))
The new Python 3 approach is to use format strings.
percent = 12print("Percentage: {0} %\n".format(percent))>>> Percentage: 12 %
This is also supported in Python > 2.6.