Write a Python program to print the index of the character in a string

Use the for-loop and enumerate function to print the index of the character in a string.

Python program to print the index of the character in a string Example

Simple example code.

str1 = "PYTHON"

for index, char in enumerate(str1):
    print(char, "position at", index)

Output:

Python program to print index of character in string

Do comment if you have any doubts and suggestions on this python index char 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.