[Tutor] trouble with function-- trying to check differencesbtwn 2 strings

Alan Gauld alan.gauld at btinternet.com
Tue Mar 6 19:14:55 CET 2007


"zannah marsh" <zannah.m at gmail.com> wrote

> rikart pointed out that you need to use a range to get to the 
> indicies of
> the items in the string.
>
> for item in range(len(string))...
>     if word1[item] == word2[item]
>

There is another way which is to use enumerate which
returns both the item and its index.

>>> for i,c in enumerate('abcd'):
>>>     print c, 'at', i
a at 0
b at 1
c at 2
d at 3

But range() works just as well in this case.

-- 
Alan Gauld
Author of the Learn to Program web site
http://www.freenetpages.co.uk/hp/alan.gauld 




More information about the Tutor mailing list