Check if String Contains Vowels in Python

Previously, we had to check if the string starts with a vowel. In this article, we will check if string contains vowels in python. The letters A, E, I, O, U, and a, e, i, o, u are vowels. Remember, all other letters are consonants.

Check if String Contains Vowels in Python using if-else

Taken string using input() while declaring variable name string. Then, check if the string contains vowels using the for loop and if-else statement. The string contains vowels or does not print using the print() function.

# 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('String: ')

# function call
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:-

String: Know Program
Yes, String contains vowels.

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

String: hmm
No, String does not contain vowels.

Python to Check if String Contains Vowels

This program same as the above program but in this program, we use different methods. 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('String: ')

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

Output:-

String: Python
Yes, String contains vowels.

Check if String Contains Vowels in Python using While Loop

In the above program, we will check if a 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('String: ')

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

Output:-

String: bcdfgh
No, String does not contain vowels.

Leave a Comment

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