Python humanize utilities for converting machine-readable data into human-friendly formats
npx @tessl/cli install tessl/pypi-humanize@4.13.0A comprehensive Python library that transforms machine-readable data into human-friendly formats. Humanize provides utilities for converting numbers into readable formats, dates and times into natural language expressions, file sizes into human-readable units, and includes extensive localization support for over 25 languages.
pip install humanizeimport humanizeImport specific functions:
from humanize import naturalsize, intcomma, naturaltimeImport from submodules:
from humanize.time import naturaldelta, precisedelta
from humanize.number import intword, ordinalimport humanize
import datetime as dt
# Format numbers with commas
print(humanize.intcomma(1000000)) # "1,000,000"
# Convert numbers to words
print(humanize.intword(1200000)) # "1.2 million"
# Format file sizes
print(humanize.naturalsize(1024)) # "1.0 kB"
# Natural time expressions
now = dt.datetime.now()
past = now - dt.timedelta(minutes=30)
print(humanize.naturaltime(past)) # "30 minutes ago"
# Create natural lists
items = ["apples", "oranges", "bananas"]
print(humanize.natural_list(items)) # "apples, oranges and bananas"The humanize library is organized into focused modules:
All functions handle edge cases gracefully, support various input types, and provide consistent error handling by returning string representations of invalid inputs.
Convert numbers into human-readable formats including comma separation, word conversion, ordinals, fractions, scientific notation, and metric prefixes with SI units.
def intcomma(value: float | str, ndigits: int | None = None) -> str: ...
def intword(value: float | str, format: str = "%.1f") -> str: ...
def ordinal(value: float | str, gender: str = "male") -> str: ...
def apnumber(value: float | str) -> str: ...
def fractional(value: float | str) -> str: ...
def scientific(value: float | str, precision: int = 2) -> str: ...
def clamp(value: float, format: str = "{:}", floor: float | None = None, ceil: float | None = None, floor_token: str = "<", ceil_token: str = ">") -> str: ...
def metric(value: float, unit: str = "", precision: int = 3) -> str: ...Natural language time and date formatting with relative expressions, precision control, and support for various input types including datetime objects, timedeltas, and seconds.
def naturaltime(value: dt.datetime | dt.timedelta | float, future: bool = False, months: bool = True, minimum_unit: str = "seconds", when: dt.datetime | None = None) -> str: ...
def naturaldelta(value: dt.timedelta | float, months: bool = True, minimum_unit: str = "seconds") -> str: ...
def naturaldate(value: dt.date | dt.datetime) -> str: ...
def naturalday(value: dt.date | dt.datetime, format: str = "%b %d") -> str: ...
def precisedelta(value: dt.timedelta | float | None, minimum_unit: str = "seconds", suppress: Iterable[str] = (), format: str = "%0.2f") -> str: ...Convert byte counts into human-readable file size representations with support for decimal (SI) and binary (IEC) units, plus GNU-style formatting.
def naturalsize(value: float | str, binary: bool = False, gnu: bool = False, format: str = "%.1f") -> str: ...Convert Python lists into natural language with proper comma placement and conjunction usage.
def natural_list(items: list[Any]) -> str: ...Activate and manage localization for 25+ supported languages, with locale-specific number and date formatting.
def activate(locale: str | None, path: str | os.PathLike[str] | None = None) -> gettext.NullTranslations: ...
def deactivate() -> None: ...
def thousands_separator() -> str: ...
def decimal_separator() -> str: ...# Type aliases used throughout the library
NumberOrString = float | str
# Datetime/time types
import datetime as dt
from typing import Any, Iterable
import os
import gettext