Python program to accept the strings which contains all vowels


Sometimes you want to accept input based on certain conditions. Here, we are going to see the same type of program. We will write a program that allows only words with vowels. We will show them whether the input is valid or not.

Let's see the approach step by step.

  • Define a list of vowels [A, E, I, O, U, a, e, i, o, u]

  • Initialize a word or sentence.

  • Iterate over the word or sentence.

    • Check if it is present in the list or not.      

3.1.1. If not, break the loop and print Not accepted.
  • Else print accepted

Example

Let's convert the text into Python code.

def check_vowels(string):
   # vowels
   vowels = ['A', 'E', 'I', 'O', 'U', 'a', 'e', 'i', 'o', 'u']
   # iterating over the string
   for char in string:
      if char not in vowels:
      print(f"{string}: Not accepted")
      break
   else:
      print(f"{string}: Accepted")
if __name__ == '__main__':
   # initializing strings
   string_1 = "tutorialspoint"
   string_2 = "AEiouaieeu"
   # checking the strings
   check_vowels(string_1)
   check_vowels(string_2)

Output

If you run the above code, you will get the following results.

tutorialspoint: Not accepted
AEiouaieeu: Accepted

Conclusion

You check different properties based on your requirements. If you have any doubts in the tutorial, mention them in the comment section.

Updated on: 12-Feb-2020

958 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements