Header Ads Widget

⚡ Premium Tools Hub • EXE Apps + Full Python Source Code
Lite • Pro • Bundle Packs • Instant Download

Python Humanize Package – Convert Dates, Numbers & Data into Readable Text

Python – Humanize Package

When working with data in Python, you often see values like:

  • 2026-06-08 14:32:10
  • 86400 seconds
  • 1500000
  • 0.0000345

These values are accurate but not user-friendly.

This is where the Humanize package comes in.

It converts machine-readable data into natural human language, such as:

  • “a few seconds ago”
  • “2 hours ago”
  • “1.5 million”
  • “yesterday”

What is Humanize in Python?

The Humanize library is a Python package that:

Converts numbers, dates, and time differences into human-readable formats.

It is widely used in:

  • Web applications
  • APIs
  • Dashboards
  • Logging systems
  • Data reports

Installation

Install the package using pip:

pip install humanize

Importing Humanize

import humanize
import datetime

1. Humanize Dates (Natural Time Format)

Example: Natural time difference

import humanize
import datetime

now = datetime.datetime.now()
past = now - datetime.timedelta(days=2, hours=5)

print(humanize.naturaltime(past))

Output:

2 days ago

2. Humanize Future Dates

import datetime
import humanize

future = datetime.datetime.now() + datetime.timedelta(minutes=30)

print(humanize.naturaltime(future))

Output:

in 30 minutes

3. Humanize Numbers

Large numbers become easier to read:

import humanize

print(humanize.intword(1000000))
print(humanize.intword(2500000000))

Output:

1.0 million
2.5 billion

4. Humanize File Sizes

Perfect for file management systems.

import humanize

size = 123456789
print(humanize.naturalsize(size))

Output:

123.5 MB

5. Humanize Time Durations

Convert seconds into readable time:

import humanize

seconds = 3665
print(humanize.precisedelta(seconds))

Output:

1 hour, 1 minute, 5 seconds

6. Humanize Ordinal Numbers

Convert numbers into ordinal form:

import humanize

print(humanize.ordinal(1))
print(humanize.ordinal(22))
print(humanize.ordinal(103))

Output:

1st
22nd
103rd

7. Humanize Fractional Numbers

import humanize

print(humanize.fractional(0.5))
print(humanize.fractional(1.75))

Output:

1/2
1 3/4

Real-World Use Cases

The Humanize package is useful in:

1. Social Media Apps

  • “Posted 2 minutes ago”
  • “Liked by 1.2K users”

2. File Upload Systems

  • “File size: 2.4 MB”

3. Analytics Dashboards

  • “Revenue: 3.5 million”
  • “Users active 5 hours ago”

4. Logging Systems

  • “Error occurred 10 seconds ago”

Why Use Humanize?

Without Humanize:

  • 168000 seconds ago ❌
  • 1500000 ❌
  • 123456789 bytes ❌

With Humanize:

  • 2 days ago ✅
  • 1.5 million ✅
  • 123.5 MB ✅

Advantages

  • Improves UI/UX
  • Makes data readable
  • Reduces cognitive load
  • Easy to integrate
  • Lightweight package

Limitations

  • Only for formatting (not data processing)
  • Language support is limited
  • Not suitable for complex localization

Summary

The Humanize package in Python is a simple but powerful tool that transforms raw data into human-friendly text.

It is especially useful in modern applications built with Python where readability and user experience matter.


Conclusion

If you're building dashboards, APIs, or web applications using Python, the Humanize library is essential for improving data presentation.

Instead of showing raw numbers and timestamps, you can make your output feel natural and professional.




Post a Comment

0 Comments