Skip to content

Commit 913efdc

Browse files
authored
Create count_vowels.py
1 parent 716bdeb commit 913efdc

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

strings/count_vowels.py

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
def count_vowels(s: str) -> int:
2+
"""
3+
Count the number of vowels in a given string.
4+
5+
:param s: Input string to count vowels in.
6+
:return: Number of vowels in the input string.
7+
8+
Examples:
9+
>>> count_vowels("hello world")
10+
3
11+
>>> count_vowels("HELLO WORLD")
12+
3
13+
>>> count_vowels("123 hello world")
14+
3
15+
>>> count_vowels("")
16+
0
17+
>>> count_vowels("a quick brown fox")
18+
5
19+
>>> count_vowels("the quick BROWN fox")
20+
5
21+
>>> count_vowels("PYTHON")
22+
1
23+
"""
24+
if not isinstance(s, str):
25+
raise ValueError("Input must be a string")
26+
27+
vowels = "aeiouAEIOU"
28+
return sum(1 for char in s if char in vowels)
29+
30+
31+
if __name__ == "__main__":
32+
from doctest import testmod
33+
testmod()

0 commit comments

Comments
 (0)