Skip to content

Commit

Permalink
Create count_vowels.py (#11474)
Browse files Browse the repository at this point in the history
* Create count_vowels.py

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
its-100rabh and pre-commit-ci[bot] committed Jul 4, 2024
1 parent 716bdeb commit c1dc8e9
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions strings/count_vowels.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
def count_vowels(s: str) -> int:
"""
Count the number of vowels in a given string.
:param s: Input string to count vowels in.
:return: Number of vowels in the input string.
Examples:
>>> count_vowels("hello world")
3
>>> count_vowels("HELLO WORLD")
3
>>> count_vowels("123 hello world")
3
>>> count_vowels("")
0
>>> count_vowels("a quick brown fox")
5
>>> count_vowels("the quick BROWN fox")
5
>>> count_vowels("PYTHON")
1
"""
if not isinstance(s, str):
raise ValueError("Input must be a string")

vowels = "aeiouAEIOU"
return sum(1 for char in s if char in vowels)


if __name__ == "__main__":
from doctest import testmod

testmod()

0 comments on commit c1dc8e9

Please sign in to comment.