You can try something like:
sum(c1 != c2 for c1,c2 in zip(w1,w2))
zip(w1,w2)
creates a generator that returns tuples consisting of corresponding letters of w1
and w2
. i.e.:
>>> list(zip(w1,w2))
[('h', 'j'), ('e', 'e'), ('l', 'l'), ('l', 'l'), ('o', 'y')]
We iterate over these tuples (c1
gets assigned to each first char and c2
to each second char) and check if c1 != c2
. We add up all the instances for which this condition is satisfied to arrive at out answer.
(See zip()
and sum()
)
>>> w1 = 'hello'
>>> w2 = 'jelly'
>>>
>>> sum(c1 != c2 for c1,c2 in zip(w1,w2))
2