Print Vowels in a String in Python
Checking vowels from the given string can be achieved in numerous ways in python. We will go through the most commonly used methods to demonstrate print vowels in a string in python with simple-to-read code examples.
1. Display vowels in a string using for loop
Here we will examine vowels in given text using a for a loop. Let’s go through the program to Display vowels in a word in python
text = input('Enter text: ') for char in text: if char.lower() in 'aeiou': print(char)
Output: Enter text: 'QuizCure' u i u e
- Prompting user to enter string using
input('Enter text: ')
- Loop through user input string using for loop
As users can enter text in uppercase, lowercase or mixed case, therefore, converted each sequence of the character to lowercase using
- Find an occurrence of lower character from the vowel string aeiou If the condition is true it prints the corresponding character. If false it continues to the next iteration.
In case we need to check if the string contains vowels using
if else in python, can follow this approach
text = input('Enter text: ') for char in text: if char.lower() in 'aeiou': print(char,'is vowel') else: print(char,'is Consonant')
Output: Enter text: 'hello' ('h', 'is Consonant') ('e', 'is vowel') ('l', 'is Consonant') ('l', 'is Consonant') ('o', 'is vowel')
2. print number of vowels in a word in python
We can get the total count of vowels in a string by adding a counter as described below.
text = input('Enter text: ') count = 0 for char in text: if char.lower() in 'aeiou': print(char) count = count + 1 print("No. of vowels:", count)
Output: Enter text: 'QuizCure' u i u e ('No. of vowels:', 4)
Explanation: From the previous program we have added a count param that is incremented by one once it satisfies the vowel condition.
3. Find vowels in a string using list comprehension
Using list comprehension to find vowels is one of the efficient ways chosen by programmers. list comprehension simplify multi liner code to simple-to-read single line code
text = input('Enter text: ') count = 0 vowel_list = [char for char in text if char.lower() in "aeiou"] print(len(vowel_list)) print(vowel_list)
Output: 4 ['u', 'i', 'u', 'e']
- Received user input string
input('Enter text: ')
- List comprehension is used here to Iterate each letter in the string. List comprehension is a replacement for loop and lengthy code blocks.
- Each letter is further converted to lowercase char.lower() and checked if the letter is any of the vowels aeiou.
List comprehension return list of vowels extracted from string based on criteria
char.lower() in "aeiou"
- len(vowels) is used to return vowels Count in a list name as vowel_list.
4. Find vowels in string python using regex
Regular expressions are very helpful if we need to search for string matching with any pattern.
Here is the simple to read program to check if a given string contains vowel letters using regular expression.
import re text = input('Enter text: ') regex = '[aeiou]' for char in text: if(re.search(regex, char.lower())): print(char)
Output: Enter text: 'QuizCURE' u i U E
- Imported re module to support regular expression functionality
- input('Enter text: ') Prompt user to enter string
[aeiou]to check for vowels
- Iterate over string received from user input
re.search()method used to find if passed letter
char.lower()matched with regular expression pattern or not.
- Print corresponding character if matched with vowel regex condition.
5. Write a program to find /print consonants in a string
From the previous explanation, we already covered how to detect vowels in a given string. We will rewrite the same previous examples by applying consonant criteria rather than vowel conditions.
Assuming you already have an idea what condition is required to apply to fetch consonants.
Print consonants in a string using for loop
text = input('Enter text: ') count = 0 for char in text: if char.lower() not in 'aeiou': print(char) count = count + 1 print("No. of consonants:", count)
Output: Enter text: 'QuizCure' Q z C r ('No. of consonants:', 4)
- Loop through the user input string
- Converted each iterative element to lower to avoid case problem
- Check if the letter doesn’t belong to vowels 'aeiou'
- Print letter met with the condition
- Print total number of consonants in a string
Print consonants in a string using list comprehension
text = input('Enter text: ') count = 0 consonants = [char for char in text if char.lower() not in "aeiou"] print(len(consonants)) print(consonants)
Output: Enter text: 'QuizCure' 4 ['Q', 'z', 'C', 'r']
list comprehension return here list of consonants met condition
char.lower() not in aeiou.
len(consonants) return length of consonants list.