PYTHON PROGRAM THAT ASKS THE USER TO ENTER A WORD AND PRINTS OUT WHETHER THAT WORD CONTAINS ANY VOWELS
Write a program that asks the user to enter a word and prints out whether that word contains any vowels
CODE:
def vowels_str(string):
s=""
vowels="aeiouAEIOU"
for i in range(len(string)):
if string[i] in vowels:
if string[i] not in s:
s=s+string[i]
if s!="":
print("The string contains vowels")
else:
print("The string doesn't contain vowels")
return s
str=input("Enter a string:")
print("Vowels in a given string :",vowels_str(str))
EXPLANATION:
In this code, we write a function to check whether the string contains any vowels.
Here we create an empty string(s="") and put set of all the vowels in the variable vowels.
Next using for loop we check if the entered string contains any vowels and then add that string to the empty string and also prints an appropriate message.
If the vowel is repeated in the string then we don't add that again to the empty string .
Finally we return the string s which contains the vowels present in the main string.
Now we need to call the function by passing a string as parameter.
OUTPUT:
Not only for a word we can also check whether a sentence has any vowels, by giving a sentence as an input.
Comments
Post a Comment