Calculate percentage from user Input in Python

avatar

Borislav Hadzhiev

Last updated: Aug 26, 2022

banner

Photo from Unsplash

Calculate percentage from user Input in Python #

To calculate percentage from user input:

  1. Convert the input values to floats.
  2. Use the division / operator to divide one number by another.
  3. Multiply the quotient by 100 to get the percentage.
  4. The result shows what percent the first number is of the second.
main.py
# 👇️ if you need to format an input value to 1 or more decimal places user_input = input('Type a percentage, e.g. 10: ') result = f'{float(user_input) / 100:.1%}' print(result) # 👉️ 10.0% # ---------------------------------------- # 👇️ make sure to convert input() result to float() def is_what_percent_of(num_a, num_b): return (num_a / num_b) * 100 print(is_what_percent_of(25, 75)) # 👉️ 33.33 print(is_what_percent_of(15, 93)) # 👉️ 16.12903.. print(round(is_what_percent_of(15, 93), 2)) # 👉️ 16.13 # -------------------------------------------------- def get_percentage_increase(num_a, num_b): return ((num_a - num_b) / num_b) * 100 print(get_percentage_increase(60, 30)) # 👉️ 100.0 print(get_percentage_increase(40, 100)) # 👉️ -60.0 # -------------------------------------------------- def get_remainder(num_a, num_b): return num_a % num_b print(get_remainder(50, 15)) # 👉️ 5 print(get_remainder(50, 20)) # 👉️ 10

The first example uses a formatted string literal to format an input value to 1 or more decimal places.

main.py
user_input = input('Type a percentage, e.g. 10: ') result = f'{float(user_input) / 100:.1%}' print(result) # 👉️ 10.0% result = f'{float(user_input) / 100:.2%}' print(result) # 👉️ 10.00%
Formatted string literals (f-strings) let us include expressions inside of a string by prefixing the string with f.

Make sure to wrap expressions in curly braces - {expression}.

We are also able to use the format specification mini-language in expressions in f-strings.

main.py
my_float = 0.79 result_1 = f'{my_float:.1%}' print(result_1) # 👉️ 79.0% result_2 = f'{my_float:.2%}' print(result_2) # 👉️ 79.00%

If you need to calculate what percent one number is of another, divide one number by the other and multiply the result by 100.

main.py
def is_what_percent_of(num_a, num_b): return (num_a / num_b) * 100 num1 = float(input('Enter a number: ')) # 👉️ 25 num2 = float(input('Enter another number: ')) # 👉️ 75 result = is_what_percent_of(num1, num2) print(result) # 👉️ 33.33333333333333 print(round(result, 2)) # 👉️ 33.33

Make sure to use the float() class to convert the input strings to floating-point numbers.

The input() function is guaranteed to return a string even if the user enters a number.

Use the round() function if you need to round the result to N decimal places.

The round function returns the number rounded to ndigits precision after the decimal point.

Note that if you try to divide by 0, you'd get a ZeroDivisionError.

If you need to get the percentage increase from one number to another, use the following function.

main.py
def get_percentage_increase(num_a, num_b): return ((num_a - num_b) / num_b) * 100 print(get_percentage_increase(60, 30)) # 👉️ 100.0 print(get_percentage_increase(40, 100)) # 👉️ -60.0

The first example shows that the percentage increase from 60 to 30 is 100 %.

And the second example shows that the percentage increase from 40 to 100 is -60%.

If you always need to get a positive number, use the abs() function.

main.py
def get_percentage_increase(num_a, num_b): return abs((num_a - num_b) / num_b) * 100 print(get_percentage_increase(60, 30)) # 👉️ 100.0 print(get_percentage_increase(40, 100)) # 👉️ 60.0

The abs function returns the absolute value of a number. In other words, if the number is positive, the number is returned, and if the number is negative, the negation of the number is returned.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.