Skip to content

Instantly share code, notes, and snippets.

@sixtusagbo
Last active November 11, 2022 01:43
Show Gist options
  • Save sixtusagbo/286963f220eaeafa553f87c48b27d893 to your computer and use it in GitHub Desktop.
Save sixtusagbo/286963f220eaeafa553f87c48b27d893 to your computer and use it in GitHub Desktop.
Count how many articles in a string in python
#!/usr/bin/python3
"""
Module that counts number of articles in a string
"""
def filter_articles(text):
"""Count how many articles (a, an, the) in a string
Args:
text: (str) string to search
Return: number of articles in `text`
"""
sum_a = text.lower().split().count('a')
sum_an = text.lower().split().count('an')
sum_the = text.lower().split().count('the')
return sum_a + sum_an + sum_the
if __name__ == "__main__":
text = input("Enter some text:\n")
print()
count = filter_articles(text)
print("There are {} articles in the text.".format(count if count > 0 else "no"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment