Saturday, 18 July 2026

πŸš€ Day 88/150 – Count Words in a File in Python

 

πŸš€ Day 88/150 – Count Words in a File in Python

Counting the number of words in a file is a common task in Python. It's useful for text analysis, document processing, data cleaning, and many real-world applications. Python provides several simple ways to count words efficiently.

In this post, we'll explore four different methods to count the words in a file.


Method 1 – Using read() and split()


Read the entire file and split the text into words.

with open("sample.txt", "r") as file: content = file.read() words = content.split() print("Total words:", len(words))






Output
Total words: 8

Explanation:

  • read() reads the complete file.
  • split() separates the text into words using whitespace.
  • len() returns the total number of words.

Method 2 – Using a for Loop

Read the file line by line and count the words in each line.

count = 0 with open("sample.txt", "r") as file: for line in file: count += len(line.split()) print("Total words:", count)








Output
Total words: 8

Explanation:

  • Read one line at a time.
  • Split each line into words.
  • Add the number of words to the counter.

Method 3 – Using sum()

A concise and efficient approach.

with open("sample.txt", "r") as file: count = sum(len(line.split()) for line in file) print("Total words:", count)





Output
Total words: 8

Explanation:
  • line.split() returns the words in each line.
  • len() counts the words.
  • sum() adds the counts from all lines.

Method 4 – Taking File Name from User

Allow the user to specify the file to analyze.

filename = input("Enter file name: ") with open(filename, "r") as file: content = file.read() print("Total words:", len(content.split()))






Sample Input
sample.txt

Output
Total words: 8

Explanation:

  • Accepts a file name from the user.
  • Reads the file.
  • Counts the total number of words.

Comparison of Methods

MethodBest For
read() + split()Small text files
for LoopUnderstanding the counting logic
sum()Clean and memory-efficient
User InputInteractive applications

πŸ”₯ Key Takeaways

  • split() is the easiest way to separate text into words.
  • read() works well for small files.
  • Reading the file line by line is more memory-efficient for larger files.
  • sum() with a generator expression provides a concise solution.
  • Word counting is widely used in text processing, document analysis, and NLP applications.

0 Comments:

Post a Comment

Popular Posts

Categories

100 Python Programs for Beginner (119) AI (311) Android (25) AngularJS (1) Api (7) Assembly Language (2) aws (31) Azure (12) BI (10) Books (282) Bootcamp (12) C (78) C# (12) C++ (83) cloud (1) Course (87) Coursera (300) Cybersecurity (32) data (9) Data Analysis (40) Data Analytics (27) data management (16) Data Science (395) Data Strucures (23) Deep Learning (200) Django (16) Downloads (3) edx (21) Engineering (15) Euron (30) Events (7) Excel (23) Finance (11) flask (4) flutter (1) FPL (17) Generative AI (76) Git (12) Google (53) Hadoop (3) HTML Quiz (1) HTML&CSS (48) IBM (43) IoT (3) IS (25) Java (99) Leet Code (4) Machine Learning (352) Meta (24) MICHIGAN (5) microsoft (13) Nvidia (8) Pandas (15) PHP (20) Projects (34) Python (1404) Python Coding Challenge (1191) Python Mathematics (5) Python Mistakes (51) Python Quiz (573) Python Tips (26) Questions (3) R (72) React (7) Scripting (3) security (4) Selenium Webdriver (4) Software (20) SQL (52) Udemy (18) UX Research (1) web application (11) Web development (9) web scraping (3)

Followers

Python Coding for Kids ( Free Demo for Everyone)