Python Standardized Numbers and Codes
python-stdnum is a Python module designed to parse, validate, and reformat a large collection of standardized numbers and codes from various countries and international standards. Currently at version 2.2, released in January 2026, the library maintains a frequent release cadence, regularly adding support for new number formats, applying bug fixes, and enhancing existing functionalities.
Warnings
- breaking Version 2.0 (released May 2025) dropped support for Python 2.x. It now requires Python 3.8 or newer. Attempts to install or run on older Python versions will fail.
- gotcha The `validate()` function raises specific exceptions (e.g., `InvalidChecksum`, `InvalidLength`) for invalid numbers, which can halt execution if not handled. In contrast, `is_valid()` returns a boolean (`True` or `False`) and never raises an exception. Choose the appropriate function based on your error handling strategy.
- gotcha The `compact()` function generally does not perform full validation but may raise exceptions for 'wildly incorrect' inputs. The `format()` function also expects a valid number and may raise exceptions if an invalid number is passed to it.
Install
-
pip install python-stdnum
Imports
- isbn
from stdnum import isbn
- get_cc_module
from stdnum import get_cc_module
Quickstart
from stdnum import isbn
from stdnum.exceptions import InvalidChecksum, InvalidLength
# Validate an ISBN
try:
validated_isbn = isbn.validate('978-0471117094')
print(f"Validated ISBN: {validated_isbn}")
except (InvalidChecksum, InvalidLength) as e:
print(f"Validation failed: {e}")
# Format an ISBN
formatted_isbn = isbn.format('9780471117094')
print(f"Formatted ISBN: {formatted_isbn}")
# Check validity without raising exceptions
is_valid = isbn.is_valid('978-0471117094')
print(f"Is valid (using is_valid): {is_valid}")