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

Installation

  • pip install humanize

Youtube Video

import humanize
    
    print(humanize.__version__)
    
4.7.0
    

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)
    
'12,000,000'
humanize.intcomma(12000000.5)
    
'12,000,000.5'
humanize.intcomma(12000000.56758, ndigits=2)
    
'12,000,000.57'
humanize.intword(1200)
    
'1.2 thousand'
humanize.intword(1200000)
    
'1.2 million'
humanize.intword(12000000000)
    
'12.0 billion'
humanize.intword(12000000000000)
    
'12.0 trillion'
humanize.intword(12000000000000000)
    
'12.0 quadrillion'
humanize.apnumber(3)
    
'three'
humanize.ordinal(1)
    
'1st'
humanize.ordinal(11)
    
'11th'
humanize.ordinal(125)
    
'125th'
humanize.ordinal(122)
    
'122nd'
humanize.ordinal(123)
    
'123rd'

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
    
    humanize.naturaldate(datetime(2022,1,7))
    
'Jan 07 2022'
humanize.naturaldate(date(2022,1,7))
    
'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.
humanize.naturalday(date(2022,1,7))
    
'Jan 07'
humanize.naturalday(datetime.now())
    
'today'
humanize.naturalday(datetime.now() - timedelta(days=1))
    
'yesterday'
humanize.naturalday(datetime.now() - timedelta(days=2))
    
'Jul 03'
humanize.naturalday(datetime.now() + timedelta(days=1))
    
'tomorrow'

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.
humanize.naturaltime(datetime.now())
    
'now'
humanize.naturaltime(datetime(2023,1,1))
    
'6 months ago'
humanize.naturaltime(timedelta(5,5,5))
    
'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.
humanize.naturaldelta(timedelta(days=5))
    
'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'
humanize.precisedelta(timedelta(seconds=1001))
    
'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.
humanize.naturalsize(5)
    
'5 Bytes'
humanize.naturalsize(500)
    
'500 Bytes'
humanize.naturalsize(1500)
    
'1.5 kB'
humanize.naturalsize(15000000)
    
'15.0 MB'
humanize.naturalsize(15000000000)
    
'15.0 GB'
humanize.naturalsize(15000000000000)
    
'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)
    
'13.6T'

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
humanize.scientific(0.3)
    
'3.00 x 10⁻¹'
humanize.scientific("200000")
    
'2.00 x 10⁵'
humanize.scientific(0.00005)
    
'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.
humanize.fractional(0.5)
    
'1/2'
humanize.fractional(0.75)
    
'3/4'
humanize.fractional(0.25)
    
'1/4'
humanize.fractional(1.25)
    
'1 1/4'
humanize.fractional(0.33)
    
'33/100'

Summary

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