Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I add a % symbol to my Python script

Tags:

python

Here is my code. I need to add a percent symbol at the end of the print string. I can't get this figured out.

total = 0
counter = 0
while True:
    score = int(input('Enter test score: '))
    if score == 0:
        break
    total += score
    counter += 1
average = total / counter
print('The average is:', format(average, ',.3f'))
like image 814
spbiknut Avatar asked Nov 07 '15 23:11

spbiknut


People also ask

How do you insert symbols in Python?

If you see utf-8 , then your system supports unicode characters. To print any character in the Python interpreter, use a \u to denote a unicode character and then follow with the character code. For instance, the code for β is 03B2, so to print β the command is print('\u03B2') .

How do you print special symbols in Python?

How do you show special characters in Python? Use repr() to print special characters Call repr(string) to return string with special characters escaped.

What is the %% in Python?

The % symbol in Python is called the Modulo Operator. It returns the remainder of dividing the left hand operand by right hand operand.

Where do we use symbols in Python?

Decorators. The main use case of the symbol @ in Python are decorators. In Python, a decorator extends the functionality of an existing function or class.


2 Answers

The probably best way would be to just use the percent format type:

format(average, '.1%')

# or using string formatting:
'{:.1%}'.format(average)

This already multiplies the average by 100 and will also show a percent sign at the end.

>>> average = .123
>>> print('The average is: {:.1%}'.format(average))
The average is: 12.3%
like image 178
poke Avatar answered Oct 18 '22 21:10

poke


print('The average is: ' + format(average, ',.3f') + '%')

?

like image 31
Stephane Martin Avatar answered Oct 18 '22 20:10

Stephane Martin