How to check for single character change in a string?

Ian Kelly ian.g.kelly at gmail.com
Sat Dec 24 10:57:01 EST 2011


On Sat, Dec 24, 2011 at 8:26 AM,  <tinnews at isbd.co.uk> wrote:
> Can anyone suggest a simple/easy way to count how many characters have
> changed in a string?
>
> E.g. giving results as follows:-
>
>    abcdefg     abcdefh         1
>    abcdefg     abcdekk         2
>    abcdefg     gfedcba         6
>
>
> Note that position is significant, a character in a different position
> should not count as a match.
>
> Is there any simpler/neater way than just a for loop running through
> both strings and counting non-matching characters?

No, but the loop approach is pretty simple:

sum(a == b for a, b in zip(str1, str2))



More information about the Python-list mailing list