Python Words Frequency in String Shorthands (original) (raw)

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)

`

Output

Counter({'hello': 2, 'world': 1, 'everyone': 1})

**Explanation:

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:

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:

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)

`

Output

Counter({'hello': 2, 'world': 1, 'everyone': 1})

**Explanation: