Trying to understand how for loop works in python?

Thread Starter

terabaaphoonmein

Joined Jul 19, 2020
56
Code:
str1="hello"

for stringElement in str1:
    print("str1","=",stringElement)
1638112678248.png

Strings are arrays in python and they are represented like this.

Now I want to understand how the for loop is working here.

My dry run-:


Code:
for h in str1
     print h
Code:
for e in str1
     print e

and so on till o...

My confusions-:

This is what I have read about python for loop from a book called python power.

1638113281451.png

My confusion is that-:

1) How can I see array index 0 when h is being printed

2) How does python knows to increment array index by 1 after the end of each for loop? I heard strings are iterables in python and that it means that objects that are capable of returing its members one at a time. . But I am not very clear what do they really mean here in this case. So some guidance will be really valuable.
 
Top