Skip to content
34 changes: 34 additions & 0 deletions strings/count_vowels_consonants.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
def count_vowels_and_consonants(text: str) -> tuple[int, int]:
Comment thread
bhatiakashvi16 marked this conversation as resolved.
"""
Count the number of vowels and consonants in a given string.

This function ignores non-alphabetic characters.

Args:
text (str): Input string.

Returns:
tuple[int, int]: A tuple containing (vowel_count,
consonant_count).
Examples:
>>> count_vowels_and_consonants("Hello World")
(3, 7)
>>> count_vowels_and_consonants("AEIOU")
(5, 0)
>>> count_vowels_and_consonants("bcdf")
(0, 4)
>>> count_vowels_and_consonants("")
(0, 0)
"""
vowels = set("aeiouAEIOU")
vowel_count = 0
consonant_count = 0

for char in text:
if char.isalpha():
if char in vowels:
vowel_count += 1
else:
consonant_count += 1

return vowel_count, consonant_count
Loading