Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Python Program to Count of Words with specific letter
In this article, we will learn how to count words containing a specific letter in a string using Python. We'll explore four different approaches to solve this problem efficiently.
Problem Statement
Given an input string and a character, we need to count how many words contain that specific character.
Example
input_string = 'hello tutorialspoint python codes' input_char = "p" # Expected output: 2 (words: tutorialspoint, python)
Method 1: Using List Comprehension with split()
List comprehension provides a concise way to filter words containing the target character ?
# Input string and character
input_string = 'hello tutorialspoint python codes'
input_char = "p"
print("Input String:", input_string)
print("Target Character:", input_char)
# Count words containing the character using list comprehension
words_count = len([word for word in input_string.split() if input_char in word])
print(f"Count of words containing '{input_char}': {words_count}")
Input String: hello tutorialspoint python codes Target Character: p Count of words containing 'p': 2
Method 2: Using split() and find() Functions
The find() method returns the index of the first occurrence or -1 if not found ?
input_string = 'hello tutorialspoint python codes'
input_char = "p"
print("Input String:", input_string)
words_count = 0
words_list = input_string.split()
# Check each word using find() method
for word in words_list:
if word.find(input_char) != -1:
words_count += 1
print(f"Count of words containing '{input_char}': {words_count}")
Input String: hello tutorialspoint python codes Count of words containing 'p': 2
Method 3: Using replace() and Length Comparison
This method removes the target character and compares string lengths ?
input_string = 'hello tutorialspoint python codes'
input_char = "p"
print("Input String:", input_string)
words_count = 0
words_list = input_string.split()
# Check by removing the character and comparing lengths
for word in words_list:
word_without_char = word.replace(input_char, "")
if len(word_without_char) < len(word):
words_count += 1
print(f"Count of words containing '{input_char}': {words_count}")
Input String: hello tutorialspoint python codes Count of words containing 'p': 2
Method 4: Using Counter() Function
The Counter class creates a frequency dictionary of characters in each word ?
from collections import Counter
input_string = 'hello tutorialspoint python codes'
input_char = "p"
print("Input String:", input_string)
words_count = 0
words_list = input_string.split()
# Use Counter to get character frequencies
for word in words_list:
char_frequency = Counter(word)
if input_char in char_frequency:
words_count += 1
print(f"Count of words containing '{input_char}': {words_count}")
Input String: hello tutorialspoint python codes Count of words containing 'p': 2
Comparison of Methods
| Method | Readability | Performance | Best For |
|---|---|---|---|
| List Comprehension | High | Fast | Simple one-liners |
| find() Method | Medium | Fast | When you need position info |
| replace() Method | Low | Slower | Educational purposes |
| Counter() | Medium | Slower | When frequency data is needed |
Conclusion
List comprehension offers the most readable and efficient solution for counting words containing a specific character. The find() method is also a good alternative when you need more control over the search process.
