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.
Scroll for More Useful Information and Relevant FAQs
- Display vowels in a string using for loop
- print number of vowels in a word in python
- Find vowels in a string using list comprehension
- Find vowels in string python using regex
- Write a program to find /print consonants in a string
- Feedback: Your input is valuable to us. Please provide feedback on this article.
Display vowels in a string using for loop
Here we will examine vowels in the 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
Explanation:
- Prompting user to enter string using
input('Enter text: ')
- Loop through user input string using for loop
-
Users can enter text in uppercase, lowercase, or mixed case, therefore, convert each sequence of the character to lowercase using
char.lower()
- Find an occurrence of a 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')
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.
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']
Explanation:
- 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 returns a list of vowels extracted from a string based on criteria
char.lower() in "aeiou"
- len(vowels) is used to return vowels Count in a list name as vowel_list.
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
Program Explanation:
- Imported re module to support regular expression functionality
- input('Enter text: ') Prompt user to enter the string
-
Regex
[aeiou]
to check for vowels - Iterate over string received from user input
-
re.search()
method used to find if passed letterchar.lower()
matched with regular expression pattern or not. - Print the corresponding character if matched with the vowel regex condition.
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)
Explanation:
- Loop through the user input string
- Converted each iterative element to lower to avoid case problem
- Check if the letter doesn't belong to the vowels 'aeiou'
- Print letter met with the condition
- Print the 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']
Explanation:
list comprehension return here list of consonants met condition char.lower()
not in aeiou. len(consonants)
return length of consonants list.