How to Humanize numbers with Python?


If you want something that converts integers to words like 99 to ninety-nine, you have to use an external package or build one yourself. The pynum2word module is pretty good at this task. You can install it using −

$ pip install pynum2word

Then use it in the following way −

>>> import num2word
>>> num2word.to_card(16)
'sixteen'
>>> num2word.to_card(23)
'twenty-three'
>>> num2word.to_card(1223)

'one thousand, two hundred and twenty-three'

If you want to get results like 1.23 million for 1,230,000, you can use the humanize library to do so. You can install it using −

$ pip install humanize

Then use it in the following way −

>>> import humanize
>>> humanize.intword(1230000)
'1.23 million'

Lakshmi Srinivas
Lakshmi Srinivas

Programmer / Analyst / Technician

Updated on: 05-Mar-2020

174 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements