Project Level 1: Beginner
This project is designed for beginner learners who are still learning and practicing Python fundamentals.
Project Description
This program defines a string, counts how many vowels and consonants are present, and displays both counts.
How the Project Works
Start your script by defining a sentence as a string. Here is an example:
text = "How many vowels and consonants are in this sentence?"
Here is how the program behaves when run:
Prerequisites
Required Libraries:
Required Files: No files are required.
IDE: You can use any IDE on your computer to code the project.
Danger Zone
Once you code the project, compare it with our solutions below.
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}")
#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}")