As a Python developer, I was working on a project where I needed to count the number of words in a large text file.
I’ve often had to analyze text data: whether it’s customer feedback, product reviews, or log files. In this article, I’ll share how I personally perform a word count in Python. I’ll cover different methods, from simple built-in functions to more advanced approaches.
By the end of this tutorial, you’ll know exactly how to count words in a string or file using Python, and you’ll be able to choose the method that best fits your project.
Method 1 – Use Python’s split() Function
The simplest way to count words in Python is by using the built-in split() method. This method splits a string into a list of words, and then we can use the len() function to count them.
text = "Python is a powerful programming language used across the USA for data, AI, and web development."
# Split the string into words
words = text.split()
# Count the number of words
word_count = len(words)
print("Word Count:", word_count)I executed the above example code and added the screenshot below.

This approach works well for basic text where words are separated by spaces. It’s simple, clean, and perfect if you just need a quick word count in Python.
Method 2 – Use Regular Expressions in Python
Sometimes, text can contain punctuation like commas, periods, or special characters. In such cases, using split() may not give accurate results. This is where Python’s re (regular expressions) module comes in handy.
import re
text = "Python is popular in the U.S.A., especially for AI, Data Science, and Web Development!"
# Use regex to find words
words = re.findall(r'\b\w+\b', text)
# Count the words
word_count = len(words)
print("Word Count (Regex):", word_count)I executed the above example code and added the screenshot below.

Here, the regex \b\w+\b ensures that we only count valid words and ignore punctuation. I often use this method when I’m analyzing customer reviews or survey responses.
Method 3 – Use collections.Counter in Python
If you not only want to count the total words but also see how many times each word appears, the Counter class is perfect.
This is especially useful when analyzing large text files like product reviews or news articles.
from collections import Counter
text = "Python is great. Python is easy to learn. Python is used everywhere in the USA."
# Convert to lowercase and split
words = text.lower().split()
# Count word frequency
word_counts = Counter(words)
print("Word Frequency:", word_counts)
print("Total Word Count:", sum(word_counts.values()))I executed the above example code and added the screenshot below.

This method not only gives you the total word count but also shows the frequency of each word. It’s a great way to see which words appear most often in your dataset.
Method 4 – Count Words in a Text File Using Python
Many times, the text is stored in a file rather than a string. Python makes it easy to read a file and count words directly from it.
def count_words_in_file(filename):
with open(filename, 'r', encoding='utf-8') as file:
text = file.read()
words = text.split()
return len(words)
# Example: Assuming sample.txt contains text data
filename = "sample.txt"
print("Word Count in File:", count_words_in_file(filename))This method is extremely useful when working with log files, reports, or large documents. I’ve used it in real-world projects to analyze text-heavy datasets like customer feedback from surveys.
Method 5 – Count Specific Word Occurrences in Python
Sometimes, you may want to count how many times a specific word appears in your text. Python makes this easy with the count() method or Counter.
text = "Python is loved by developers in the USA. Python is also popular among data scientists."
# Convert to lowercase for case-insensitive search
word_to_count = "python"
count = text.lower().split().count(word_to_count)
print(f"The word '{word_to_count}' appears {count} times.")I executed the above example code and added the screenshot below.

This is useful when you want to track keywords like “Python,” “AI,” or “Data” in your dataset.
Method 6 – Advanced Word Count Using Python and nltk
For more advanced text analysis, you can use the Natural Language Toolkit (NLTK). This library allows you to tokenize words more accurately, especially when dealing with complex sentences.
import nltk
from nltk.tokenize import word_tokenize
# Download tokenizer (first time only)
nltk.download('punkt')
text = "Python is widely used in AI, Machine Learning, and Natural Language Processing in the USA."
# Tokenize words
words = word_tokenize(text)
# Count words
word_count = len(words)
print("Word Count (NLTK):", word_count)This method is more reliable for professional projects where accuracy is critical. I often use it when working with natural language processing tasks.
Real-Life Example – Word Count for Customer Reviews
Let’s put everything together with a practical example.
Imagine you have a file containing customer reviews from a U.S.-based e-commerce store. You want to analyze how many words are in the reviews and which words are most common.
from collections import Counter
import re
def analyze_reviews(filename):
with open(filename, 'r', encoding='utf-8') as file:
text = file.read().lower()
words = re.findall(r'\b\w+\b', text)
total_words = len(words)
word_counts = Counter(words)
return total_words, word_counts.most_common(10)
filename = "customer_reviews.txt"
total, top_words = analyze_reviews(filename)
print("Total Words in Reviews:", total)
print("Top 10 Most Common Words:", top_words)This approach gives you both the total count and the most frequent words in the reviews. It’s a powerful way to extract insights from raw text data.
Conclusion
While Python doesn’t have a single built-in function for word count, it gives us plenty of flexible methods.
You can use split() for quick counts, regex for cleaner results, Counter for word frequencies, and nltk for advanced text processing.
In my experience, choosing the right method depends on the complexity of your text and the level of detail you need.
You may also read:
- Use Python Type Hint Tuples for More Robust Code
- Sort a List of Tuples by the Second Element in Python
- Fix the IndexError: tuple index out of range Error in Python
- Create a Tuple from the List in Python

I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.