Python Program to Count of Words with specific letter


In this article, we will learn how to count Words with a specific letter in a string in python.

Methods Used

The following are the various methods to accomplish this task −

  • Using list comprehension, len() & split() functions

  • Using split() & find() functions

  • Using split(),replace() & len() functions

  • Using Counter() function

Example

Assume we have taken an input string and some random character. We will now count the words that contain the given input character in an input string.

Input

inputString = 'hello tutorialspoint python codes'
inputChar = "p"

Output

Count of words containing the char 'p' is: 2

In the above string, the words that contain the input character ‘p’ are tutorialspoint , python. Hence the output is 2.

Method 1: Using list comprehension, len() & split() functions

List Comprehension

When you wish to build a new list based on the values of an existing list, list comprehension provides a shorter/concise syntax.

len() − The number of items in an object is returned by the len() method. The len() function returns the number of characters in a string when the object is a string.

split() − splits a string into a list. We can define the separator; the default separator is any whitespace.

Algorithm (Steps)

Following are the Algorithm/steps to be followed to perform the desired task –.

  • Create a variable to store an input string.

  • Print the input list.

  • Create another variable to store an input character.

  • Use the split() function to split the input string into a list of words and traverse in that list and then check if the input character is present in the current list element

  • Print the count of words containing the given input character in an input string.

Example

The following program returns a count of words with the given input character in an input string using list comprehension, len() and split() functions –

# input string
inputString = 'hello tutorialspoint python codes'

# printing input string
print("Input String:", inputString)

# input character
inputChar = "p"

# splitting the input string into a list of words and traversing in that list

# and then checking if the input char is present in the current list element
wordsCount = len([element for element in inputString.split() if inputChar in element])

# printing the count of words containing the input character
print("The Count of words containing the char 'p' is:", wordsCount)

Output

On executing, the above program will generate the following output –

Input String: hello tutorialspoint python codes
The count of words containing the char 'p' is: 2

Method 2: Using split() & find() functions

find() method − Finds the first occurrence of the given value. This method returns -1 if the value is not found.

Syntax

string.find(value, start, end)

Example

The following program returns a count of words with the given input character in an input string using split() & find() functions –

# input string
inputString = 'hello tutorialspoint python codes'

# printing input string
print("Input String:", inputString)

# input character
inputChar = "p"

# storing the words count with the input character
wordsCount=0

# splitting input string into the list of words
wordsList= inputString.split()

# traversing in that words list
for element in wordsList:
   
   # checking whether input char is present in the current list element
   if(element.find(inputChar)!=-1):
      
      # incrementing word count value by 1 if the condition is true
      wordsCount+=1
print("The Count of words containing the char 'p' is:", wordsCount)

Output

On executing, the above program will generate the following output –

Input String: hello tutorialspoint python codes
The count of words containing the char 'p' is: 2

Method 3: Using split(),replace() & len() functions

replace() function − returns a copy of the string that replaces all occurrences of an old substring with another new substring.

Syntax

string.replace(old, new, count)

Example

The following program returns a count of words with the given input character in an input string using split(),replace() & len() functions –

# input string
inputString = 'hello tutorialspoint python codes'

# printing input string
print("Input String:", inputString)

# input character
inputChar = "p"

# storing the words count with the input character
wordsCount=0

# splitting input string into the list of words
wordsList= inputString.split()

# traversing in that words list
for element in wordsList:

   # replacing given input char with space and storing that string
   p = element.replace(inputChar, "")

   # incrementing the words count by 1 if the length of the above string # is less than the length of the current element
   if(len(p) < len(element)):
      wordsCount += 1
print("The Count of words containing the char 'p' is:", wordsCount)

Output

Input String: hello tutorialspoint python codes
The count of words containing the char 'p' is: 2

Method 4: Using Counter() function

Counter() function − a sub-class that counts the hashable objects. It implicitly creates a hash table of an iterable when called/invoked.

In this method, the Counter() function returns the frequency of characters of each word in an input string.

Example

The following program returns a count of words with the given input character in an input string using the Counter() function –

# importing a Counter function from the collections module
from collections import Counter

# input string
inputString = 'hello tutorialspoint python codes'

# printing input string
print("Input String:", inputString)

# input character
inputChar = "p"

# storing the words count with the input character
wordsCount = 0

# splitting input string into the list of words
wordsList = inputString.split()

# traversing through the elements of the words list
for element in wordsList:
   
   # getting the frequency of characters of the current word as a key-value pair
   ele_frequency = Counter(element)
   
   # checking whether the input char is present in the keys of the frequency dictionary
   if inputChar in ele_frequency.keys():
      
      # incrementing the words count by 1 if the condition is true
      wordsCount += 1
print("The Count of words containing the char 'p' is:", wordsCount)

Output

Input String: hello tutorialspoint python codes
The count of words containing the char 'p' is: 2

Conclusion

In this article, we studied 4 different ways to count words that start with a particular letter. We also learned how to use the Counter() function to obtain the frequency of the iterable items (dictionary hashing)

Updated on: 27-Jan-2023

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements