+ 3
Is there Any other Simple Solution??
Write a program that takes in a string as input, counts and outputs the number of vowels (A, E, I, O, U). Input Format: A string (letters can be both uppercase or lower case). Output Format: A number which represents the total number of vowels in the string.
9 Answers
+ 7
HASAN IMTIAZ ,
your code is absolutely ok, just modify it as Lisa mentioned. (don't be too much `impressed` by the codes from RuntimeTerror and from Brian ) -> readability counts!
the code snippet below has a different approach compared to yours, and is iterating over the vowels string instead over the input text.
to get the number of each vowel we use the count() method of the string class.
user_input = input().lower()
vowels = "aeiou"
vowel_count = 0
for char in vowels:
vowel_count += user_input.count(char)
print(vowel_count)
+ 5
you can simplify your code by defining vowels as
vowel = "aeiou"
(it does not need to be a list of individual characters)
and you do not need to sort the user_input.
+ 4
HASAN IMTIAZ
for strings, the 'in' operator checks if the value is a substring.
'Alya' in 'Rayla' would return False
'ayla' in 'Rayla' would return True
in Brian's code,
char in 'aeiou' returns True everytime char is a vowel. True in Python is also treated as 1. The sum function adds up the 1's to give the vowel count
+ 3
Lothar Sure, I'd go with yo answer too but I found an opportunity to use << lol đ€Łđ€Ł
I'm not letting it slide
+ 2
user_input = input().lower()
vowel =["a","e","i","o","u"]
text = sorted(user_input)
vowel_count = 0
for i in text:
if i in vowel:
vowel_count += 1
print(vowel_count)
+ 2
#This was my simplest solution:
print(sum(char in 'aeiou' for char in input().lower()))
+ 2
From Brian, I added more condition for statement that aren't fully alphabet
print(sum((1 << (ord(char.lower()) - 0x61)) & 0b10000100000100000100001 > 0 for char in input() if char.isalpha()))
+ 1
So......How Does *in* Keyword works When we Check a String.
For instance,
if "Alya" in "Rayla":
How does The Code will Work..? Please Explain me
0
Brian ,Your Codes Are Working But I have No idea how