In this tutorial, I will explain how to count words in a string using Python. As a data scientist at a US-based company, I recently encountered a situation where I needed to analyze user feedback and count the occurrence of specific words. Python provides several ways to accomplish this task efficiently. Let’s get into the details with relevant examples.
Count Words in a String Using Python
Python provides several ways to count words in a string using Python. Let us see some important methods.
Read Find the First Number in a String in Python
1. Use split() Method
The split() method divides the string into words based on whitespace and returns a list in Python. You can then use len() to count the number of words.
text = "New York is a popular city in the USA."
word_count = len(text.split())
print(word_count) Output:
9I have executed the above example code and added the screenshot below.

The split() method is simple and effective for counting words when the string is separated by whitespace.
Read How to Compare Strings in Python?
2. Use re.findall() Method
re.findall() in Python can be used to find all words by searching for sequences of alphanumeric characters. It returns a list of all words, which can be counted.
import re
text = "San Francisco is known for its beautiful architecture."
words = re.findall(r'\w+', text)
word_count = len(words)
print(word_count)Output:
8I have executed the above example code and added the screenshot below.

Regular expressions provide more flexibility, especially if you want to account for more complex word patterns or handle punctuation.
Read How to Create a String of N Characters in Python?
3. Use a for Loop
You can iterate over the string’s split result and count the words manually in a loop in Python.
text = "Los Angeles is famous for its entertainment industry."
word_count = 0
for word in text.split():
word_count += 1
print(word_count) Output:
8I have executed the above example code and added the screenshot below.

A for loop is a simple approach and is useful when you want to process each word individually during counting.
Read How to Split a String into Equal Parts in Python?
4. Use collections.Counter
You can use Counter from the collections module to count occurrences of each word in a list after splitting the string in Python.
from collections import Counter
text = "Chicago is a vibrant city with a diverse culture."
words = text.split()
word_count = len(Counter(words))
print(word_count) Output:
8str.count() is useful when you need to count specific words but don’t count all words in the string.
Read How to Insert a Python Variable into a String?
5. Use str.split() with List Comprehension
List comprehension in Python can be used to filter out non-alphabetic words and count the remaining valid words.
text = "Phoenix, Arizona is a city in the southwestern USA."
word_count = len([word for word in text.split() if word.isalpha()])
print(word_count) Output:
7List comprehension offers a compact and flexible way to count words while filtering unwanted characters, such as punctuation.
Read How to Split a String by Index in Python?
Conclusion
In this tutorial, I have explained how to count words in a string using Python. I covered some important methods like using the split() method, refindall() method, for loop, and collections.Counter, str.split() with list comprehension.
You may also like to read:
- Convert Binary to Decimal in Python
- How to Split a String Every N Characters in Python?
- Python Split Regex

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.