{"id":2734,"library":"python-stdnum","title":"Python Standardized Numbers and Codes","description":"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.","status":"active","version":"2.2","language":"en","source_language":"en","source_url":"https://github.com/arthurdejong/python-stdnum/","tags":["standardized numbers","validation","identification codes","checksum","VAT","ISBN","IBAN"],"install":[{"cmd":"pip install python-stdnum","lang":"bash","label":"Install latest version"}],"dependencies":[],"imports":[{"note":"Common pattern for importing a specific number format module (e.g., 'isbn', 'us.ssn', 'nl.btw').","symbol":"isbn","correct":"from stdnum import isbn"},{"note":"Used for dynamically retrieving a country-specific module by country code and name (e.g., `get_cc_module('nl', 'vat')`).","symbol":"get_cc_module","correct":"from stdnum import get_cc_module"}],"quickstart":{"code":"from stdnum import isbn\nfrom stdnum.exceptions import InvalidChecksum, InvalidLength\n\n# Validate an ISBN\ntry:\n    validated_isbn = isbn.validate('978-0471117094')\n    print(f\"Validated ISBN: {validated_isbn}\")\nexcept (InvalidChecksum, InvalidLength) as e:\n    print(f\"Validation failed: {e}\")\n\n# Format an ISBN\nformatted_isbn = isbn.format('9780471117094')\nprint(f\"Formatted ISBN: {formatted_isbn}\")\n\n# Check validity without raising exceptions\nis_valid = isbn.is_valid('978-0471117094')\nprint(f\"Is valid (using is_valid): {is_valid}\")\n","lang":"python","description":"This quickstart demonstrates how to validate and format an International Standard Book Number (ISBN) using `stdnum.isbn`. It shows how to catch validation-related exceptions and how to use `is_valid()` for a boolean check without exceptions."},"warnings":[{"fix":"Ensure your project's Python environment is version 3.8 or later before upgrading to python-stdnum 2.0 or newer.","message":"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.","severity":"breaking","affected_versions":">=2.0"},{"fix":"Wrap `validate()` calls in `try...except stdnum.exceptions.StdnumException` blocks or use `is_valid()` for simple boolean checks. For example, `try: stdnum.isbn.validate(num) except stdnum.exceptions.InvalidChecksum: ...`","message":"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.","severity":"gotcha","affected_versions":"All"},{"fix":"It is best practice to `validate()` a number before attempting to `compact()` or `format()` it, especially if the input source is untrusted or prone to errors.","message":"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.","severity":"gotcha","affected_versions":"All"}],"env_vars":null,"last_verified":"2026-04-10T00:00:00.000Z","next_check":"2026-07-09T00:00:00.000Z"}