How to print a percentage value?

Since Python 3.0, str.format and format support a percentage presentation type:

>>> f"{1/3:.0%}"
>>> "{:.0%}".format(1/3)
>>> format(1/3, ".0%")

Percentage. Multiplies the number by 100 and displays in fixed ('f') format, followed by a percent sign.

The .0 part of the format spec .0% indicates that you want zero digits of precision after the decimal point, because with f"{1/3:%}" you would get the string '33.333333%'.

It works with integers, floats, and decimals. See PEP 3101.

Leave a Comment