Python enumerate string | Example code

Using the Python enumerate function in the string, you can loop over it and will get value with the index. The Enumerate() command adds a counter to each item of the iterable object and returns an enumerate object as an output string.

enumerate(iterable, startIndex)

Enumerate allows you to loop through a list, tuple, dictionary, string, and gives the values along with the index.

Python enumerate string

Simple example code Iterating each character in a string using Python.

my_str = "HELLO"

for i in enumerate(my_str):
    print(i)

Output: the result will show you the index and value for each character of the string.

Python enumerate string

Do comment if you have any doubts and suggestions on this Python enumerate topic.

Note: IDE: PyCharm 2021.1.3 (Community Edition)

Windows 10

Python 3.7

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


Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.