39

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
1
  • 1
    @OliverW.: yes but that one's title and wording are very unclear, so this one should be canonical
    – smci
    Feb 8, 2018 at 22:22

6 Answers 6

78

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 %

EDIT

Nowadays in python3 a better (and more readable) approach is to use f-strings. Note that other solutions (shown below) do work as well:

$python3
>>> percent = 12
>>> print(f'Percentage: {percent}%') # f-string
Percentage: 12%
>>> print('Percentage: {0}%'.format(percent)) # str format method
Percentage: 12%
>>> print('Percentage: %s%%' % percent) # older format, we 'escape' the '%' character
Percentage: 12%
1
  • In some cases, like logging, f-strings are eagerly evaluated, while the old %s formatting is lazily evaluated - so f-strings are slower in these cases, which I would argue is not better for large code files.
    – Rafs
    Feb 14 at 13:20
13

Or use format() function, which is more elegant.

percent = 12
print "Percentage: {}%".format(percent)

4 years later edit

Now In Python3x print() requires parenthesis.

percent = 12
print ("Percentage: {}%".format(percent))
5
x = 0.25
y = -0.25
print("\nOriginal Number: ", x)
print("Formatted Number with percentage: "+"{:.2%}".format(x));
print("Original Number: ", y)
print("Formatted Number with percentage: "+"{:.2%}".format(y));
print()

Sample Output:

Original Number:  0.25                                                                                        
Formatted Number with percentage: 25.00%                                                                      
Original Number:  -0.25                                                                                       
Formatted Number with percentage: -25.00% 

Helps in proper formatting of percentage value

+++

Using ascii value of percentage - which is 37

print( '12' + str(chr(37)) )
4

The new Python 3 approach is to use format strings.

percent = 12
print("Percentage: {0} %\n".format(percent))
>>> Percentage: 12 %

This is also supported in Python > 2.6.

See the docs here: Python 3 and Python 2

3
  • 2
    Note that no need 0 inside bracelets
    – GLHF
    Feb 5, 2015 at 12:36
  • 1
    @howaboutNO 0 is required if you're using Py2.6
    – canni
    Feb 6, 2015 at 10:14
  • But that's slightly more characters than the nice modulos and my lazy 1st-world doesn't like typing characters to get results Jul 19, 2019 at 22:09
2

format() is more elegant but the modulo sign seems to be quicker!

http://inre.dundeemt.com/2016-01-13/string-modulo-vs-format-fight/ - shows that modulo is ~30% faster!

-2
print("Interest Rate (APR): " , format(APR, '.0f'),'%')
3
  • took this out of an interest calculator i made in class, this was the output: Mar 26, 2021 at 20:26
  • Interest Rate (APR): 5 % Mar 26, 2021 at 20:27
  • 3
    Please, add an explanation in your answer.
    – Syscall
    Mar 26, 2021 at 20:38

Not the answer you're looking for? Browse other questions tagged or ask your own question.