Program to validate IBAN Numbers
Program to validate IBAN Numbers
Python Challenge for Intermediates
This is a Python challenge for intermediates where you should develop a program to validate IBAN numbers.
International Bank Account Number (IBAN) is a standardized international bank account numbering system used to identify bank accounts when making cross-border transactions. It’s a unique alphanumeric code that helps ensure seamless and error-free processing of international payments. The reasons for IBAN Numbers are:
- Identification: IBAN numbers help identify specific bank accounts when conducting international transactions, preventing errors and ensuring funds reach the intended recipient.
- Standardization: It facilitates efficient and accurate processing of cross-border payments across different countries and financial institutions thanks to to its uniform format for bank account identification
- Reduced Errors: Due to its standardized structure, IBAN minimizes the risk of errors during the manual entry of bank account details, thereby enhancing the security and reliability of international transactions.
Challenge instructions
Create a Python function that validates if an International Bank Account Number (IBAN) input by the user is a valid IBAN. The program must validate if any alphanumeric code entered is a correct IBAN. Use the following steps:
-
Prompt the user to enter an IBAN number.
-
Remove any spaces from the input and move the first 4 characters to the end of the string.
-
Replace each letter in the string with two digits, thereby expanding the string, where A=10, B=11, …, Z=35.
-
Convert the string to an integer.
-
Perform modulo 97 on the integer: The resulting number is then divided by 97. If the remainder of this division is 1, the IBAN number is valid.
The modulo 97 check ensures that the IBAN number has been accurately constructed and is suitable for use in international banking transactions. If the computed result is not equal to 1, the IBAN is deemed invalid. -
If the result is 1, the IBAN is valid; otherwise, it is invalid.
Source Code
def validate_iban(iban):
iban = iban.replace(' ', '') # Remove spaces from the input
iban = iban[4:] + iban[:4] # Move the first 4 characters to the end of the string
iban_num = ''
# Replace each letter in the string with two digits
for char in iban:
if char.isalpha():
iban_num += str(10 + ord(char) - ord('A'))
else:
iban_num += char
iban_int = int(iban_num)
# Check the validity of the IBAN
if iban_int % 97 == 1:
return True
else:
return False
# Prompt the user to enter an IBAN number
user_input = input("Enter the IBAN number to validate: ")
# Validate the input IBAN number
if validate_iban(user_input):
print("The IBAN number is valid.")
else:
print("The IBAN number is invalid.")
