Print Vowels in a String in Python

Previously we have to check a character is a vowel or consonant, check if a string starts with the vowel, and count vowels in a string. Now in this post, we will print vowels in a string in python using for loop and list comprehension. Also, develop a Python program to print vowels and consonants in a string. The alphabets ‘A’, ‘E’, ‘I’, ‘O’, ‘U’ (in uppercase) and ‘a’, ‘e’, ‘i’, ‘o’, ‘u’ are vowels, and remaining alphabets are called consonants.

This python program using the for loop to print vowels in a string. We will take a user-defined function to check and print if a string contains vowels. Then, we will take a string while declaring the variables. Finally, call the function and the result will be displayed on the screen.

# Python program to print vowels in a string

def printVowels(string):
    # to print the vowels
    for char in string:
        if char in "aeiouAEIOU":
            print(char, end=', ')
    return char

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

# calling function
printVowels(string)

Output for the different input values:-

Enter any string: Know Program
o, o, a,

Enter any string: vowel and consonant
o, e, a, o, o, a,

Enter any string: Python Program to print Vowels in a String
o, o, a, o, i, o, e, i, a, i,

Python Program to Print Vowels in a String

This python program also performs the same task but in a different way. This is the simplest and efficient way to find vowels in a string.

# Python program to print vowels in a string

def printVowels(string):
    # to print the vowels
    vowels = [each for each in string if each in "aeiouAEIOU"]
    print(vowels)

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

# calling function
printVowels(string)

Output:-

Enter any string: Python Java HTML CSS JAVASCRIPT
[‘o’, ‘a’, ‘a’, ‘A’, ‘A’, ‘I’]

Python Program to Print Vowels and Consonants in a String

In the above program, we will print only vowels present in a string but in this program, print vowels and consonants in a string using the if-else statement.

# Python program to print vowels and consonants in a string

def vowelConsonant(string):
    #check alphabet or not
    if not string.isalpha():
        return 'Neither'
    #check vowel or consonant
    if string.lower() in 'aeiou':
        return 'Vowel'
    else:
        return 'Consonant'

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

# calling function and display result
for ch in string:
    #print vowels and consonants
    print(ch,'is',vowelConsonant(ch),end=' : ')

Output:-

Enter any string: Python
P is Consonant : y is Consonant : t is Consonant : h is Consonant : o is Vowel : n is Consonant :

Enter any string: Java
J is Consonant : a is Vowel : v is Consonant : a is Vowel :

Enter any string: programming
p is Consonant : r is Consonant : o is Vowel : g is Consonant : r is Consonant : a is Vowel : m is Consonant : m is Consonant : i is Vowel : n is Consonant : g is Consonant :

In this program, the isalpha() method returns True if all characters in the string are alphabets. If not, it returns False. The lower() method converts all uppercase characters to lowercase characters.

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

Leave a Comment

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