Skip to content

Iterate through string Python | Example code

  • by

Using the for loop construct can Iterate through the string in Python. If you need access to the index as you iterate through the string, use enumerate() function.

Examples Iterate through string Python

Simple example code.

Using for-loop

for c in "string":
    print(c)

Output:

Iterate through string Python

Use enumerate() function

Get index and char values. Iterate over character in a string with index.

for i, c in enumerate('Hello'):
    print(i, c)

Output:

0 H
1 e
2 l
3 l
4 o

Do comment if you have any doubts and suggestions on this Python iterate topic,

Note: IDE: PyCharm 2021.3.3 (Community Edition)

Windows 10

Python 3.10.1

All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.

Leave a Reply

Discover more from Tutorial

Subscribe now to keep reading and get access to the full archive.

Continue reading