Open In App

Find Frequency of Each Word in a String in Python

Last Updated : 10 Dec, 2025
Comments
Improve
Suggest changes
9 Likes
Like
Report

Given a string, the task is to count how many times each word appears. Examples:

Input: "apple mango apple orange"
Output: apple -> 2
mango -> 1
orange -> 1

Let's explore different ways to find frequency of each word in a string in Python.

Using collections.Counter()

This method splits the string into words and uses Counter to automatically count how many times each word occurs. It creates a mapping where each word becomes a key and its frequency becomes the value.

Python
from collections import Counter

s = "apple mango apple orange orange apple guava mango mango"
words = s.split()
freq = Counter(words)

for w, c in freq.items():
    print(w, ":", c)

Output
apple : 3
mango : 3
orange : 2
guava : 1

Explanation:

  • s.split() converts the string into a list of words
  • Counter(words) creates a dictionary-like object where each key is a word and value is the count
  • freq.items() gives (word, count) pairs to print

Using a Dictionary with .get()

This method builds the word-frequency dictionary manually. For each word in the list, it increases its count using .get() to handle new words cleanly.

Python
s = "apple mango apple orange orange apple guava mango mango"
words = s.split()
freq = {}

for w in words:
    freq[w] = freq.get(w, 0) + 1

for w, c in freq.items():
    print(w, ":", c)

Output
apple : 3
mango : 3
orange : 2
guava : 1

Explanation:

  • freq.get(w, 0) returns the existing count or 0 if the word is new
  • freq[w] = ... + 1 increases the count for each occurrence
  • This manually creates a word count mapping

Using operator.countOf()

This method first collects unique words and then counts each one using countOf(), which returns how many times a word appears in the list.

Python
import operator as op

s = "apple mango apple orange orange apple guava mango mango"
words = s.split()
uniq = []

for w in words:
    if w not in uniq:
        uniq.append(w)

for w in uniq:
    print(w, ":", op.countOf(words, w))

Output
apple : 3
mango : 3
orange : 2
guava : 1

Explanation:

  • uniq stores unique words to avoid repeating counts
  • op.countOf(words, w) counts how many times w is present in the list
  • Loop prints frequency of each unique word

Using Dictionary Comprehension

This method counts words by looping over unique words and creating a dictionary where each key is a word and each value is its count using .count().

Python
s = "apple mango apple orange orange apple guava mango mango"
words = s.split()
freq = {w: words.count(w) for w in set(words)}

for w, c in freq.items():
    print(w, ":", c)

Output
apple : 3
orange : 2
mango : 3
guava : 1

Explanation:

  • set(words) provides unique words
  • {w: words.count(w) ...} builds a dictionary where each key is a word, and value is the number of occurrences
  • words.count(w) counts occurrences in the full list

Article Tags :

Explore