Updated On : Jan-16,2024 Time Investment : ~10 mins

Transforming Numbers, Dates, and File Sizes with Humanize in Python

In the exciting world of coding, where cool stuff like algorithms and data structures usually steal the spotlight, it's super important not to lose sight of the fact that real people are the ones using our creations. Whether you're building a website, playing around with data visuals, or tackling massive datasets, making information easy for humans to understand is key for getting your message across.

Let's dive into the magic of making your code speak human with relatable formats, because, let's face it, we want our creations to be user-friendly and not just a jumble of techy stuff!

The "humanize" is a Python library that can help us format numbers, dates, file sizes, metrics, etc in human-readable form. In this blog post, i'll explain various functions of the "humanize" library to transform mundane numbers, dates, and sizes into easily understandable and relatable formats.

Table of Contents

  1. Numbers
  2. Date, Time & Timedeltas
  3. File Sizes
  4. Metrics
  5. Scientifica Notations
  6. Human Readable Floats


  • pip install humanize

Youtube Video

import humanize

1. Humanize Numbers

In this section, I have explained functions available from "humanize" to format numbers.

Here are list of functions for formating numbers.

  • intcomma() - As the name suggests, the function is used to add comma to numbers. It adds comma after every 3 digits.
  • intword() - It converts number by adding word (thousand, million, billion, etc) at the end.
  • apnumber() - It converts number to actual word (one, two three, etc).
  • ordinal() - It converts number to sequence like 1st, 2nd, etc.

Below, I have included few examples explaining the usage of all functions.

humanize.intcomma(12000000.56758, ndigits=2)
'1.2 thousand'
'1.2 million'
'12.0 billion'
'12.0 trillion'
'12.0 quadrillion'

2. Humanize Dates, Time & Timedeltas

In this section, I have included examples of functions for formating dates, time and time deltas.

2.1 Natural Date

  • naturaldate() - This function can be used to represent dates in natural format like month name, followed by day and year.
from datetime import datetime, date, time, timedelta
'Jan 07 2022'
'Jan 07 2022'

2.2 Natural Day

  • naturalday() - The function as its name suggest is used to represent particular day (today, tomorrow, yeasterday, Jan 01, etc.) from date object.
'Jan 07'
humanize.naturalday(datetime.now() - timedelta(days=1))
humanize.naturalday(datetime.now() - timedelta(days=2))
'Jul 03'
humanize.naturalday(datetime.now() + timedelta(days=1))

2.3 Natural Time

  • naturaltime() - The function can be used to represent time in natural words like now, tomorrow, 5 minutes ago, 5 days ago, 2 hours ago, etc. The time is represented for input datetime from current time.
'6 months ago'
'5 days ago'
humanize.naturaltime(datetime.now() - timedelta(days=5))
'5 days ago'
humanize.naturaltime(datetime.now() - timedelta(hours=5))
'5 hours ago'
humanize.naturaltime(datetime.now() - timedelta(seconds=60))
'a minute ago'
humanize.naturaltime(datetime.now() - timedelta(seconds=30))
'30 seconds ago'

2.4 Natural Time Delta

  • naturaldelta() - This function can be used to represent time delta in more human readable form like 5 days, 1 hour, 5 mins, etc.
'5 days'
humanize.naturaldelta(timedelta(days=5, hours=5))
'5 days'
humanize.precisedelta(timedelta(days=5, hours=5))
'5 days and 5 hours'
humanize.precisedelta(timedelta(days=5, hours=5, minutes=10))
'5 days, 5 hours and 10 minutes'
humanize.precisedelta(timedelta(days=5, hours=5, minutes=10, milliseconds=300))
'5 days, 5 hours, 10 minutes and 0.30 seconds'
'16 minutes and 41 seconds'

3. Humanize File-Size

  • naturalsize() - If you are looking to represent file sizes in proper format then this function is for you. It takes number of bytes as input and converts it to proper format like MB, Kb, GB, etc.
'5 Bytes'
'500 Bytes'
'1.5 kB'
'15.0 MB'
'15.0 GB'
'15.0 TB'
humanize.naturalsize(15000000000000, binary=True) ## It uses 2^10 base instead of 10^3
'13.6 TiB'
humanize.naturalsize(15000000000000, gnu=True)

4. Metric

  • metric() - This function can be used to convert numbers for specific metric format. We can provide metric unit as well. It reounds the number in proper format and adds correct suffixes like M (mega), m (mili), n (nano), etc.
humanize.metric(12000000, unit="W")
'12.0 MW'
humanize.metric(12000000, unit="V")
'12.0 MV'
humanize.metric(12e-10, unit="V")
'1.20 nV'
humanize.metric(12e-6, unit="V")
'12.0 μV'
humanize.metric(12.4567e-6, unit="V", precision=4)
'12.46 μV'
humanize.metric(12.4567e-3, unit="V", precision=4)
'12.46 mV'

5. Scientific Notation

  • scientific() - In order to represent number in scientific notation (z.wq x 10ⁿ) this function can be used. It
'3.00 x 10⁻¹'
'2.00 x 10⁵'
'5.00 x 10⁻⁵'

6. Human Readable Floats

  • fractional() - If you want to convert floats to fractional representation then this function is what you need. It'll perform proper conversion from float to fractional representation of it.
'1 1/4'


In this blog post, I explain how you can format numbers, dates, time, time deltas, file sizes, etc using Python library humanize. Feel free to contact us if you have any doubts or questions.

Sunny Solanki  Sunny Solanki

YouTube Subscribe Comfortable Learning through Video Tutorials?

If you are more comfortable learning through video tutorials then we would recommend that you subscribe to our YouTube channel.

Need Help Stuck Somewhere? Need Help with Coding? Have Doubts About the Topic/Code?

When going through coding examples, it's quite common to have doubts and errors.

If you have doubts about some code examples or are stuck somewhere when trying our code, send us an email at coderzcolumn07@gmail.com. We'll help you or point you in the direction where you can find a solution to your problem.

You can even send us a mail if you are trying something new and need guidance regarding coding. We'll try to respond as soon as possible.

Share Views Want to Share Your Views? Have Any Suggestions?

If you want to

  • provide some suggestions on topic
  • share your views
  • include some details in tutorial
  • suggest some new topics on which we should create tutorials/blogs
Please feel free to contact us at coderzcolumn07@gmail.com. We appreciate and value your feedbacks. You can also support us with a small contribution by clicking DONATE.

Subscribe to Our YouTube Channel

YouTube SubScribe

Newsletter Subscription