How to Return an Iterator From a String in Python


In this example, we will learn how to return an iterator from a string and print each character in Python.

Source Code

my_str = 'python'
my_iter = iter(my_str)
print(next(my_iter))
print(next(my_iter))
print(next(my_iter))
print(next(my_iter))
print(next(my_iter))
print(next(my_iter))

Output:

p
y
t
h
o
n
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments