Python Loop Iterator


Looping Through an Iterator

We can also use a for loop to iterate through an iterable object:

Example

Iterate the values of a tuple:

mytuple = ("apple", "banana", "cherry")

for x in mytuple:
  print(x)
Try it Yourself »

Example

Iterate the characters of a string:

mystr = "banana"

for x in mystr:
  print(x)
Try it Yourself »

The for loop actually creates an iterator object and executes the next() method for each loop.



Copyright 1999-2023 by Refsnes Data. All Rights Reserved.