How to print a percentage value in python?
format
supports a percentage floating point precision type:
>>> print "{0:.0%}".format(1./3)
33%
If you don't want integer division, you can import Python3's division from __future__
:
>>> from __future__ import division
>>> 1 / 3
0.3333333333333333
# The above 33% example would could now be written without the explicit
# float conversion:
>>> print "{0:.0f}%".format(1/3 * 100)
33%
# Or even shorter using the format mini language:
>>> print "{:.0%}".format(1/3)
33%
Is there an operator to calculate percentage in Python?
There is no such operator in Python, but it is trivial to implement on your own. In practice in computing, percentages are not nearly as useful as a modulo, so no language that I can think of implements one.
Converting from float to percentage
An idea is to create a custom datatype, I'll call it Percent
, which inherit everything from float
, but when you want to use it as a string it shows a %
and multiplies the number with 100.
class Percent(float):
def __str__(self):
return '{:.2%}'.format(self)
x = Percent(0.3333)
print(x)
# 33.33%
If you want to represnt the value in envoirment like jupyter notebook in your format you can add same method with the name __repr__
to Percent Class:
def __repr__(self):
return '{:.2%}'.format(self)
How do I print a '%' sign using string formatting?
To print the %
sign you need to 'escape' it with another %
sign:
percent = 12
print "Percentage: %s %%\n" % percent # Note the double % sign
>>> Percentage: 12 %
EDIT
Nowadays in python3 a better (and more readable) approach is to use f-strings. Note that other solutions (shown below) do work as well:
$python3
>>> percent = 12
>>> print(f'Percentage: {percent}%') # f-string
Percentage: 12%
>>> print('Percentage: {0}%'.format(percent)) # str format method
Percentage: 12%
>>> print('Percentage: %s%%' % percent) # older format, we 'escape' the '%' character
Percentage: 12%
How to print out status bar and percentage?
There's a Python module that you can get from PyPI called progressbar
that implements such functionality. If you don't mind adding a dependency, it's a good solution. Otherwise, go with one of the other answers.
A simple example of how to use it:
import progressbar
from time import sleep
bar = progressbar.ProgressBar(maxval=20, \
widgets=[progressbar.Bar('=', '[', ']'), ' ', progressbar.Percentage()])
bar.start()
for i in xrange(20):
bar.update(i+1)
sleep(0.1)
bar.finish()
To install it, you can use easy_install progressbar
, or pip install progressbar
if you prefer pip.
Format print value as percentage in python
The %
specifier multiplies the value by a hundred then acts as if you'd used the f
specifier.
Since your maximum SPEED
here is 0.0129
(as per the if
statement), your formula (SPEED / 100.0) * 15.5
will generate, at most, 0.0019995
. The %
specifier will then turn that into 0.19995
, which is only about 0.2%
.
If you're expecting it to give you 20%
, you need to get rid of the / 100.0
bit from your formula. That way, you'll end up with 0.19995
, the %
specifier will change that to 19.995
, and you should then get 20%
by virtue of the fact you're using 1.0
as the width specification, as per the following transcript:
>>> SPEED = 0.0129
>>> seqSpeed = SPEED * 15.5
>>> print("Speed: {:1.0%}".format(seqSpeed))
Speed: 20%
Related Topics
How to Replace Back Slash Character with Empty String in Python
Why Are Default Arguments Evaluated at Definition Time
How to Break a Line of Chained Methods in Python
Using a Where _ in _ Statement
Django Model "Doesn't Declare an Explicit App_Label"
Python Map Object Is Not Subscriptable
Count Frequency of Values in Pandas Dataframe Column
Trouble Installing Textblob with Pip
How to Treat Python Argparse.Namespace() as a Dictionary
Python Webdriver to Handle Pop Up Browser Windows Which Is Not an Alert
How to Leave/Exit/Deactivate a Python Virtualenv
How to Add a Question Mark [] Button on the Top of a Tkinter Window
Django What Is Reverse Relationship