Signup/Sign In
PUBLISHED ON: JULY 2, 2021

Python program to accept the strings which contain all vowels

In this tutorial, you will learn to write a program that accepts a string and contains all vowels. Strings in Python are a sequence of characters wrapped inside single, double, or triple quotes. For a given string we have to check if every vowel is present in the string or not. Vowels in either uppercase ( 'A', 'E', 'I', 'O', 'U') or lowercase ('a', 'e', 'i', 'o', 'u') should be considered. For example,

Input: "Python is a Language"

Output: accepted

Input: "abcdifgho"

Output: not accepted

To solve this problem, we will create a set of vowels using set() method. Since both uppercase and lowercase vowels have to be considered, we will convert the string to lower case. Then we will iterate over the string and if any character is a vowel, we will delete it from the vowel set using remove().

In the end, we will check the length of the vowel set. If the length is 0 then the string will be accepted else the string will not be accepted.

The set() function will convert any of the iterable to the sequence of iterable elements with distinct elements, commonly called Set.

Algorithm

Follow the algorithm to understand the approach better.

Step 1- Define a function that will check for vowels in a string

Step 2- Convert the string to lower case

Step 3- Create a set of vowels

Step 4- Iterate over the characters in the string

Step 5- Check if the character is in the vowel set

Step 6- If the character is a vowel remove it from the set

Step 7- At last check if the vowel set is empty

Step 8- Print accepted if there is no vowel left in the vowel set

Step 9- Else print not accepted

Python Program

Look at the program to understand the implementation of the above-mentioned approach. To remove vowel from the set, we have used remove() which is a string class method.

# check if the string 
# contains all vowels 

#function
def CheckString(s):
    s = s.lower()
    vowels = set("aeiou")
    for char in s:
        if char in vowels:
            vowels.remove(char)
    if len(vowels) == 0:
        print("Accepted")
    else:
        print("Not accepted")


s1= "AeBIdeffoBUw"
print(s1)
CheckString(s1)

s2="python"
print(s2)
CheckString(s2)


AeBIdeffoBUw
Accepted
python
Not accepted

Conclusion

In this tutorial, we have discussed how to check if a string has all the vowels present. We have also seen how to use some built-in functions like set(), lower() and remove() in our program.



About the author:
Nikita Pandey is a talented author and expert in programming languages such as C, C++, and Java. Her writing is informative, engaging, and offers practical insights and tips for programmers at all levels.