Skip to content
Related Articles

Related Articles

Python – Count of Words with specific letter

View Discussion
Improve Article
Save Article
  • Last Updated : 16 Aug, 2022
View Discussion
Improve Article
Save Article

Given a string of words, extract the count of words with a specific letters.

Input : test_str = ‘geeksforgeeks is best for geeks’, letter = “g” Output : 2 Explanation : “g” occurs in 2 words. Input : test_str = ‘geeksforgeeks is best for geeks’, letter = “s” Output : s Explanation : “s” occurs in 4 words.

Method #1 : Using list comprehension + len() + split()

This is one of the ways in which this task can be performed. In this, we perform task of extracting words from string using split() and loop is used to iterate words to check for letter existence of letter and len() is used to fetch the number of words with letter.

Python3




# Python3 code to demonstrate working of
# Count of Words with specific letter
# Using list comprehension + len() + split()
 
# initializing string
test_str = 'geeksforgeeks is best for geeks'
 
# printing original string
print("The original string is : " + str(test_str))
 
# initializing letter
letter = "e"
 
# extracting desired count using len()
# list comprehension is used as shorthand
res = len([ele for ele in test_str.split() if letter in ele])
 
# printing result
print("Words count : " + str(res))

Output

The original string is : geeksforgeeks is best for geeks
Words count : 3

Method #2 : Using filter() + lambda + len() + split()

This is yet another way in which this task can be performed. In this, we perform the task of filtering using filter() + lambda.

Python3




# Python3 code to demonstrate working of
# Count of Words with specific letter
# Using filter() + lambda + len() + split()
 
# initializing string
test_str = 'geeksforgeeks is best for geeks'
 
# printing original string
print("The original string is : " + str(test_str))
 
# initializing letter
letter = "e"
 
# extracting desired count using len()
# filter() used to check for letter existence
res = len(list(filter(lambda ele : letter in ele, test_str.split())))
 
# printing result
print("Words count : " + str(res))

Output

The original string is : geeksforgeeks is best for geeks
Words count : 3

Method #3 : Using len() + for loop

We use len() and for loop to iterate over the string to solve the problem.

Python3




# Python3 code to get count of Words with specific letter
 
# initializing string
test_str = 'geeksforgeeks is best for geeks'
 
# printing original string
print("The original string is : " + str(test_str))
 
# initializing letter
letter = "e"
""" As we have to only count one letter per word we have to
    check if we have checked that word or not and to do so
    we use variable word_count that become odd if we founded the letter
    in the word and even when not"""
word_count = 0
 
# count of words with specific letter
word = 0
 
# For loop to iterate over string
for i in range(len(test_str)):
    if word_count % 2 == 0 and test_str[i] == letter:
        word += 1
        word_count = 1
        # set word_count an odd number so we know to not check the current word.
    elif test_str[i] == " ":
        """    New word starts with " "  """
        word_count = 0
        # set word_count an even number so we know to check the current word.
print("Words count :", word)
# This code is contributed by Shivesh Kumar Dwivedi

Output

The original string is : geeksforgeeks is best for geeks
Words count : 3

Method #4 : Using split() and find()

Python3




# Python3 code to demonstrate working of
# Count of Words with specific letter
# Using split() and find()
 
# initializing string
test_str = 'geeksforgeeks is best for geeks'
 
# printing original string
print("The original string is : " + str(test_str))
 
# initializing letter
letter = "e"
 
# extracting words with specific letter
res=0
x=test_str.split()
for i in x:
    if(i.find(letter)!=-1):
        res+=1
 
# printing result
print("Words count : " + str(res))

Output

The original string is : geeksforgeeks is best for geeks
Words count : 3

The Time and Space Complexity for all the methods are the same

Time Complexity: O(n)

Space Complexity: O(n)


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!