How to Print a Percentage Value in Python?

To print a percentage value in Python, use the str.format() method or an f-string on the format language pattern "{:.0%}". For example, the f-string f"{your_number:.0%}" will convert variable your_number to a percentage string with 0 digits precision.

Simply run those three basic statements in your shell:

  1. your_number = 0.42
  2. percentage = "{:.0%}".format(your_number)
  3. print(percentage)

As a more Pythonic alternative to step number 2, you can use f-strings such as in percentage = f"{your_number:.0%}" for Python version 3.6 and above.

Here’s the minimal 3-step code example to convert a numeric value to a string that is formatted as a percentage value:

# 1. Create a float or integer number:
your_number = 0.42

# 2. Convert the number to a string value:
percentage = "{:.0%}".format(your_number)

# alternative for Python 3.6+: 
percentage = f"{your_number:.0%}"

# 3. Print the result
print(percentage)

The resulting output is:

42%

What does the format string "{:.0%}" mean?

  • :% – The colon symbol followed by the percentage symbol means “convert it to a percentage number”.
  • .0 – The dot symbol followed by the zero means “use zero digits after the decimal number”.

If you want to include more digits and accomplish a higher precision, use a larger number after the dot such as in "{:.2%}" for two digits or "{:.2%}" for thirteen digits.

print("{:.0%}".format(2/3))
# 67%

print("{:.2%}".format(2/3))
# 66.67%

print("{:.13%}".format(2/3))
# 66.6666666666667%

? Instead of using the str.format() function, you can also use f-strings for Python 3.6 and newer versions. For example, f"{x:.0%}" would convert the numeric value stored in variable x to a percentage number without a decimal digit.

Here are the same three examples as in the previous code snippet—but using f-strings instead:

x = 2/3

print(f"{x:.0%}")
# 67%

print(f"{x:.2%}")
# 66.67%

print(f"{x:.13%}")
# 66.6666666666667%

This is even more concise and it is the most Pythonic version for Python 3.6 and above. You can learn everything about the differences of f-strings and the more traditional str.format() here.

Lazy Non-Format Alternative

If you’re a bit lazy about f-strings and you always have to look up the syntax, you can simply use the round() built-in method on the fractional number normalized to the 1-100 percentage range. Then, convert the result to a string and use string concatenation to append a suffix '%' symbol.

These three steps will do the trick:

  1. x = 2/3
  2. percentage = str(round(x*100)) + '%'
  3. print(percentage)

Here’s the code:

x = 2/3
percentage = str(round(x*100)) + '%'
print(percentage)
# 67%

Thanks for reading through this whole article! πŸ™‚

If you want to boost your Python skills on autopilot, check out the market-leading free Finxter Email Academy with hundreds of Python lessons sent directly into your INBOX:

Programmer Humor

Q: How do you tell an introverted computer scientist from an extroverted computer scientist?

A: An extroverted computer scientist looks at your shoes when he talks to you.