How to check if two strings contain same letters and numbers in Python?
from string import ascii_letters, digits def compare_alphanumeric(first, second): for character in first: if character in ascii_letters + digits and character not in second: return False return True str1 = 'ABCD' str2 = 'ACDB' print(compare_alphanumeric(str1, str2)) str1 = 'A45BCD' str2 = 'ACD59894B' print(compare_alphanumeric(str1, str2)) str1 = 'A45BCD' str2 = 'XYZ9887' print(compare_alphanumeric(str1, str2))
Output
True True False