Level 1: Beginner
def count_vowels_consonants(text):
vowels = "aeiouAEIOU"
vowels_count = 0
consonants_count = 0
for char in text:
if char in vowels:
vowels_count += 1
elif char.isalpha():
consonants_count += 1
return vowels_count, consonants_count
text = input(f"Enter text or sentence to find number of vowels and consonants: ")
vowels_count, consonants_count = count_vowels_consonants(text)
print(f"The number of vowels in the string is: {vowels_count}")
print(f"The number of consonants in the string is: {consonants_count}")
Hi Ardit = Please check the code in the below link and suggest me if any optimization required.
https://pastebin.com/S0YnWUdr
Thanks,
Dev
I like that you are using a function. However, it's better to have the text as an argument. Here is an improvement: https://pastebin.com/TnHtuLDL
Noted! Thank you very much, Ardit.
#TO COUNT vowels and consonants
vowels = ['a','e','i','o','u','A','E','I','O','U']
cons_count = 0
for letter in word:
if letter.isalpha():
if letter in vowels:
else:
cons_count += 1
print(f"Vowels: {vowels_count}")
print(f"Consonants: {cons_count}")
import string
text = "Edu3cation is a power123ful tool for improving our under2standing of the uni45verse. !"
counts = {'vowels': 0, 'consonants': 0, 'numbers': 0, 'punctuation': 0}
for x in text:
if x.isalpha():
if x in vowels:
counts['vowels'] += 1
counts['consonants'] += 1
elif x.isnumeric():
counts['numbers'] += 1
elif x in string.punctuation:
counts['punctuation'] += 1
total = sum(counts.values())
print(f'Total characters: {total}')
print(f"Total count of vowel characters: {counts['vowels']}")
print(f"Total count of consonant characters: {counts['consonants']}")
print(f"Total count of numbers: {counts['numbers']}")
print(f"Total count of punctuation: {counts['punctuation']}")
vowels = ['a', 'e', 'i', 'o', 'u']
text = "How many vowels and consonants are in this sentence?".replace(' ', '')
for letter in text:
vowels_count+=1
elif letter != '?':
consonants_count+= 1
print(f"vowels count: {vowels_count}\nconsonants count: {consonants_count}")
vowels = ['a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U']
consonants = ['b', 'c', 'd', 'f', 'g', 'h', 'j', 'k', 'l', 'm', 'n', 'p', 'q', 'r', 's', 't', 'v', 'x', 'y', 'w', 'z', 'B', 'C', 'D', 'F', 'G', 'H', 'J', 'K', 'L', 'M', 'N', 'P', 'Q', 'R', 'S', 'T', 'V', 'W', 'X', 'Y', 'Z']
text = "How many vowels and consonants are in this sentence?"
v = 0
c = 0
for item in vowels:
amount = text.count(item)
v += amount
for item in consonants:
c += amount
print(f'The number of vowels in your text is: {v}')
print(f'The number of consonants in your text is: {c}')
using generator expression:
VOWELS = "aeiouAEIOU"
def count_vowels(text):
"""Counts the number of vowels in the given text."""
return sum(1 for char in text if char in VOWELS)
"""Counts the number of vowels and consonants in the given text."""
vowels_count = sum(1 for char in text if char in VOWELS)
consonants_count = sum(1 for char in text if char.isalpha() and char not in VOWELS)
if __name__ == "__main__":
string = input("Enter a text of sentence to find the number of vowels and consonants: ")
vowels = count_vowels(string)
vowels_count, consonants_count = count_vowels_consonants(string)
print(f"The number of vowels in the text or sentence is {vowels_count}")
print(f"The number of consonants in the text or sentence is {consonants_count}")
def count_vowels_consonants(text):
vowels = "aeiouAEIOU"
vowels_count = 0
consonants_count = 0
for char in text:
if char in vowels:
vowels_count += 1
elif char.isalpha():
consonants_count += 1
return vowels_count, consonants_count
text = input(f"Enter text or sentence to find number of vowels and consonants: ")
vowels_count, consonants_count = count_vowels_consonants(text)
print(f"The number of vowels in the string is: {vowels_count}")
print(f"The number of consonants in the string is: {consonants_count}")
Hi Ardit = Please check the code in the below link and suggest me if any optimization required.
https://pastebin.com/S0YnWUdr
Thanks,
Dev
I like that you are using a function. However, it's better to have the text as an argument. Here is an improvement: https://pastebin.com/TnHtuLDL
Noted! Thank you very much, Ardit.
#TO COUNT vowels and consonants
vowels = ['a','e','i','o','u','A','E','I','O','U']
vowels_count = 0
cons_count = 0
for letter in word:
if letter.isalpha():
if letter in vowels:
vowels_count += 1
else:
cons_count += 1
print(f"Vowels: {vowels_count}")
print(f"Consonants: {cons_count}")
import string
text = "Edu3cation is a power123ful tool for improving our under2standing of the uni45verse. !"
vowels = "aeiouAEIOU"
counts = {'vowels': 0, 'consonants': 0, 'numbers': 0, 'punctuation': 0}
for x in text:
if x.isalpha():
if x in vowels:
counts['vowels'] += 1
else:
counts['consonants'] += 1
elif x.isnumeric():
counts['numbers'] += 1
elif x in string.punctuation:
counts['punctuation'] += 1
total = sum(counts.values())
print(f'Total characters: {total}')
print(f"Total count of vowel characters: {counts['vowels']}")
print(f"Total count of consonant characters: {counts['consonants']}")
print(f"Total count of numbers: {counts['numbers']}")
print(f"Total count of punctuation: {counts['punctuation']}")
vowels = ['a', 'e', 'i', 'o', 'u']
text = "How many vowels and consonants are in this sentence?".replace(' ', '')
vowels_count = 0
consonants_count = 0
for letter in text:
if letter in vowels:
vowels_count+=1
elif letter != '?':
consonants_count+= 1
print(f"vowels count: {vowels_count}\nconsonants count: {consonants_count}")
vowels = ['a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U']
consonants = ['b', 'c', 'd', 'f', 'g', 'h', 'j', 'k', 'l', 'm', 'n', 'p', 'q', 'r', 's', 't', 'v', 'x', 'y', 'w', 'z', 'B', 'C', 'D', 'F', 'G', 'H', 'J', 'K', 'L', 'M', 'N', 'P', 'Q', 'R', 'S', 'T', 'V', 'W', 'X', 'Y', 'Z']
text = "How many vowels and consonants are in this sentence?"
v = 0
c = 0
for item in vowels:
amount = text.count(item)
v += amount
for item in consonants:
amount = text.count(item)
c += amount
print(f'The number of vowels in your text is: {v}')
print(f'The number of consonants in your text is: {c}')
using generator expression:
VOWELS = "aeiouAEIOU"
def count_vowels(text):
"""Counts the number of vowels in the given text."""
return sum(1 for char in text if char in VOWELS)
def count_vowels_consonants(text):
"""Counts the number of vowels and consonants in the given text."""
vowels_count = sum(1 for char in text if char in VOWELS)
consonants_count = sum(1 for char in text if char.isalpha() and char not in VOWELS)
return vowels_count, consonants_count
if __name__ == "__main__":
string = input("Enter a text of sentence to find the number of vowels and consonants: ")
vowels = count_vowels(string)
vowels_count, consonants_count = count_vowels_consonants(string)
print(f"The number of vowels in the text or sentence is {vowels_count}")
print(f"The number of consonants in the text or sentence is {consonants_count}")