Every IBAN carries its own checksum: two digits right after the country code. You can catch almost every typo in a bank account number with about ten lines of Python, before a payment bounces and the bank charges you a fee.
How the IBAN check works (ISO 13616, mod 97)
- Remove spaces and make the letters upper case.
- Check the length for the country (DE = 22, FR = 27, NL = 18, BG = 22, ES = 24, IT = 27, GB = 22).
- Move the first 4 characters (country code + check digits) to the end.
- Replace every letter with two digits: A = 10, B = 11 ... Z = 35.
- Read the result as one big number. If
number % 97 == 1, the IBAN is valid.
Python has arbitrary-size integers, so step 5 is one line.
LENGTHS = {"DE": 22, "FR": 27, "NL": 18, "BG": 22, "ES": 24, "IT": 27,
"GB": 22, "AT": 20, "BE": 16, "CH": 21, "PL": 28, "RO": 24}
def iban_ok(iban: str) -> tuple[bool, str]:
s = iban.replace(" ", "").upper()
if not s.isalnum():
return False, "only letters and digits are allowed"
want = LENGTHS.get(s[:2])
if want and len(s) != want:
return False, f"{s[:2]} IBANs have {want} characters, this one has {len(s)}"
moved = s[4:] + s[:4]
digits = "".join(str(int(c, 36)) for c in moved) # int("A", 36) == 10
if int(digits) % 97 != 1:
return False, "checksum wrong: probably a typo"
return True, "ok"
for x in ["DE89 3704 0044 0532 0130 00", # documented example IBAN
"DE89 3704 0044 0532 0130 01", # one digit changed
"GB82 WEST 1234 5698 7654 32"]: # documented example IBAN
print(x, iban_ok(x))
Output:
DE89 3704 0044 0532 0130 00 (True, 'ok')
DE89 3704 0044 0532 0130 01 (False, 'checksum wrong: probably a typo')
GB82 WEST 1234 5698 7654 32 (True, 'ok')
Check a whole list from a CSV
import csv
with open("suppliers.csv", newline="", encoding="utf-8") as f, \
open("suppliers_checked.csv", "w", newline="", encoding="utf-8") as out:
rows = csv.DictReader(f)
w = csv.DictWriter(out, fieldnames=rows.fieldnames + ["iban_ok", "iban_note"])
w.writeheader()
for r in rows:
ok, note = iban_ok(r["iban"])
w.writerow({**r, "iban_ok": ok, "iban_note": note})
What mod 97 does NOT tell you
- It does not prove the account exists. It only proves the string is well formed. A closed account still passes.
- Some countries add a national check digit inside the BBAN (for example France, Italy, Spain, Belgium). A full validator checks that too.
- The bank is encoded in the BBAN part (German Bankleitzahl, UK sort code). To show the bank name you need a bank directory, not just maths.
Common mistakes I see in payment files
- The letter O typed instead of zero, or I instead of 1.
isalnum()passes them, the checksum catches them. - Copying from a PDF adds non-breaking spaces (
). Replace them too:iban.replace(" ", ""). - Excel turns long numbers into
3.70400440532013E+17when an account number column is formatted as a number. Format the column as text before you paste.
No code needed
If you only need to check one IBAN, or you want to send the check to a colleague who does not write Python, the free IBAN checker does the same checks, including the national check digit, and shows the bank. There is also a page per country with the exact layout, for example German IBAN format.
What else do you validate before you send a payment file?
Disclosure: I build the tool used in this post, and the article was written with AI assistance. If a code sample does not work for you, tell me in the comments and I will fix it.
Top comments (1)
The length check before the mod 97 step is a nice touch, since a wrong-length IBAN can occasionally still pass the checksum. It also only proves the format is valid, not that the account actually exists.