How do for loops work in python?

In summary: I think what you want to do is create an empty string at the beginning of the for loop and add to the empty string with each iterationstr1="hello"for str_element in str1: str2 = str2 + str_element print(str2)Or if you want to just create the entirety of the new string (a copy) and then print it all at the end (instead of printing at the end of each iteration) you just change the indentstr1 = "hello"str2 = ""for str_element in str1: str2 = str2 + str_elementprint
  • #1
shivajikobardan
674
54
TL;DR Summary
how for loops work in python
Code:
str1="hello"

for stringElement in str1:
    print("str1","=",stringElement)

1638113476732.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.

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:
Technology news on Phys.org
  • #2
I think what you want to do is create an empty string at the beginning of the for loop and add to the empty string with each iteration

Code:
str1 = "hello"
str2 = ""

for str_element in str1:
    str2 = str2 + str_element
    print(str2)

Or if you want to just create the entirety of the new string (a copy) and then print it all at the end (instead of printing at the end of each iteration) you just change the indent

Code:
str1 = "hello"
str2 = ""

for str_element in str1:
    str2 = str2 + str_element

print(str2)

Did I understand your question correctly?
 
  • #3
I think you have big misconceptions.
shivajikobardan said:
My dry run-:
Code:
 for h in str1
     print h

Code:
 for e in str1
     print e
and so on till o...
Have you tried to run these codes? both are exactly equal and will produce the exact same result and the same all others 'till o'. You are just changing the variable, but there's no difference in the result.

shivajikobardan said:
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.
For 1) I think the question arises from your misconceptions, you can obtain the index of the iteration using the enumerate function.

For 2), python doesn't know to increment any index by 1, this is not what is doing. It is iterating over all the elements of the string (which are length 1 strings), so the only thing Python needs to know is what is the current element and what will be the next.
 
  • Like
Likes shivajikobardan and PhDeezNutz
  • #4
Is this what you are looking for:

Python:
str1="hello"

for index in range(len(str1)):
    print("str1[",index,"] = ",str1[index])

Which should give:

Code:
str1[ 0 ] =  h
str1[ 1 ] =  e
str1[ 2 ] =  l
str1[ 3 ] =  l
str1[ 4 ] =  o
 
  • Like
Likes shivajikobardan, jedishrfu and anorlunda
  • #6
jack action said:
Is this what you are looking for:

Python:
str1="hello"

for index in range(len(str1)):
    print("str1[",index,"] = ",str1[index])

Which should give:

Code:
str1[ 0 ] =  h
str1[ 1 ] =  e
str1[ 2 ] =  l
str1[ 3 ] =  l
str1[ 4 ] =  o
exactly. thanks...LOTS OF THANKS. This was in my book but index=0 was done there so I got confused.
 

FAQ: How do for loops work in python?

How do for loops work in python?

For loops are used for iterating over a sequence of elements in python. They allow you to repeatedly execute a block of code until a certain condition is met. The syntax for a for loop in python is: for element in sequence: where element represents a variable that will take on each value in the sequence, and sequence is a collection of elements such as a list, tuple, or string.

What is the difference between a for loop and a while loop in python?

A for loop is used when you know the number of iterations you want to perform, while a while loop is used when you want to repeat a block of code until a certain condition is met. For loops are commonly used for iterating over a sequence of elements, while while loops are often used for implementing control flow and handling user input.

Can you give an example of a for loop in python?

Yes, here is an example of a for loop that prints out the numbers from 1 to 10:

for i in range(1, 11): print(i)

How do you break out of a for loop in python?

To break out of a for loop in python, you can use the break keyword. This will immediately exit the loop and continue with the next line of code outside of the loop. You can also use the continue keyword to skip the current iteration and continue with the next one.

Can you nest for loops in python?

Yes, for loops can be nested in python. This means that you can have one for loop inside another for loop. This can be useful for iterating over multiple sequences or for creating complex patterns. However, it is important to make sure that the nested loops do not cause an infinite loop.

Similar threads

Replies
2
Views
948
Replies
3
Views
2K
Replies
10
Views
2K
Replies
16
Views
2K
Replies
3
Views
973
Replies
17
Views
2K
Replies
5
Views
3K
Replies
3
Views
1K
Replies
3
Views
693
Back
Top