Dark Mode On/Off
PUBLISHED ON: JULY 6, 2021

# Python Words Frequency in String Shorthands

In this tutorial, we will learn to get the frequency of words in a Python string. Strings in Python are a sequence of characters wrapped inside single, double, or triple quotes. Word can be defined as a substring that exists in the string. The frequency of a word is simply the total number of times that word occurs in the string. For example,

Input: string="Python Language"

Output: { 'Python':1, 'Language':1 }

To solve this problem, there are two shorthand ways

1. By using dictionary comprehension
2. By using Counter() and split()

We will be discussing all these approaches separately below.

## Approach 1: Using dictionary comprehension

Dictionaries are much like lists with an extra parameter called keys to access the elements of a dictionary. Now, to extract the words in the string, we will use the split() function, and to count the occurrence we will use the count() function.

### Algorithm

Follow the algorithm to understand the approach better.

Step 1- Define a string with values

Step 2- Print the string

Step 3- Declare a dictionary to store the result

Step 4- Using dictionary comprehension store the words as the key and their count as the values

Step 5- Print the resulting dictionary

### Python Program 1

Look at the program to understand the implementation of the above-mentioned approach. We have used a dictionary to count and display all the words in the string. The result will be stored using dictionary comprehension which is a shorter syntax for dictionary operations.

``````string=" This tutorial for Python tutorial"
print(string)

# dictionary and count() and split()
word= {key: string.count(key) for key in string.split()}

# print result
print("Words in the string")
print(word)``````

This tutorial for Python tutorial
Words in the string
{'This': 1, 'tutorial': 2, 'for': 1, 'Python': 1}

## Approach 2: By using the Counter and split() function

The Counter is used to count hashable objects. It implicitly creates a hash table of an iterable when invoked. It returns an itertool of all the known elements in the Counter object. To extract the words, we will use the split() function of the String class.

### Algorithm

Follow the algorithm to understand the approach better.

Step 1- Import counter in program

Step 2- Define a string with values

Step 3- Print the string

Step 4- Declare another variable that will store the value returned by the Counter

Step 5- Extract words using split()

Step 6- Print the result

### Python Program 2

Look at the program to understand the implementation of the above-mentioned approach. To use the Counter(), we have to import Counter from collections. Python Counter is a subclass of the dictionary class.

``````from collections import Counter
string=" This tutorial for Python for"
print(string)

word= Counter(string.split())

# print result
print("Words in the string")
print(word)``````

This tutorial for Python for
Words in the string
Counter({'for': 2, 'This': 1, 'tutorial': 1, 'Python': 1})

## Conclusion

In this tutorial, we have discussed the shortcut approaches for finding the frequency of words in Python. We have also discussed some built-in functions in our program. split(), count() and Counter() were used in the approaches discussed above.

Want to learn coding?
Try our new interactive courses.
Over 20,000+ students enrolled.