calculate the hamming distance between two equal length strings in information theory the hamming distance between two strings of equal length is the number of positions at which the corresponding symbols are different https en wikipedia orgwikihammingdistance args string1 str sequence 1 string2 str sequence 2 returns int hamming distance hammingdistancepython python 0 hammingdistancekarolin kathrin 3 hammingdistance00000 11111 5 hammingdistancekarolin kath traceback most recent call last valueerror string lengths must match calculate the hamming distance between two equal length strings in information theory the hamming distance between two strings of equal length is the number of positions at which the corresponding symbols are different https en wikipedia org wiki hamming_distance args string1 str sequence 1 string2 str sequence 2 returns int hamming distance hamming_distance python python 0 hamming_distance karolin kathrin 3 hamming_distance 00000 11111 5 hamming_distance karolin kath traceback most recent call last valueerror string lengths must match
def hamming_distance(string1: str, string2: str) -> int:
    
    if len(string1) != len(string2):
        raise ValueError("String lengths must match!")

    count = 0

    for char1, char2 in zip(string1, string2):
        if char1 != char2:
            count += 1

    return count


if __name__ == "__main__":
    import doctest

    doctest.testmod()