Python program to count words in a sentence
Data preprocessing is an important task in text classification. With emergence of Python in the field of data science, it is essential to have certain shorthands to have upper hand among others. This article discusses ways to count words in a sentence, it starts with space separated words but also includes ways to in presence of special characters as well. Let’s discuss certain ways to perform this.
Method #1 : Using split()
split function is quite useful and usually quite generic method to get words out of the list, but this approach fails once we introduce special characters in the list.
# Python3 code to demonstrate # to count words in string # using split() # initializing string test_string = "Geeksforgeeks is best Computer Science Portal" # printing original string print ("The original string is : " + test_string) # using split() # to count words in string res = len(test_string.split()) # printing result print ("The number of words in string are : " + str(res)) |
The original string is : Geeksforgeeks is best Computer Science Portal
The number of words in string are : 6
Method #2 : Using regex(findall())
Regular expressions have to be used in case we require to handle the cases of punctuation marks or special characters in the string. This is the most elegant way in which this task can be performed.
# Python3 code to demonstrate # to count words in string # using regex (findall()) import re # initializing string test_string = "Geeksforgeeks, is best @# Computer Science Portal.!!!" # printing original string print ("The original string is : " + test_string) # using regex (findall()) # to count words in string res = len(re.findall(r'\w+', test_string)) # printing result print ("The number of words in string are : " + str(res)) |
The original string is : Geeksforgeeks, is best @# Computer Science Portal.!!!
The number of words in string are : 6
Method #3 : Using sum() + strip() + split()
This method performs this particular task without using regex. In this method we first check all the words consisting of all the alphabets, if so they are added to sum and then returned.
# Python3 code to demonstrate # to count words in string # using sum() + strip() + split() import string # initializing string test_string = "Geeksforgeeks, is best @# Computer Science Portal.!!!" # printing original string print ("The original string is : " + test_string) # using sum() + strip() + split() # to count words in string res = sum([i.strip(string.punctuation).isalpha() for i in test_string.split()]) # printing result print ("The number of words in string are : " + str(res)) |
The original string is : Geeksforgeeks, is best @# Computer Science Portal.!!!
The number of words in string are : 6
Recommended Posts:
- Python | Check if given words appear together in a list of sentence
- Python | Remove all duplicates words from a given sentence
- Python | Spilt a sentence into list of words
- Python | Sort words of sentence in ascending order
- NLP | How tokenizing text, sentence, words works
- Python program to print even length words in a string
- Python | Program to crawl a web page and get most frequent words
- Python program to find uncommon words from two Strings
- Program to replace a word with asterisks in a sentence
- Python | Reverse each word in a sentence
- Count words in a given string
- Python program to count Even and Odd numbers in a List
- Python Program to Count set bits in an integer
- Python Program for Check if count of divisors is even or odd
- Python Program to Count ways to reach the n'th stair
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



