Check if String Starts with Vowel in Python

Previously we have to check whether a character is a vowel or consonant. Now in this post, we will check if the string starts with a vowel in python. In the English language, the letters A, E, I, O, U and a, e, i, o, u defined as a vowel. Except these all other letters are consonants. We will also develop a program to extract the words that start with a vowel from a list in python.

Python to Check if Word Starts with Vowel

This python program using the if-else statement to check if a word starts with a vowel or consonant. We will take a string while declaring the variables. Then, check if the string starts with a vowel in python using the if-else statement. Finally, the result will be displayed on the screen.

# Python program to check if string starts with vowel

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

# vowel alphabet
vowel = 'aeiou'

# check string starts with vowel or consonant
if ch[0].lower() in vowel:
    print(ch,'starts with vowel',ch[0])
else:
    print(ch,'starts with consonant',ch[0])

Output for the different input values:-

Enter any String: Python
Python starts with consonant P

Enter any String: Everyone
Everyone starts with vowel E

Enter any String: string
string starts with consonant s

Enter any String: open
open starts with vowel o

The lower() method returns the lowercased string from the given string. It converts all uppercase characters to lowercase. If no uppercase characters exist, it returns the original string.

We can also write a program in a simple way to check if a string starts with a vowel or consonant.

ch = input('Enter any String: ')

if ch[0].lower() in ['aeiou']:
    print(ch,'starts with vowel',ch[0])
else:
    print(ch,'starts with consonant',ch[0])

Program to accept Strings starting with a Vowel

These are the different methods to check whether a string starts with a vowel or consonant. In this program, we can also take the help of a user-defined function. A function is a block of code that performs a specific task.

# Python program to check if string starts with vowel
  
# Function to check if first character is vowel
def Vowel(string):
  
    if (string[0] == 'A' or string[0] == 'a'
        or string[0] == 'E' or string[0] == 'e'
        or string[0] == 'I' or string[0] == 'i'
        or string[0] == 'O' or string[0] == 'o'
        or string[0] == 'U' or string[0] == 'u'):
        return 1
    else:
        return 0
  
# Function to check 
def check(string):
    if (Vowel(string)):
        print('The string starts with a vowel')
    else:
        print('The string starts with a consonant')
  
# take input
character = input('Enter any String: ')

# calling function and display result
check(character)

Output:-

Enter any String: all
The string starts with a vowel

Python Program to Extract the Words that Start with a Vowel from a list

We will develop a program to extract the words that start with a vowel from a list in python using startswith() and for loop. In this, we check for each word and check if it starts with a vowel using startswith() on the first alphabet of every word. The iteration part is done using the loop. Finally, print all the words that start with a vowel.

# Python program to extract the words that start with a vowel from a list

# take list
words = ['String','Egg','know','Open','program','animal']

# vowel alphabet
vowel = 'A','E','I','O','U','a','e','i','o','u'

# check words and display result
print([w for w in words if w.startswith(vowel)])

Output:-

[‘Egg’, ‘Open’, ‘animal’]

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or you find anything incorrect? Let us know in the comments. Thank you!

Leave a Reply