Python Humanize Utilities

4.15.0 · active · verified Sat Mar 28

humanize is a Python library (current version 4.15.0) that provides a collection of utilities to make data more human-readable. It transforms raw numerical values, dates, times, and file sizes into intuitive and easily understandable formats, bridging the gap between machine-centric data and human comprehension. The project maintains an active release cadence, with frequent updates and a focus on expanding localization support.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates common `humanize` functionalities, including formatting numbers with commas or words, converting numbers to ordinals, presenting dates and times in natural language, and converting byte counts into human-readable file sizes.

import humanize
import datetime as dt

# Humanize numbers
print(f"Commas: {humanize.intcomma(1234567)}")
print(f"Words: {humanize.intword(1234567890)}")
print(f"Ordinal: {humanize.ordinal(21)}")

# Humanize dates and times
now = dt.datetime.now()
one_hour_ago = now - dt.timedelta(hours=1, minutes=5)
tomorrow = now + dt.timedelta(days=1)
print(f"Time ago: {humanize.naturaltime(one_hour_ago)}")
print(f"Time from now: {humanize.naturaltime(tomorrow, future=True)}")
print(f"Natural day: {humanize.naturalday(now)}")

# Humanize file sizes
print(f"File size: {humanize.naturalsize(1024 * 1024 * 1.5)}")

view raw JSON →