Saturday 22 August 2020

Counting vowels in a string

 Using count() method:

string_data=input("Enter a string:")
vowels={'a','e','i','o','u'}
# Using Count() Method
list1=[]
for ch in string_data:
	if ch in vowels:
		if ch not in list1:
			list1.append(ch)
			print("{} occurred {} times".format(ch,string_data.count(ch)))

Without using count() method:

string_data=input("Enter a string:")
vowels={'a','e','i','o','u'}
d={}
for ch in string_data:
	if ch in vowels:
		d[ch]=d.get(ch,0)+1

print(d)
for key,value in sorted(d.items()):
	print(key,"occurred",value,"times")

2 comments: