Python - Words Frequency in String Shorthands
Last Updated :
27 Oct, 2025
Given a string, the task is to find how many times each word appears in it. For example, the string "hello world hello everyone" should output each word with its frequency count.
Example:
Input: "hello world hello everyone"
Output: {'hello': 2, 'world': 1, 'everyone': 1}
Let’s explore different methods to find word frequency in Python.
Using collections.Counter
Counter() automatically counts how many times each word appears in a list when you split the string into words using .split().
Python
from collections import Counter
s = "hello world hello everyone"
res = Counter(s.split())
print(res)
OutputCounter({'hello': 2, 'world': 1, 'everyone': 1})
Explanation:
- s.split() splits the string into a list of words.
- Counter() counts how many times each unique word appears.
- The result shows 'hello' appears twice, 'world' and 'everyone' once each.
Using dict.get() with a for loop
Using dict.get() with a for loop allows you to efficiently count occurrences of each character or word in a string. The get() method retrieves the current count and if the character or word isn't in the dictionary yet, it returns a default value (usually 0).
Python
s = "hello world hello everyone"
res = {}
for word in s.split():
res[word] = res.get(word, 0) + 1
print(res)
Output{'hello': 2, 'world': 1, 'everyone': 1}
Explanation:
- for word in s.split() iterates through each word.
- w_freq.get(word, 0) fetches the word’s count (or 0 if it’s new).
- The count is incremented by 1 for every occurrence.
Using defaultdict(int) from collections
This is another efficient dictionary-based approach where every missing key is automatically initialized to 0.
Python
from collections import defaultdict
s = "hello world hello everyone"
res = defaultdict(int)
for word in s.split():
res[word] += 1
print(dict(res))
Output{'hello': 2, 'world': 1, 'everyone': 1}
Explanation:
- defaultdict(int) automatically starts each new key with 0.
- For each word, res[word] += 1 increments the count.
Using List Comprehension with collections.Counter
Using list comprehension with collections.Counter allows you to efficiently count the frequency of elements by transforming the data (such as characters or words in a string) and applying Counter to generate frequency counts.
Python
from collections import Counter
s = "hello world hello everyone"
res = Counter([word for word in s.split()])
print(res)
OutputCounter({'hello': 2, 'world': 1, 'everyone': 1})
Explanation:
- [word for word in s.split()] creates a list of all words.
- Counter() then counts each word’s occurrence.
Explore
Python Fundamentals
Python Data Structures
Advanced Python
Data Science with Python
Web Development with Python
Python Practice