---
title: "Count word frequency in a sentence in python"  
description: "Count word frequency in a sentence in python"  
author: "Anubhav Sharma"  
published: 2025-04-24  
updated: 2025-04-24  
canonical: https://www.mindstick.com/interview/34062/count-word-frequency-in-a-sentence-in-python  
category: "python"  
tags: ["python-3.4", "python"]  
reading_time: 3 minutes  

---

# Count word frequency in a sentence in python

### Problem Statement

Given a sentence like:

```python
sentence = "hello world hello war is upcoming"
```

You need to return:

```python
{'hello': 2, 'world': 1, 'war': 1, 'is': 1, 'upcoming': 1}
```

### Approach

1. **Split** the sentence into words.
2. Use a **dictionary** to count the number of times each word occurs.
3. Optionally, convert all words to **lowercase** for case-insensitive calculations.

### Solution in Python

```python
def count_word_frequency(sentence):
    # Step 1: Convert to lowercase to avoid case mismatches
    sentence = sentence.lower()

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

    # Step 3: Create an empty dictionary to hold word counts
    word_count = {}

    # Step 4: Count each word
    for word in words:
        if word in word_count:
            word_count[word] += 1  # Increment count if word already exists
        else:
            word_count[word] = 1   # Initialize count if word appears for the first time

    return word_count

# Example usage:
sentence = "Hello world hello"
result = count_word_frequency(sentence)
print(result)
```

### Explanation

1. `sentence.lower()`: Ensures "Hello" and "hello" are treated as the same.
2. `sentence.split()`: Splits on whitespace by default.
3. The dictionary `word_count` stores each word as a key and its count as a value.

### Output:

```python
{'hello': 2, 'world': 1, 'war': 1, 'is': 1, 'upcoming': 1}
```

## Answers

### Answer by Anubhav Sharma

### Problem Statement

Given a sentence like:

```python
sentence = "hello world hello war is upcoming"
```

You need to return:

```python
{'hello': 2, 'world': 1, 'war': 1, 'is': 1, 'upcoming': 1}
```

### Approach

1. **Split** the sentence into words.
2. Use a **dictionary** to count the number of times each word occurs.
3. Optionally, convert all words to **lowercase** for case-insensitive calculations.

### Solution in Python

```python
def count_word_frequency(sentence):
    # Step 1: Convert to lowercase to avoid case mismatches
    sentence = sentence.lower()

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

    # Step 3: Create an empty dictionary to hold word counts
    word_count = {}

    # Step 4: Count each word
    for word in words:
        if word in word_count:
            word_count[word] += 1  # Increment count if word already exists
        else:
            word_count[word] = 1   # Initialize count if word appears for the first time

    return word_count

# Example usage:
sentence = "Hello world hello"
result = count_word_frequency(sentence)
print(result)
```

### Explanation

1. `sentence.lower()`: Ensures "Hello" and "hello" are treated as the same.
2. `sentence.split()`: Splits on whitespace by default.
3. The dictionary `word_count` stores each word as a key and its count as a value.

### Output:

```python
{'hello': 2, 'world': 1, 'war': 1, 'is': 1, 'upcoming': 1}
```


---

Original Source: https://www.mindstick.com/interview/34062/count-word-frequency-in-a-sentence-in-python

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
