How to Count Repeated Words in a String in Python
Last Updated :
20 Nov, 2024
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 and using a dictionary to keep track of their occurrences. This method is straightforward and works well for basic word counting tasks.
Python
# Using a dictionary to count words
s = "Geeks for Geeks"
word = s.split()
count = {}
for i in word:
count[i] = count.get(i, 0) + 1
print(count)
Output{'Python': 2, 'is': 2, 'great': 1, 'and': 1, 'fun': 1}
Above example splits the string into words using split() and iterates through each word to count their occurrences using a dictionary.
Using collections.Counter
Counter class from collections module provides an efficient way to count words.
Python
from collections import Counter
s = "Geeks for Geeks"
word = s.split()
# Using Counter to count words
count = Counter(word)
print(count)
OutputCounter({'Python': 2, 'is': 2, 'great': 1, 'and': 1, 'fun': 1})
Counter class automatically creates a dictionary where keys are words and values are their counts.
Using Regular Expressions
For more complex text processing such as ignoring punctuation or case, regular expressions can be used. This method extracts words using a pattern and counts them.
Python
import re
from collections import Counter
# Using regular expressions to count words
s = "Python is great. Python is fun!"
# Extract words and convert to lowercase
word = re.findall(r'\b\w+\b', s.lower())
count = Counter(word)
print(count)
OutputCounter({'python': 2, 'is': 2, 'great': 1, 'fun': 1})
Explanation:
The re.findall() function extracts words from the string while ignoring punctuation. Converting to lowercase ensures that words like "Python" and "python" are treated as the same.
Similar Reads
Python program to count words in a sentence 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.Pythons = "Python is fun and versatile." # Counting words word_count = len(s.split()) print(word_count) Output5 Explanat
2 min read
Recursively Count Vowels From a String in Python Python is a versatile and powerful programming language that provides various methods to manipulate strings. Counting the number of vowels in a string is a common task in text processing. In this article, we will explore how to count vowels from a string in Python using a recursive method. Recursion
3 min read
Count Words in Text File in Python Our task is to create a Python program that reads a text file, counts the number of words in the file and prints the word count. This can be done by opening the file, reading its contents, splitting the text into words, and then counting the total number of words.Example 1: Count String WordsFirst,
3 min read
Python - Ways to Count Number of Substring in String Given a string s, determine the number of substrings that satisfy certain criteria. For example we are given a string s="hellohellohello" we need to count how many time the substring occur in the given string suppose we need to find the substring "hello" so that the count becomes 3. We can use metho
2 min read
Python - List Words Frequency in String Given a List of Words, Map frequency of each to occurrence in String. Input : test_str = 'geeksforgeeks is best for geeks and best for CS', count_list = ['best', 'geeksforgeeks', 'computer'] Output : [2, 1, 0] Explanation : best has 2 occ., geeksforgeeks 1 and computer is not present in string.Input
4 min read