Check if Two Strings Are Anagrams in Python Using counter()

Have you ever wondered how to determine if two words are anagrams? An anagram is a word or phrase formed by rearranging the letters of another word or phrase. In this blog, we will explore a Python program to check if two strings are Anagrams using the counter() function. This program provides a simple and efficient solution to the anagram problem. We will delve into the step-by-step logic and operation of the code, making it easy for beginners to understand. 

So, let’s dive in and discover how to check if two strings are anagrams in Python using counter().

Python Program to Check if Two Strings Are Anagrams Using counter() Function

# Check If Two Strings are Anagram Using counter() function in python
# Python3 program for the above approach from collections import Counter
# function to check if two strings are anagram or not

def check(s1, s2):
	# implementing counter function
	if(Counter(s1) == Counter(s2)):
		print("The strings are anagrams.")
		print("The strings aren't anagrams.")
# driver code
s1 = "listen"
s2 = "silent"
check(s1, s2)

Code Logic Explanation

Importing the Counter() Function

We begin by importing the Counter() function from the collections module. The Counter() function is a powerful tool that helps us count the occurrences of elements in a collection.

Defining the Function to Check Anagrams

Next, we define a function called check() that takes two strings, s1 and s2, as arguments. This function will determine whether the given strings are anagrams.

Implementing the Counter() Function

Within the check() function, we use the Counter() function to create a dictionary-like object that holds the counts of each character in both strings. We compare the resulting counters of s1 and s2 using the equality operator (==).

Checking Anagram Condition

We compare the counters of s1 and s2 using the equality operator (==). If the counters are equal, it means that both strings have the same characters in the same quantities, indicating that they are anagrams.

Printing the Result

Finally, we print the desired message based on the result of the anagram check. If the counters are equal, we print “The strings are anagrams.” Otherwise, we print “The strings aren’t anagrams.”

Testing the Code

To test our code, we provide two example strings, s1 = “listen” and s2 = “silent”. We call the check() function with these strings as arguments, which performs the anagram check and prints the result.


The output of the code is a message that indicates whether the two strings are anagrams or not. In this example, the output will be:

The strings are anagrams.

Here are a few alternate methods:

Using sorted() Function:

It can be used to sort the characters of a string and return a new sorted list. To check if two strings are anagrams, you can sort the characters of both strings using sorted() and compare the sorted lists. The advantage of using sorted() is that it directly returns the sorted list without modifying the original string. However, using sorted() requires additional memory to store the sorted lists, and it may not be as efficient as the inbuilt list and sort() method.

Using sort():

By converting the strings to lists, you can use the sort() method to sort the characters. The advantage of using the sort() method is that it directly modifies the original list without the need for creating new lists or importing additional modules. It is a simple and efficient approach for checking if two strings are anagrams, particularly when you are working with lists.

In our code, we used the Counter() method as it provides a concise and efficient solution for checking anagrams in Python. It simplifies the process by automatically counting the occurrences of each character in a string, eliminating the need for explicit loops or data structures. The Counter() function handles the counting and comparison of characters behind the scenes, allowing for more readable and brief code. This method is particularly beneficial when dealing with larger strings or multiple strings, as it offers a straightforward way to compare character counts and determine anagram relationships.


In conclusion, we have explored a Python program that checks whether two strings are anagrams or not using the Counter() function from the collections module. By comparing the occurrences of each character in the strings, we can determine if they have the same characters with the same frequencies, indicating that they are anagrams. The Counter() function simplifies the process of counting and comparing characters, making it easier to solve the anagram problem in Python. This program provides a convenient way to identify anagrams and can be used in various applications that involve string manipulation and comparison.

Frequently Asked Questions

Q: What if the two counter objects are not equal?

A: If the two Counter objects are not equal, it means the strings have different characters or different counts of characters. In this case, the check() function determines that the strings are not anagrams.

Q: Can I input strings with different lengths?

A: Yes, this code can handle strings with different lengths. The Counter() function counts the occurrences of characters regardless of the string’s length. It compares the characters and their frequencies, regardless of the overall length of the strings.

Q: How does the Counter() function work?

A: The Counter() function takes a sequence (such as a string) as input and counts the occurrences of each element. It creates a Counter object that stores the elements as dictionary keys and their counts as dictionary values.

Q: How does the code handle case sensitivity?

A: The code is case-sensitive, meaning that uppercase and lowercase letters are treated as different characters. For example, “listen” and “Silent” would not be considered anagrams. To make the code case-insensitive, you can convert the input strings to lowercase or uppercase before passing them to the Counter() function.

Q: Can I check if phrases or sentences are anagrams?

A: No, this code can check anagrams for individual words. This code only compares individual characters within the strings.

About The Author

Leave a Reply