How To Print A Percentage Value In Python?

How To Print A Percentage Value In Python

Different fields like Machine learning, Data science, and Deep learning make use of ‘data’ to build different models. We perform different tasks on the ‘data’; some numbers/results are in the form of ‘Percentage values‘. Let’s understand how to print a percentage value in Python.

The domains like Machine learning and Data science build models using Python language. Sometimes, these models perform mathematical calculations, and results are printed in the form of percentage values. For example, The accuracy of every model is printed in the form of percentages, like 99.7%, or 98.5%. These percentage values are very important in this model for analysis.

In some models, we need to use mathematical formulas to evaluate the results. For example, very basic operations like 1.0/3.0 = 0.333, can be converted into the percentage value i.e. 33%. These operations may be a small part of the big calculations. Let’s see the percentage values in detail.

What is Percentage Value?

The percentage value is a result of dividing two numbers and multiplying the result by 100. The percentage value is always represented with the ‘%’ suffix. Let’s see some simple examples to understand the percentage values.

How to Convert the 4/5 in Percentage Value?

The result of 4/5 is 0.8. After multiplication, the value becomes 80%. The final result is ‘80%’. In this way, we can calculate the percentage values.

It is very simple when we perform these calculations manually or using a calculator. There are many methods in Python language to print percentage values. Let’s see the examples.

How to Print a Percentage Value in Python?

There are different methods in Python to print the percentage value. All methods are easy and understandable. Let’s see the examples one by one.

Example 1: Print Percentage Value Using Format Function in Python

The format function in Python is used to replace something in the string. The format function is used to replace every data type like integer, float, string, character, etc. So, it becomes easy when we use the format method to print the percentage value in Python.

Number = 0.75
Percentage_value = "{:.0%}".format(Number)
print(Percentage_value)

In example 1, The Number holds the float value ‘0.75’ which is used in the .format() function. The second line of code consists of syntax “{:.0%}”, which is used to print the % sign and percentage calculation without any precision. This value will be replaced by the ‘Number’ and printed in the next line.

Percentage Value Using Format Function
Percentage Value Using Format Function

The output of example 1 is 75% which is correct!

Example 2: Print Percentage Value Using Format Function in Python (With Precision)

Format function can be used to print the percentage values with precision. Let’s implement the example to see the result.

print("{:.2%}".format(8/9))

In this code, “{:.2%}” denotes the precision of two numbers in the output.

Percentage Value Using Format Function (With Precision)
Percentage Value Using Format Function (With Precision)

The output of example 2 is 88.89% which is correct!

Example 3: Print Percentage Value Using F-string in Python

The f-string in Python works similarly as a format function. The f-string is used with curly braces, and ‘f’ is used as a prefix. This f-string will replace the things from the curly braces with the given string. Here, we’ll replace the original value with the percentage value.

Number = 0.53
Percentage_value = f"{Number:.0%}"
print(Percentage_value)

In this example 3, we use f-string instead of the format function but the working is the same. The Number contains the value and the ‘:.0%‘ syntax will help to convert an original value into a percentage value. Let’s see the result.

Percentage Value Using F String
Percentage Value Using F String

Example 4: Print Percentage Value Using F-string in Python (With Precision)

Here, let’s print the percentage value using f-string in Python.

Number = 0.5373
print(f"{Number:.2%}")

Here, in this example 4, the percentage value is printed with the 2 precision places, ‘.2%’ denotes the 2-precision points.

Percentage Value Using F String With Precision
Percentage Value Using F String With Precision

The output of example 4 is ‘53.73%’ which is correct!

Example 5: Print Percentage Value Using Formula

There is simple technique/ formula to print the percentage value in Python, which is very simple but for knowledge, we can implement it.

print(str(float(4/5)*100)+'%')

Here, in this example 5, the formula to get the percentage value is directly used. The simple string is printed with a ‘%’ sign. After comparing all the examples, this one is the easiest technique to print percentage values in Python.

Percentage Value Using Formula
Percentage Value Using Formula

Summary

This article covers the topic of how to print percentage values in detail. Some basic topics like, what is percentage value? how to get the percentage value? printing the percentage value using different functions like format and f-string, examples with precision is also given. Hope you will enjoy this article.

References

Do read the official documentation on format and f-string for details.