- #1
shivajikobardan
- 674
- 54
- TL;DR Summary
- how for loops work in python
Code:
str1="hello"
for stringElement in str1:
print("str1","=",stringElement)
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
My confusions-:
This is what I have read about python for loop from a book called python power.
https://i.stack.imgur.com/qApVt.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. But I am not very clear what do they really mean here in this case. So some guidance will be really valuable.
Last edited: