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.

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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))

chevron_right


Output:

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.

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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))

chevron_right


Output:

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.

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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))

chevron_right


Output:

The original string is : Geeksforgeeks, is best @# Computer Science Portal.!!!
The number of words in string are : 6



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.