{"id":8298,"library":"luhn","title":"Luhn Check Digit Generator and Verifier","description":"The `luhn` Python library, currently at version 0.2.0, provides a simple and lightweight implementation of the Luhn algorithm. It's designed to generate and verify Luhn check digits for identification numbers, commonly used for credit cards and other IDs to detect accidental errors. This version was released in 2015 and has a stable, albeit minimal, API.","status":"maintenance","version":"0.2.0","language":"en","source_language":"en","source_url":"https://github.com/mmcloughlin/luhn","tags":["luhn","checksum","validation","credit card","utility"],"install":[{"cmd":"pip install luhn","lang":"bash","label":"Install with pip"}],"dependencies":[],"imports":[{"symbol":"verify","correct":"from luhn import verify"},{"symbol":"generate","correct":"from luhn import generate"},{"symbol":"append","correct":"from luhn import append"},{"note":"Commonly used for convenience to import all functions, but explicit imports are generally recommended for clarity.","symbol":"*","correct":"from luhn import *"}],"quickstart":{"code":"from luhn import verify, generate, append\n\n# Verify a Luhn compliant string\nvalid_number = '79927398713'\ninvalid_number = '79927398710'\nprint(f\"'{valid_number}' is valid: {verify(valid_number)}\")\nprint(f\"'{invalid_number}' is valid: {verify(invalid_number)}\")\n\n# Generate a check digit for a string\nbase_number = '53461861341123'\ncheck_digit = generate(base_number)\nprint(f\"Check digit for '{base_number}' is: {check_digit}\")\n\n# Append a check digit to a string\nappended_number = append(base_number)\nprint(f\"'{base_number}' with check digit appended: {appended_number}\")","lang":"python","description":"Demonstrates how to verify a number, generate a check digit, and append a check digit using the `luhn` library's primary functions."},"warnings":[{"fix":"Ensure input strings are stripped of any non-digit characters before passing them to `verify`, `generate`, or `append`. For example, `number_string.replace(' ', '').replace('-', '')`.","message":"The `luhn` library expects string input consisting solely of digits. Passing non-digit characters (e.g., spaces, hyphens, letters) will result in a `ValueError` during the internal conversion to integers.","severity":"gotcha","affected_versions":"0.1.0, 0.1.1, 0.2.0"},{"fix":"For applications requiring higher security or more robust error detection, consider alternative or additional checksum algorithms, such as Verhoeff or Damm algorithms, or cryptographic hash functions.","message":"The Luhn algorithm itself is designed to detect single-digit errors and most adjacent digit transpositions, but it is not cryptographically secure and will not detect all types of transcription errors (e.g., '09' to '90' transposition).","severity":"gotcha","affected_versions":"All versions (inherent to Luhn algorithm)"},{"fix":"While functional for basic Luhn operations, consider evaluating more recent alternatives like `luhncheck` if your project requires new features, active maintenance, or broader compatibility.","message":"This library (version 0.2.0, released in 2015) is older and may not receive active development or updates. Newer, more actively maintained Luhn implementations (e.g., `luhncheck`) are available on PyPI, offering additional features or potentially better performance.","severity":"deprecated","affected_versions":"0.2.0 and earlier"}],"env_vars":null,"last_verified":"2026-04-16T00:00:00.000Z","next_check":"2026-07-15T00:00:00.000Z","problems":[{"fix":"Pre-process the input string to remove any non-digit characters. Example: `cleaned_number = input_string.strip().replace(' ', '').replace('-', '')`.","cause":"The input string contained non-digit characters (e.g., spaces, hyphens, letters) which cannot be converted to an integer by the library's internal `map(int, string)` call.","error":"ValueError: invalid literal for int() with base 10:"},{"fix":"Convert the integer to a string before passing it to the `luhn` functions. Example: `verify(str(12345))`.","cause":"An integer was passed to a function (e.g., `verify`, `generate`, `append`) that expects a string of digits.","error":"TypeError: 'int' object is not iterable"},{"fix":"Review the specifications of the Luhn algorithm to understand its inherent strengths and weaknesses. If more robust error detection is needed, consider alternative checksums.","cause":"This is typically not a bug in the `luhn` library itself (which correctly implements the standard algorithm), but rather a misunderstanding of the Luhn algorithm's limitations, such as its inability to detect certain transposition errors (e.g., 09-90).","error":"Incorrect Luhn validation result for long numbers or specific patterns."}]}