Check if String Contains Vowels in Python

Previously we had to check if a string starts with a vowel in python. Now in this post, we will check if string contains vowels in python. In the English language, the letters A, E, I, O, U, and a, e, i, o, u are defined as a vowel. Except these all other letters are consonants.

Check if String Contains Vowels in Python using if else

We will take a string while declaring the variables. Then, check if the string contains vowels using the for loop and if-else statement. Finally, the result will be displayed on the screen.

# Python program to check if string contains vowels

def checkVowels(string):  #use-defined function
   # check the string contains vowels
   for char in string:
      if char in 'aeiouAEIOU':
         return True
   return False

# take inputs
string = input('Enter any String: ')

# calling function
if (checkVowels(string) == True):
   print('Yes, String contains vowels.')
else:
   print('No, String does not contain vowels.')

Output for the input values test-case-1:-

Enter any String: Know Program
Yes, String contains vowels.

Output for the input values test-case-2:-

Enter any String: hmm
No, String does not contain vowels.

Python to Check if String Contains Vowels

This python program also performs the same task but in a different way. In this method, we will check if a string contains vowels using list comprehension.

# Python program to check if string contains vowels

def checkVowels(string):  #use-defined function
   # check the string contains vowels
   vowels = [each for each in string if each in "aeiouAEIOU"]
   return vowels

# take inputs
string = input('Enter any String: ')

# calling function
if (checkVowels(string)):
   print('Yes, String contains vowels.')
else:
   print('No, String does not contain vowels.')

Output:-

Enter any String: Python
Yes, String contains vowels.

Check if String Contains Vowels in Python using While Loop

In the above program, we will check if string contains vowels using the For Loop but in this program, we are using the while loop.

# Python program to check if string contains vowels using while loop

def checkVowels(string):  #use-defined function
   count = 0
   num_vowels = 0
    
   # to count the vowels
   while count < len(string):
      if string[count] == "a" or string[count] == "e" 
          or string[count] == "i" or string[count] == "o" 
           or string[count] == "u" or string[count] == "A" 
            or string[count] == "E" or string[count] == "I" 
             or string[count] == "O" or string[count] == "U":
         num_vowels = num_vowels+1
      count = count+1
   return num_vowels

# take inputs
string = input('Enter any String: ')

# calling function
if (checkVowels(string) != 0):
   print('Yes, String contains vowels.')
else:
   print('No, String does not contain vowels.')

Output:-

Enter any String: bcdfgh
No, String does not contain vowels.

Leave a Comment

Your email address will not be published. Required fields are marked *