Luhn Check Digit Generator and Verifier

0.2.0 · maintenance · verified Thu Apr 16

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.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates how to verify a number, generate a check digit, and append a check digit using the `luhn` library's primary functions.

from luhn import verify, generate, append

# Verify a Luhn compliant string
valid_number = '79927398713'
invalid_number = '79927398710'
print(f"'{valid_number}' is valid: {verify(valid_number)}")
print(f"'{invalid_number}' is valid: {verify(invalid_number)}")

# Generate a check digit for a string
base_number = '53461861341123'
check_digit = generate(base_number)
print(f"Check digit for '{base_number}' is: {check_digit}")

# Append a check digit to a string
appended_number = append(base_number)
print(f"'{base_number}' with check digit appended: {appended_number}")

view raw JSON →