Robotics & Artificial Intelligence
Write a function which counts and displays the number of vowels, consonants, uppercase, and lowercase characters in a string entered as input.
Answer
def count_characters(s):
vowels = 0
consonants = 0
uppercase = 0
lowercase = 0
for ch in s:
if 'A' <= ch <= 'Z':
uppercase += 1
if ch in 'AEIOU':
vowels += 1
else:
consonants += 1
elif 'a' <= ch <= 'z':
lowercase += 1
if ch in 'aeiou':
vowels += 1
else:
consonants += 1
print("Vowels:", vowels)
print("Consonants:", consonants)
print("Uppercase letters:", uppercase)
print("Lowercase letters:", lowercase)
text = input("Enter a string: ")
count_characters(text)Output
Enter a string: HelloWorld
Vowels: 3
Consonants: 7
Uppercase letters: 2
Lowercase letters: 8
Related Questions
Write a function which returns if a number passed as as argument is a perfect number or an Armstrong number.
Write a function which returns if a number passed as an argument is a prime or composite number.
Write a function which deletes all the negative values from a list passed as an argument.
Write a function which takes a number and a list as arguments and counts the occurrence of that number in the given list.