Since Python 3.0, str.format
and format
support a percentage presentation type:
>>> f"{1/3:.0%}"
'33%'
>>> "{:.0%}".format(1/3)
'33%'
>>> format(1/3, ".0%")
'33%'
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%'
.