---
title: "Count Word Frequency in a String with explanation"  
description: "Count Word Frequency in a String with explanation"  
author: "Ravi Vishwakarma"  
published: 2025-09-03  
updated: 2025-09-03  
canonical: https://www.mindstick.com/interview/34366/count-word-frequency-in-a-string-with-explanation  
category: "python"  
tags: ["python-3.4", "python", "Python 3"]  
reading_time: 4 minutes  

---

# Count Word Frequency in a String with explanation

### Problem:

You are given a string, and you want to know **how many times each word appears** in it.\
Example:

```plaintext
"This is a test. This test is simple."
```

Expected output (word frequency):

```plaintext
this → 2
is → 2
a → 1
test → 2
simple → 1
```

### Step-by-Step Solution

#### 1. Take the input string

```python
text = "This is a test. This test is simple."
```

#### 2. Normalize the string

- Convert everything to lowercase → to avoid treating `"This"` and `"this"` as different words.
- Remove punctuation (`.`, `,`, etc.).

```python
import re

# Lowercase and remove non-alphabetic characters
cleaned_text = re.sub(r'[^a-zA-Z\s]', '', text.lower())
```

## Now:

```plaintext
cleaned_text = "this is a test this test is simple"
```

#### 3. Split the string into words

```python
words = cleaned_text.split()
```

## Now:

```plaintext
words = ['this', 'is', 'a', 'test', 'this', 'test', 'is', 'simple']
```

#### 4. Count word frequency

We can use:

- **Dictionary (manual counting)**
- `collections.Counter` (built-in shortcut)

## Method 1: Using Dictionary

```python
word_freq = {}
for word in words:
    word_freq[word] = word_freq.get(word, 0) + 1
```

## Method 2: Using Counter

```python
from collections import Counter
word_freq = Counter(words)
```

#### 5. Display the result

```python
for word, freq in word_freq.items():
    print(f"{word} → {freq}")
```

####

#### Full Working Code

```python
import re
from collections import Counter

text = "This is a test. This test is simple."

# Step 1: Clean and normalize
cleaned_text = re.sub(r'[^a-zA-Z\s]', '', text.lower())

# Step 2: Split into words
words = cleaned_text.split()

# Step 3: Count frequency
word_freq = Counter(words)

# Step 4: Print results
for word, freq in word_freq.items():
    print(f"{word} → {freq}")
```

### Output

```plaintext
this → 2
is → 2
a → 1
test → 2
simple → 1
```

## Explanation Summary:

- Normalize the string (lowercase + remove punctuation).
- Split the string into words.
- Count how many times each word occurs.
- Print or return the result.

## Answers

### Answer by Ravi Vishwakarma

### Problem:

You are given a string, and you want to know **how many times each word appears** in it.\
Example:

```plaintext
"This is a test. This test is simple."
```

Expected output (word frequency):

```plaintext
this → 2
is → 2
a → 1
test → 2
simple → 1
```

### Step-by-Step Solution

#### 1. Take the input string

```python
text = "This is a test. This test is simple."
```

#### 2. Normalize the string

- Convert everything to lowercase → to avoid treating `"This"` and `"this"` as different words.
- Remove punctuation (`.`, `,`, etc.).

```python
import re

# Lowercase and remove non-alphabetic characters
cleaned_text = re.sub(r'[^a-zA-Z\s]', '', text.lower())
```

## Now:

```plaintext
cleaned_text = "this is a test this test is simple"
```

#### 3. Split the string into words

```python
words = cleaned_text.split()
```

## Now:

```plaintext
words = ['this', 'is', 'a', 'test', 'this', 'test', 'is', 'simple']
```

#### 4. Count word frequency

We can use:

- **Dictionary (manual counting)**
- `collections.Counter` (built-in shortcut)

## Method 1: Using Dictionary

```python
word_freq = {}
for word in words:
    word_freq[word] = word_freq.get(word, 0) + 1
```

## Method 2: Using Counter

```python
from collections import Counter
word_freq = Counter(words)
```

#### 5. Display the result

```python
for word, freq in word_freq.items():
    print(f"{word} → {freq}")
```

####

#### Full Working Code

```python
import re
from collections import Counter

text = "This is a test. This test is simple."

# Step 1: Clean and normalize
cleaned_text = re.sub(r'[^a-zA-Z\s]', '', text.lower())

# Step 2: Split into words
words = cleaned_text.split()

# Step 3: Count frequency
word_freq = Counter(words)

# Step 4: Print results
for word, freq in word_freq.items():
    print(f"{word} → {freq}")
```

### Output

```plaintext
this → 2
is → 2
a → 1
test → 2
simple → 1
```

## Explanation Summary:

- Normalize the string (lowercase + remove punctuation).
- Split the string into words.
- Count how many times each word occurs.
- Print or return the result.


---

Original Source: https://www.mindstick.com/interview/34366/count-word-frequency-in-a-string-with-explanation

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
