Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Count words in a sentence in Python program
In this article, we will learn different approaches to count the number of words in a string in Python. This is a common text processing task that can be accomplished using several methods.
Problem statement ? We are given a string and need to count the number of words in it.
Using split() Method
The split() method breaks the string into a list using space as the default delimiter. This is the simplest and most commonly used approach ?
Example
test_string = "Tutorials point is a learning platform"
# Display original string
print("The original string is:", test_string)
# Count words using split() method
word_count = len(test_string.split())
# Display result
print("The number of words in string are:", word_count)
Output
The original string is: Tutorials point is a learning platform The number of words in string are: 6
Using Regular Expressions
The re.findall() function with pattern \w+ matches sequences of word characters. This method handles punctuation better than simple splitting ?
Example
import re
test_string = "Tutorials point is a learning platform"
# Display original string
print("The original string is:", test_string)
# Count words using regex findall()
word_count = len(re.findall(r'\w+', test_string))
# Display result
print("The number of words in string are:", word_count)
Output
The original string is: Tutorials point is a learning platform The number of words in string are: 6
Using sum() with List Comprehension
This approach uses sum() with a list comprehension to count valid words while filtering out punctuation ?
Example
import string
test_string = "Tutorials point is a learning platform"
# Display original string
print("The original string is:", test_string)
# Count words using sum() + strip() + isalpha()
word_count = sum([word.strip(string.punctuation).isalpha()
for word in test_string.split()])
# Display result
print("The number of words in string are:", word_count)
Output
The original string is: Tutorials point is a learning platform The number of words in string are: 6
Comparison of Methods
| Method | Handles Punctuation | Performance | Best For |
|---|---|---|---|
split() |
No | Fastest | Simple text without punctuation |
re.findall() |
Yes | Medium | Text with punctuation |
sum() + comprehension |
Yes | Slowest | Complex filtering requirements |
Conclusion
Use split() for simple word counting in clean text. For text with punctuation, re.findall() provides better accuracy. The list comprehension approach offers the most control for complex filtering requirements.
