Unit Conversion with Python and the Pint Package

Do you need to work measurements often? What about converting from one unit of measurement to another? There is a Python package called Pint that makes working with quantities easy to do. Pint allows you do arithmetic operations between a numerical value and a quantity as well. You can see the many different unit types included with Pint on their GitHub project.

Let’s get started by learning how to install Pint!


You can install Pint using pip like this:

python3 -m pip install pint

If you are a conda user, then you would want to use this command instead:

conda install -c conda-forge pint

Now that you have Pint installed, you are ready to learn how to use it!

Getting Started with Pint

One of the coolest features of Pint is that you can use it to convert from one unit type to another. For example, you might want to convert from some Imperial unit to a Metric unit.

A popular use case would be to convert from miles to kilometers. Open up your Python REPL (or IDLE) and try out the following code:

>>> from pint import UnitRegistry
>>> ureg = UnitRegistry()
>>> distance = 5 * ureg.mile
>>> distance
<Quantity(5, 'mile')>
>>> distance.to("kilometer")
<Quantity(8.04672, 'kilometer')>

Here you create a Quantity object named distance. You set its value to 5 miles. Then to convert it to kilometers, you call the distance’s to() method and pass in the new quantity name that you want. The result is that 5 miles is converted to 8.04672 kilometers.

You can convert the quantity to different unit types within the same system too. For example, you could convert kilometers to centimeters, if you wanted to:

>>> from pint import UnitRegistry 
>>> ureg = UnitRegistry()
>>> distance_in_km = 5 * ureg.kilometer
>>> distance_in_km
<Quantity(5, 'kilometer')>
>>> distance_in_cm = distance_in_km.to("centimeter")
>>> distance_in_cm
<Quantity(500000.0, 'centimeter')>

Pint Parses Strings

One of Pint’s cool features is that you can specify quantities using strings. That means you can do stuff like this:

>>> my_quantity = ureg.Quantity
>>> my_quantity(2.54, 'centimeter')
<Quantity(2.54, 'centimeter')>

Or you can simplify it, even more, to simply:

>>> my_quantity = ureg.Quantity
>>> my_quantity('2.54in')
<Quantity(2.54, 'inch')>

Now that you have played around with converting between different unit types, you are ready to learn about string formatting with Pint.

Using String Formatting with Pint

Pint supports formatting using Python’s .format() and by using f-strings. Here is an example from the Pint tutorial:

>>> ureg = ureg.Quantity
>>> accel = 1.3 * ureg['meter/second**2']
>>> print(f'The str is {accel}')
The str is 1.3 meter / second ** 2

When the f-string is evaluated, the Quantity object is converted into a more human-readable format.

Pint goes farther than that by extending Python’s formatting capabilities. Here is an example of their custom “pretty print”:

>>> ureg = ureg.Quantity
>>> accel = 1.3 * ureg['meter/second**2']
>>> # Pretty print
>>> 'The pretty representation is {:P}'.format(accel)
'The pretty representation is 1.3 meter/second²'

Pint also supports custom printing of LaTeX and HTML for Jupyter Notebooks.

Wrapping Up

Pint is a really nice Python package. While this tutorial doesn’t cover it, Pint allows you to set your locale so that the unit names match your language. If you regularly work with quantities that need to be converted between unit types (like cm to mm or inches to centimeters), this may be just what you need to make your coding life easier.

More Neat Python Packages

Want to learn about other neat 3rd party Python packages? Check out the following articles: