Python program to count words in a sentence Last Updated : 07 Jan, 2025 Comments Improve Suggest changes Like Article Like Report In this article, we will explore different methods for counting words in a sentence. The split() method is one of the simplest and most efficient ways to count words in a sentence. Python s = "Python is fun and versatile." # Counting words word_count = len(s.split()) print(word_count) Output5 Explanation:The split() method divides the sentence into a list of words based on whitespace.The len() function is used to calculate the total number of words in the list.Let's explore some more methods and see the different python programs to count words in a sentence.Table of ContentUsing Regular ExpressionsUsing collections.CounterUsing a loopUsing Regular ExpressionsRegular expressions are particularly useful when the sentence contains special characters or multiple spaces. Python s = "Python: easy, powerful, and flexible!" # Counting words using regex import re words = re.findall(r'\b\w+\b', s) word_count = len(words) print(word_count) OutputWord Count: 5 Explanation:The pattern \b\w+\b identifies whole words, considering word boundaries and alphanumeric characters.The findall() function from the re module extracts all matching words into a list.The len() function calculates the total number of words in the list.Using collections.CounterFor additional analysis, such as word frequencies, the Counter class can be used. Python s = "Python is simple, yet powerful and simple." # Counting words with Counter from collections import Counter word_list = s.split() word_count = len(word_list) word_frequencies = Counter(word_list) print(word_count) print(word_frequencies) Output7 Counter({'Python': 1, 'is': 1, 'simple,': 1, 'yet': 1, 'powerful': 1, 'and': 1, 'simple.': 1}) Explanation:The split() method generates a list of words.The len() function calculates the total number of words.Counter provides a frequency count for each word in the list.Using a loopA manual approach using a loop provides more control over the word counting process. Python s = "Learning Python step by step." # Counting words manually word_count = 0 for word in s.split(): word_count += 1 print("Word Count:", word_count) OutputWord Count: 5 Explanation:The split() method creates a list of words from the sentence.A loop iterates through each word in the list, incrementing the count for every word. Comment More infoAdvertise with us Next Article Python program to count words in a sentence M manjeet_04 Follow Improve Article Tags : Python Python Programs Python string-programs Practice Tags : python Similar Reads Python program to find the longest word in a sentence In this article, we will explore various methods to find the longest word in a sentence.Using LoopFirst split the sentence into words using split() and then uses a loop (for loop) to iterate through the words and keeps track of the longest word by comparing their lengths.Pythons = "I am learning Pyt 1 min read Python Program to Count the Number of Vowels in a String In this article, we will be focusing on how to print each word of a sentence along with the number of vowels in each word using Python. Vowels in the English language are: 'a', 'e', 'i', 'o', 'u'. So our task is to calculate how many vowels are present in each word of a sentence. So let us first des 10 min read Split a sentence into list of words in Python When working with text in Python, we often need to break down a sentence into individual words. This task is easy to accomplish in Python using different methods. The simplest way is by using split(), but more complex tasks can be handled with regular expressions or list comprehension. Depending on 2 min read How to Count Repeated Words in a String in Python In this article, we will learn how to count repeated words in a string. Python provides several methods to Count Repeated Words , such as dictionaries, collections. Counter module, or even regular expressions. The simplest way to count repeated words is by splitting the string into individual words 2 min read Python - Sequence Assignment to Words Given a String of words, assign an index to each word. Input : test_str = 'geeksforgeeks is best' Output : {0: 'geeksforgeeks', 1: 'is', 2: 'best'} Explanation : Index assigned to each word. Input : test_str = 'geeksforgeeks best' Output : {0: 'geeksforgeeks', 1: 'best'} Explanation : Index assigned 5 min read Like