Python Program to Accept the Strings Which Contains all Vowels
The problem is to determine if a string contains all the vowels: a, e, i, o, u. In this article, we’ll look at different ways to solve this.
Using all()
all()
function checks if all vowels are present in the string. It returns True
if every condition in the list comprehension is met.
s = "Geeksforgeeks"
v = 'aeiou'
# check if each vowel exists in the string
if all(i in s.lower() for i in v):
print("True")
else:
print("False")
Output
False
Let’s understand more methods to accept the strings which contains all vowels .
Table of Content
Using set()
set()
function is used to store unique elements and can efficiently check if all vowels are present in a string.
s= "geeksforgeeks"
# Define set of vowels
v= set('aeiou')
# Check if all vowels are present in the string (case-insensitive)
if v.issubset(set(s.lower())):
print("True")
else:
print("False")
Output
False
Using a Loop
Looping through the string, this method checks for the presence of each vowel. It stops early once all vowels are found, ensuring an efficient solution with linear time complexity.
s = "education"
v = 'aeiou'
a= set()
# Loop through each character in `s`
for char in s.lower():
if char in v:
# Add the vowel to the found set
a.add(char)
if len(a) == 5:
# If all vowels are found, exit early
print("True")
break
else:
print("False")
Output
True
Using regular expression
regular expression provide an efficient way to search for patterns in strings. This method uses regex to check if a string contains all five vowels (a, e, i, o, u), regardless of their order.
import re
s = "geeksforgeeks"
# Check if all vowels are present using regex
if re.search(r'(?=.*a)(?=.*e)(?=.*i)(?=.*o)(?=.*u)', s.lower()):
print("True")
else:
print("False")
Output
False