Python humanize utilities
Project Links
Meta
Author: Jason Moiron
Maintainer: Hugo van Kemenade
Requires Python: >=3.10
Classifiers
Development Status
- 5 - Production/Stable
Intended Audience
- Developers
Operating System
- OS Independent
Programming Language
- Python
- Python :: 3 :: Only
- Python :: 3.10
- Python :: 3.11
- Python :: 3.12
- Python :: 3.13
- Python :: 3.14
- Python :: Implementation :: CPython
- Python :: Implementation :: PyPy
Topic
- Text Processing
- Text Processing :: General
humanize
This modest package contains various common humanization utilities, like turning a number into a fuzzy human-readable duration ("3 minutes ago") or into a human-readable size or throughput. It is localized to:
- Arabic
- Basque
- Bengali
- Brazilian Portuguese
- Catalan
- Danish
- Dutch
- Esperanto
- European Portuguese
- Finnish
- French
- German
- Greek
- Hebrew
- Indonesian
- Italian
- Japanese
- Klingon
- Korean
- Norwegian
- Persian
- Polish
- Russian
- Simplified Chinese
- Slovak
- Slovenian
- Spanish
- Swedish
- Turkish
- Ukrainian
- Uzbek
- Vietnamese
API reference
https://humanize.readthedocs.io
Installation
From PyPI
python3 -m pip install --upgrade humanize
From source
git clone https://github.com/python-humanize/humanize
cd humanize
python3 -m pip install -e .
Usage
Integer humanization
>>> import humanize
>>> humanize.intcomma(12345)
'12,345'
>>> humanize.intword(123455913)
'123.5 million'
>>> humanize.intword(12345591313)
'12.3 billion'
>>> humanize.apnumber(4)
'four'
>>> humanize.apnumber(41)
'41'
Date & time humanization
>>> import humanize
>>> import datetime as dt
>>> humanize.naturalday(dt.datetime.now())
'today'
>>> humanize.naturaldelta(dt.timedelta(seconds=1001))
'16 minutes'
>>> humanize.naturalday(dt.datetime.now() - dt.timedelta(days=1))
'yesterday'
>>> humanize.naturalday(dt.date(2007, 6, 5))
'Jun 05'
>>> humanize.naturaldate(dt.date(2007, 6, 5))
'Jun 05 2007'
>>> humanize.naturaltime(dt.datetime.now() - dt.timedelta(seconds=1))
'a second ago'
>>> humanize.naturaltime(dt.datetime.now() - dt.timedelta(seconds=3600))
'an hour ago'
Precise time delta
>>> import humanize
>>> import datetime as dt
>>> delta = dt.timedelta(seconds=3633, days=2, microseconds=123000)
>>> humanize.precisedelta(delta)
'2 days, 1 hour and 33.12 seconds'
>>> humanize.precisedelta(delta, minimum_unit="microseconds")
'2 days, 1 hour, 33 seconds and 123 milliseconds'
>>> humanize.precisedelta(delta, suppress=["days"], format="%0.4f")
'49 hours and 33.1230 seconds'
Smaller units
If seconds are too large, set minimum_unit
to milliseconds or microseconds:
>>> import humanize
>>> import datetime as dt
>>> humanize.naturaldelta(dt.timedelta(seconds=2))
'2 seconds'
>>> delta = dt.timedelta(milliseconds=4)
>>> humanize.naturaldelta(delta)
'a moment'
>>> humanize.naturaldelta(delta, minimum_unit="milliseconds")
'4 milliseconds'
>>> humanize.naturaldelta(delta, minimum_unit="microseconds")
'4 milliseconds'
>>> humanize.naturaltime(delta)
'now'
>>> humanize.naturaltime(delta, minimum_unit="milliseconds")
'4 milliseconds ago'
>>> humanize.naturaltime(delta, minimum_unit="microseconds")
'4 milliseconds ago'
File size humanization
>>> import humanize
>>> humanize.naturalsize(1_000_000)
'1.0 MB'
>>> humanize.naturalsize(1_000_000, binary=True)
'976.6 KiB'
>>> humanize.naturalsize(1_000_000, gnu=True)
'976.6K'
Human-readable floating point numbers
>>> import humanize
>>> humanize.fractional(1/3)
'1/3'
>>> humanize.fractional(1.5)
'1 1/2'
>>> humanize.fractional(0.3)
'3/10'
>>> humanize.fractional(0.333)
'333/1000'
>>> humanize.fractional(1)
'1'
Scientific notation
>>> import humanize
>>> humanize.scientific(0.3)
'3.00 x 10⁻¹'
>>> humanize.scientific(500)
'5.00 x 10²'
>>> humanize.scientific("20000")
'2.00 x 10⁴'
>>> humanize.scientific(1**10)
'1.00 x 10⁰'
>>> humanize.scientific(1**10, precision=1)
'1.0 x 10⁰'
>>> humanize.scientific(1**10, precision=0)
'1 x 10⁰'
Localization
How to change locale at runtime:
>>> import humanize
>>> import datetime as dt
>>> humanize.naturaltime(dt.timedelta(seconds=3))
'3 seconds ago'
>>> _t = humanize.i18n.activate("ru_RU")
>>> humanize.naturaltime(dt.timedelta(seconds=3))
'3 секунды назад'
>>> humanize.i18n.deactivate()
>>> humanize.naturaltime(dt.timedelta(seconds=3))
'3 seconds ago'
You can pass additional parameter path
to activate
to specify a path to search
locales in.
>>> import humanize
>>> humanize.i18n.activate("xx_XX")
<...>
FileNotFoundError: [Errno 2] No translation file found for domain: 'humanize'
>>> humanize.i18n.activate("pt_BR", path="path/to/my/own/translation/")
<gettext.GNUTranslations instance ...>
How to add new phrases to existing locale files:
xgettext --from-code=UTF-8 -o humanize.pot -k'_' -k'N_' -k'P_:1c,2' -k'NS_:1,2' -k'_ngettext:1,2' -l python src/humanize/*.py # extract new phrases
msgmerge -U src/humanize/locale/ru_RU/LC_MESSAGES/humanize.po humanize.pot # add them to locale files
How to add a new locale:
msginit -i humanize.pot -o humanize/locale/<locale name>/LC_MESSAGES/humanize.po --locale <locale name>
Where <locale name>
is a locale abbreviation, eg. en_GB
, pt_BR
or just ru
, fr
etc.
List the language at the top of this README.
Oct 15, 2025
4.14.0
Aug 25, 2025
4.13.0
Apr 30, 2025
4.12.3
Mar 24, 2025
4.12.2
Feb 18, 2025
4.12.1
Feb 14, 2025
4.12.0
Oct 05, 2024
4.11.0
Jul 08, 2024
4.10.0
Nov 21, 2023
4.9.0
Aug 16, 2023
4.8.0
Jun 27, 2023
4.7.0
Feb 04, 2023
4.6.0
Jan 29, 2023
4.5.0
Sep 21, 2022
4.4.0
Aug 05, 2022
4.3.0
Jun 30, 2022
4.2.3
Jun 27, 2022
4.2.2
Jun 23, 2022
4.2.1
Jun 19, 2022
4.2.0
May 03, 2022
4.1.0
Feb 13, 2022
4.0.0
Jan 30, 2022
3.14.0
Nov 29, 2021
3.13.1
Nov 29, 2021
3.13.0
Oct 06, 2021
3.12.0
Aug 01, 2021
3.11.0
Jul 05, 2021
3.10.0
Jun 15, 2021
3.9.0
Jun 12, 2021
3.8.0
Jun 06, 2021
3.7.1
Jun 02, 2021
3.7.0
May 29, 2021
3.6.0
Apr 30, 2021
3.5.0
Apr 12, 2021
3.4.1
Apr 11, 2021
3.4.0
Mar 20, 2021
3.3.0
Dec 12, 2020
3.2.0
Oct 19, 2020
3.1.0
Oct 02, 2020
3.0.1
Sep 30, 2020
3.0.0
Aug 13, 2020
2.6.0
Jul 05, 2020
2.5.0
Jun 27, 2020
2.4.1
Apr 23, 2020
2.4.0
Apr 06, 2020
2.3.0
Mar 23, 2020
2.2.0
Mar 20, 2020
2.1.0
Mar 05, 2020
2.0.0
Mar 05, 2020
1.1.0
Feb 08, 2020
1.0.0
Nov 13, 2014
0.5.1
Jul 10, 2013
0.5
May 16, 2013
0.4
Dec 21, 2012
0.3
Jan 19, 2012
0.2
Oct 12, 2011
0.1