Python - Vowel Indices in String


Python is a widely used programming language for different purposes such as web development, data science machine learning and to perform many different processes with automation. In this article, we'll explore how to use several Python features to extract vowel indices from a given text.

Different Methods to Find Vowel Indices in a String

For Loop

In this method by simply checking each element in the string, we can determine if it is a vowel and record its index. We can understand it in a better way through the following syntax and example: -

Example

def vowel_indices_in_a_string(whole_string): # The function vowel_indices_in_a_string is given the string as input
   vowels = "aeiou" # All the vowels are defined in the variable 
   vowel_indices = [] # This empty list will store the indices of the vowels in the string

   for index, char in enumerate(whole_string): # enumerate will help us in finding the indices and the character from the string given as input
      if char.lower() in vowels: # Only the lower characters of the vowels will be considered with the help of if function
         vowel_indices.append(index) # All the indices of the vowels in the string will be stored in this list

   return vowel_indices
main_string = "They are a really good group of programmers" # String is given as input
final_indices = vowel_indices_in_a_string(main_string) # The function vowel_indices_in_a_string is run 
print(final_indices) 

Output

The output of the above example will be as follows: -

[2, 5, 7, 9, 12, 13, 19, 20, 25, 26, 29, 34, 37, 40] 
# The numbering starts from 0 and space in between words is also considered as 1 indices

List Comprehension

The indexes of the vowels that are present in the string will be created in a new list utilising this technique. List comprehension is an excellent method to use the existing list to create a new list for this need. Let's take an example to understand it in a better way: -

Example

def vowel_indices_in_a_string(whole_string): # The function vowel_indices_in_a_string is given the string as input
   vowels = "aeiou" # The vowels are defined in a variable
   return [index for index, char in enumerate(whole_string) if char.lower() in vowels] # With the help of list comprehension we check each element present in the string and with the help of if statement we will consider only the lower case characters

#Example
main_string = "They are a really good group of programmers" # String is given as input
final_indices = vowel_indices_in_a_string(main_string) # The function vowel_indices_in_a_string is run 
print(final_indices) 

Output

The output of the above example will be as follows: -

[2, 5, 7, 9, 12, 13, 19, 20, 25, 26, 29, 34, 37, 40] 
# The numbering starts from 0 and space in between words is also considered as 1 indices

Regular Expression Module

By constructing a pattern and locating the exact letters described in the pattern, we are able to quickly identify the indices of vowels in a string with the support of the regular expression module. Let's take an example to understand it in a better way: -

Example

import re # Do not forget to import re module or else error might occur

def vowel_indices_in_a_string(whole_string): # The function vowel_indices_in_a_string is given the string as input
   list_comprehension_pattern = r'[aeiou]' # A new pattern is created and all the vowels are defined in it  
   vowel_indices = [m.start() for m in re.finditer(list_comprehension_pattern, whole_string, re.IGNORECASE)] # The re.finditer function will help us to find all the vowels in the string with the help of the pattern and re.ignorecase will not consider the case of the character
   return vowel_indices

#Example
main_string = "They are a really good group of programmers" # String is given as input
final_indices = vowel_indices_in_a_string(main_string) # The function vowel_indices_in_a_string is run 
print(final_indices) 

Output

The output of the example will be as follows: -

[2, 5, 7, 9, 12, 13, 19, 20, 25, 26, 29, 34, 37, 40] 
# The numbering starts from 0 and space in between words is also considered as 1 indices

Filter Function

In this method we will simply filter out the indices of the required element from the input string given with the help of lambda function along with the filter function. Let's take an example to understand it in a better way:

Example

def all_vowel(char): # A function called all_vowel is created
   vowels = "aeiou" # All the vowels are defined in the function
   return char.lower() in vowels

def vowel_indices_in_a_string(whole_string):# The function vowel_indices_in_a_string is given the string as input
   vowel_indices = list(filter(lambda x: all_vowel(whole_string[x]), range(len(whole_string)))) # The range function is used to create the indices of all the characters. Filter function is used to apply the all_vowel function to each index in the sequence generated by the range function and lambda x: is used to check if the specific element is a vowel and list will convert the whole output into a list
   return vowel_indices
#Example
main_string = "They are a really good group of programmers" # String is given as input
final_indices = vowel_indices_in_a_string(main_string) # The function vowel_indices_in_a_string is run 
print(final_indices) 

Output

The output of the above example will be as follows: -

[2, 5, 7, 9, 12, 13, 19, 20, 25, 26, 29, 34, 37, 40] 
# The numbering starts 
from 0 and space in between words is also considered as 1 indices

Numpy Library

This is a complex method as numpy library is mostly used in cases when numerical data is present in the input but it can also be used efficiently in this case also if it used properly. Let's take an example to understand it in a better way: -

Example

import numpy as np # Do not forget to import numpy or else error might occur

def vowel_indices_in_a_string(whole_string):# The function vowel_indices_in_a_string is given the string as input
   vowels = "aeiou" # The vowels are defined with the help of a variable
   lowercase_input = whole_string.lower() # All the characters are converted into lower case so that all the characters are treated equally 
   all_vowel = np.array([char in vowels for char in lowercase_input]) # An array is created with the help of np.array using a list comprehension. Each element will check if the corresponding element is a vowel or not
   vowel_indices = list(np.where(all_vowel)[0]) #np.where function will help us to find the indices of the vowels detected in the input string
   return vowel_indices
#Example
main_string = "They are a really good group of programmers" # String is given as input
final_indices = vowel_indices_in_a_string(main_string) # The function vowel_indices_in_a_string is run 
print(final_indices)

Output

The output of the above example will be as follows: -

[2, 5, 7, 9, 12, 13, 19, 20, 25, 26, 29, 34, 37, 40]

Conclusion

It is important to have knowledge about different methods that can be used to find the indices of a vowel in a given string to become an efficient programmer. One can refer the methods given in the above article and use any of the methods mentioned above as per convenience.

Updated on: 07-Aug-2023

174 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements