11. for your strings


when we say

words="eggs" and then
for c in words:
print c

how come does python recognise that it should list all the letters?

We can simply replace "c" with any letter and python will still bring all the same result. I find it hard to follow the logic behind.


confusing isn't it? Python is here doing a lot of work for you.

what python is doing under the hood, is determine the length of the string (or lists, works roughly the same) and loop over them. Lets look at the general syntax:

for iterator in string:

the iterator gets assigned each item in the list in sequence, in essence you are defining a variable which gets updated by the loop (it gets assigned the next letter until the string is over)

Python does a lot for you, this can be nice (you have to write less code) and confusing (why does this code work this)


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.