Sometimes, while working with Python Strings, we can have a problem in which we need to extract indices of vowels in it. This kind of application is common in day-day programming. Lets discuss certain ways in which this task can be performed.
Method #1 : Using loop
This is one way in which this task can be performed. In this we use brute force to perform this task. In this we iterate for each string element and test for vowel.
# Python3 code to demonstrate working of # Vowel indices in String # Using loop # initializing string test_str = "geeksforgeeks" # printing original string print("The original string is : " + test_str) # Vowel indices in String # Using loop res = [] for ele in range(len(test_str)): if test_str[ele] in "aeiou": res.append(ele) # printing result print("The vowel indices are : " + str(res)) |
The original string is : geeksforgeeks The vowel indices are : [1, 2, 6, 9, 10]
Method #2 : Using enumerate() + list comprehension
The combination of above methods can also be used to perform this task. In this, we access the index using enumerate() and list comprehension is used to check for vowels.
# Python3 code to demonstrate working of # Vowel indices in String # Using list comprehension + enumerate() # initializing string test_str = "geeksforgeeks" # printing original string print("The original string is : " + test_str) # Vowel indices in String # Using list comprehension + enumerate() res = [idx for idx, ele in enumerate(test_str) if ele in "aeiou"] # printing result print("The vowel indices are : " + str(res)) |
The original string is : geeksforgeeks The vowel indices are : [1, 2, 6, 9, 10]
Recommended Posts:
- Python program to check if given string is vowel Palindrome
- Python Regex - Program to accept string starting with vowel
- Python | Multiple indices Replace in String
- Python - Get the indices of Uppercase characters in given string
- Python | Get indices of True values in a binary list
- Python - Minimum element indices
- Python | Ways to find indices of value in list
- Python | Group elements at same indices in a multi-list
- Python | Get match indices
- Python | Find indices with None values in given list
- Python | Indices of numbers greater than K
- Python | Find elements of a list by indices
- Python | Indices of sorted list of list elements
- Python program to fetch the indices of true values in a Boolean list
- Python | Duplicate element indices in list
- Python | Indices list of matching element from other list
- Python | Indices of N largest elements in list
- Python | Segregate True and False value indices
- Python | Indices of Kth element value
- Python - Find the indices for k Smallest elements
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

