Python Humanize Package Tutorial: Complete Guide

Python Humanize Package Tutorial: Complete Guide

Python Humanize Package Tutorial. In today’s software applications, developers often work with large numbers, timestamps, file sizes, and technical data that can look confusing to regular users. Values such as 123456789, 2048000, or 2026-05-07 14:45:22 may be accurate from a programming perspective, but they are not very easy to read or understand quickly.

This is where the Python Humanize package becomes incredibly helpful.

The Humanize library is designed to transform machine-oriented data into clean, human-friendly text. For example, instead of displaying 2048000 bytes, it can show 2.0 MB, and instead of printing an exact datetime value, it can display phrases like 3 hours ago.

Because of its simplicity and usefulness, the package is commonly used in many types of applications, including:

  • Web applications
  • Data dashboards
  • Reporting tools
  • Command-line utilities
  • Analytics platforms
  • File management software
  • Social media platforms
  • AI and machine learning projects

As highlighted in the official documentation and tutorials, the main purpose of Humanize is to present technical data in a way that feels more natural and readable for humans.

Python Humanize Package Tutorial

What is the Python Humanize Package?

Python Humanize is a lightweight Python library designed to make data more readable for humans.

It provides utilities for:

  • Human-readable numbers
  • File size formatting
  • Relative time formatting
  • Date formatting
  • Ordinal conversion
  • Fraction formatting
  • Duration formatting

For example:

Raw ValueHumanized Output
12345671.2 million
20480002.0 MB
33rd
datetime.now() – 2 hours2 hours ago

The library is open-source and available on the Python Package Index (PyPI).

Why Developers Love Humanize

One of the biggest challenges in software development is presenting technical data in a readable format.

Python Humanize Package Tutorial

Suppose you are building:

  • A cloud storage application
  • A social media website
  • A finance dashboard
  • A machine learning monitoring system

Displaying raw data can confuse users. Humanize solves this problem beautifully.

Benefits include:

  • Better user experience
  • Cleaner dashboards
  • Professional-looking reports
  • Easier interpretation of data
  • Reduced cognitive load for users

Many developers in the programming community also recommend using Humanize for displaying dates and durations naturally in applications.

Installing the Humanize Package

Installing Humanize is extremely simple.

pip install humanize

The package supports modern Python versions and works across multiple operating systems.

Importing Humanize

After installation, import the package like this:

import humanize

1. Formatting Large Numbers with intcomma()

Large numbers are difficult to read without commas.

Example:

This function is extremely useful in:

  • Financial applications
  • Analytics dashboards
  • Reporting systems
  • Banking software

2. Converting Numbers into Words using intword()

The intword() function converts very large numbers into readable words.

import humanize
number = 1234567890
print(humanize.intword(number))

This feature is excellent for:

  • Business reports
  • Data visualization
  • User dashboards
  • Financial applications

3. Converting Integers into Ordinal Numbers

The ordinal() function converts numbers into ordinal forms.

import humanize
print(humanize.ordinal(1))
print(humanize.ordinal(2))
print(humanize.ordinal(3))
print(humanize.ordinal(21))

Output:

1st
2nd
3rd
21st

This is useful in:

  • Ranking systems
  • Competitions
  • Education platforms
  • Leaderboards

4. AP Number Formatting

The apnumber() function converts small numbers into words.

import humanize
print(humanize.apnumber(5))
five

This helps create cleaner and more readable textual content.

5. Formatting File Sizes with naturalsize()

One of the most popular Humanize functions is naturalsize().

Instead of showing bytes, it converts them into KB, MB, or GB.

import humanize
file_size = 5242880
print(humanize.naturalsize(file_size))

Output:

5.2 MB

This feature is heavily used in:

  • File managers
  • Cloud storage systems
  • Backup software
  • Data engineering tools

The official documentation also highlights its usefulness in converting raw byte values into understandable formats.

6. Working with Human-Readable Dates

Dates are often difficult to interpret in raw format.

Humanize makes dates natural and intuitive.


7. Using naturaltime()

The naturaltime() function converts timestamps into relative time descriptions.

from datetime import datetime, timedeltaimport humanizepast_time = datetime.now() - timedelta(hours=5)print(humanize.naturaltime(past_time))

Output

5 hours ago

This feature is commonly used in:

  • Social media feeds
  • Messaging apps
  • Notifications
  • Blogging platforms

Reddit developers also frequently discuss Humanize for displaying “x minutes ago” style timestamps in applications.


8. Using naturaldate()

from datetime import dateimport humanizetoday = date.today()print(humanize.naturaldate(today))

Output

today

Another example:

from datetime import dateold_date = date(2022, 7, 15)print(humanize.naturaldate(old_date))

Output

Jul 15 2022

9. Using naturalday()

from datetime import datetime, timedeltaimport humanizetoday = datetime.now()tomorrow = today + timedelta(days=1)print(humanize.naturalday(today))print(humanize.naturalday(tomorrow))

Output

todaytomorrow

This creates a very user-friendly experience.


10. Working with Time Durations

Humanize is excellent at displaying durations.

Using naturaldelta()

from datetime import timedelta
import humanize
duration = timedelta(days=3, hours=5)
print(humanize.naturaldelta(duration))
3 days

Using precisedelta()

from datetime import timedeltaimport humanizeduration = timedelta(days=2, hours=4, minutes=30)print(humanize.precisedelta(duration))

Output

2 days, 4 hours and 30 minutes

This function is ideal for:

  • Stopwatch applications
  • Analytics dashboards
  • Time tracking software
  • Productivity applications

Real-World Use Cases of Humanize

Let’s explore practical scenarios where Humanize becomes incredibly powerful.


Social Media Platforms

Applications like social media sites use human-readable timestamps:

  • 5 minutes ago
  • 2 hours ago
  • yesterday

Humanize makes this implementation very easy.


File Upload Systems

Instead of showing:

2048000 bytes

You can show:

2 MB

This creates a cleaner user interface.


Data Science Dashboards

Machine learning dashboards often display huge numbers.

Humanize converts:

1000000

into:

1 million

This improves readability significantly.


Finance Applications

Financial software benefits greatly from:

  • Comma formatting
  • Billion/trillion representation
  • Clean numerical displays

CLI Tools

Command-line tools become more user-friendly with Humanize.

Example:

Backup completed 3 minutes ago

instead of:

Backup completed at 2026-05-07 14:20:00

Humanize with Flask Example

Here’s a simple Flask application using Humanize.

from flask import Flaskimport humanizeapp = Flask(__name__)@app.route("/")def home(): number = 1234567 return humanize.intcomma(number)if __name__ == "__main__": app.run(debug=True)

This will display:

1,234,567

Humanize with Django Example

Django already includes Humanize support.

Add this in settings.py:

INSTALLED_APPS = [ 'django.contrib.humanize',]

Then use it in templates:

{{ value|intcomma }}

Example Output:

1,000,000

Performance of Humanize

Humanize is lightweight and fast.

Advantages include:

  • Minimal memory usage
  • Fast formatting
  • Easy integration
  • Beginner-friendly API

It is suitable for both small and enterprise-level projects.


Best Practices for Using Humanize

1. Use Humanize for User Interfaces

Humanize should mainly be used for display purposes.

Avoid using formatted values for calculations.


2. Combine with Logging

Human-readable logs are easier to debug.

Example:

print(f"Backup completed {humanize.naturaltime(past_time)}")

3. Improve Dashboard Readability

Always humanize:

  • Large numbers
  • File sizes
  • Time durations
  • Timestamps

This improves user experience tremendously.


Common Mistakes Beginners Make

Forgetting to Import Humanize

import humanize

Always import before use.


Passing Incorrect Data Types

Some functions require specific input types.

For example:

  • naturaltime() expects datetime objects
  • naturalsize() expects numbers

Using Humanized Values for Computation

Humanized strings should only be used for display.

Avoid calculations like:

"2 million" + 500

Humanize vs Manual Formatting

Without Humanize:

size = 2048000 / (1024 * 1024)print(f"{size:.2f} MB")

With Humanize:

import humanizeprint(humanize.naturalsize(2048000))

Humanize dramatically reduces code complexity.


Advantages of Humanize

FeatureBenefit
Easy APIBeginner friendly
LightweightFast performance
Readable OutputBetter UX
Multiple UtilitiesVersatile package
Open SourceFree to use

Limitations of Humanize

Although Humanize is excellent, it has some limitations.

  • Mainly focused on display formatting
  • Not intended for calculations
  • Localization support may vary depending on functions
  • Limited customization in some utilities

Still, for most applications, it performs exceptionally well.


Humanize Localization Support

The package supports multiple languages and localization options. The official PyPI project mentions support for several international languages including French, German, Japanese, Korean, Russian, Chinese, and more.

This makes Humanize suitable for global applications.


Future of Humanize

As user experience becomes increasingly important in software development, libraries like Humanize are becoming more essential.

Modern applications focus heavily on:

  • Better readability
  • Cleaner dashboards
  • Natural language formatting
  • Human-centered design

Humanize perfectly aligns with these trends.

The Humanize package is one of the most practical utility libraries available for Python developers.

It transforms raw machine data into readable and user-friendly formats with minimal code. Whether you are building a web application, dashboard, reporting tool, CLI utility, or analytics system, Humanize can significantly improve the overall user experience.

Its simple syntax, lightweight design, and powerful utilities make it an excellent addition to any Python project.

If you regularly work with:

  • Numbers
  • Dates
  • Time durations
  • File sizes
  • Reports
  • User dashboards

then Humanize is definitely worth learning and using.

Author

Sona Avatar

Written by

Leave a Reply

Trending

CodeMagnet

Your Magnetic Resource, For Coding Brilliance

Programming Languages

Web Development

Data Science and Visualization

Career Section

<script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-4205364944170772"
     crossorigin="anonymous"></script>