Articles

Program to count the number of articles in a given paragraph in Python

Program to count the number of articles in a given paragraph in Python


In the python programming article, we are going to learn

  • program to count the number of articles in a given paragraph in python

Program to count the number of articles in a given paragraph in Python

The text file (paragraph.txt) is as follows:

 

Lorem Ipsum is simply dummied text of the printing and typesetting
industry. Lorem Ipsum has been the industry's standard dummy
text ever since the 1500s when an unknown printer took a galley
of type and scrambled it to make a type specimen book. It has
survived not only five centuries, but also the leap into electronic
typesetting, remaining essentially unchanged. It was popularised in
the 1960s with the release of Letraset sheets containing Lorem
Ipsum passages, and more recently with desktop publishing software
like Aldus PageMaker including versions of Lorem Ipsum.

The program is as follows:

 

# Owner : TutorialsInhand Author : Devjeet Roy

file = open("paragraph.txt","r")

article_count = 0

for i in file:
    i = i.lower()
    word_list = i.split(" ")
    for j in word_list:
        if j == "a" or j == "an" or j == "the":
            article_count += 1

print("The number of articles:",article_count)

The output is as follows:

 

PS C:\Users\DEVJEET\Desktop\tutorialsInHand> python code.py
The number of articles: 9

Few important tips about the program

1. We use the open() function to open() a file in Python. The "r" argument signifies that the file has been opened in the reading mode.

2. We convert the line which is a string to lowercase to avoid distinct conditions for articles in capital letters and small letters.

3. We break the string spacewise using the split() method.

4. We check for articles and increment the count.

 

count-number-of-articles-in-a-paragraph-in-python

 


Basic Python Programs

Would you like to see your article here on tutorialsinhand. Join Write4Us program by tutorialsinhand.com

About the Author
Devjeet Roy
Full Stack Web Developer & Data Science Enthusiast
Page Views :    Published Date : Oct 13,2022  
Please Share this page

Related Articles

Like every other website we use cookies. By using our site you acknowledge that you have read and understand our Cookie Policy, Privacy Policy, and our Terms of Service. Learn more Got it!