w3resource

Python: Print the index of the character in a string

Python String: Exercise-44 with Solution

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

Python String Exercises: Print the index of the character in a string

Sample Solution:

Python Code:

# Define a string 'str1'.
str1 = "w3resource"

# Iterate through the characters of the string using enumeration.
# 'index' contains the position of the character, and 'char' contains the character itself.
for index, char in enumerate(str1):
    # Print the current character, its position, and a descriptive message.
    print("Current character", char, "position at", index)

Sample Output:

Current character w position at 0                                                                             
Current character 3 position at 1                                                                             
Current character r position at 2                                                                             
Current character e position at 3                                                                             
Current character s position at 4                                                                             
Current character o position at 5                                                                             
Current character u position at 6                                                                             
Current character r position at 7                                                                             
Current character c position at 8                                                                             
Current character e position at 9 

Flowchart:

Flowchart: Print the index of the character in a string

Python Code Editor:

Previous: Write a Python program to print the square and cube symbol in the area of a rectangle and volume of a cylinder.
Next: Write a Python program to check if a string contains all letters of the alphabet.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://198.211.115.131/python-exercises/string/python-data-type-string-exercise-44.php