Do you ever play with words, make new ones, and try to find connections between them? There’s a type of puzzle called anagrams that challenges you to make new words from a given set of letters.
Python is a powerful programming language that offers many features for developers. This blog post will show how to use Python to find if two strings are anagrams. We will also provide code examples so you can try it out yourself. Happy coding!
Table of Contents
What is Anagram?
An Anagram is a word that can be created by rearranging the letters of another word. Such two strings will be called anagrams. For example, from the word HEART, we can make EARTH. So, Heart and Earth are anagrams. There are other examples like Fried and Fired, Listen and Silent, Keen and Knee, Race and Care, Triangle and Integral, and many more.
To understand Python Anagrams, we will study two methods to find if strings are anagrams in Python.
Method# 01: Sorted( ) Function
Python uses a built-in sorted( ) function in the program to check if two strings are anagrams or not. The sorted( ) function does not change the given original string and returns the sorted string in the output.
Let’s study the example for better understanding.
def find(string1, string2):
if(sorted(string1)== sorted(string2)):
print("The strings are Anagrams.")
else:
print("The strings are not Anagrams.")
string1 ="triangle"
string2 ="integral"
find(string1, string2)
Output

Method# 02: Counter( ) Function
We can also use the Python counter() function to find an anagram in the program. The counter() function counts the occurrence frequency of both strings provided. If the frequencies match, the strings are anagrams.
Here’s one example to study the implementation.
from collections import Counter
def find(string1, string2):
# the sorted strings are checked
if(Counter(string1) == Counter(string2)):
print("The strings are Anagrams.")
else:
print("The strings are not Anagrams.")
# driver code
string1 ="triangle"
string2 ="integral"
find(string1, string2)
Output

Method# 03: Reverse String
This method is similar to finding Palindrome. One reversed string will be compared with the other string, and if it matches, the program will call it anagram string.
Learn Python Palindromes.
Let’s check the python implementation in the following program.
find_list = ["tic", "rat", "cit", "Tall"]
python_anagrams = {}
for x in find_list:
reverse=x[::-1]
if reverse in find_list:
python_anagrams[x] = (find_list.pop(find_list.index(reverse)))
print(python_anagrams)
Output

Learn about how to reverse list in Python.
Conclusion
We discussed two methods on how to find if python strings are anagrams. I hope this article helped you in understanding Python Anagram. Also, if you want to learn more about Python or programming in general, be sure to check out our other tutorials.
FAQs
What sort of programming language is best suited for checking anagrams?
There is no one-size-fits-all answer to this question, as the best programming language for checking anagrams will vary depending on the specific task at hand. However, some of the most popular languages that are used for this purpose include Python, C++, and Java.
How hard was it for you to write your python program to find anagrams?
Python is a powerful language that is easy to learn and use, so you can easily create programs to meet your specific needs. Using these simple tools, you can easily create a program to help you with your particular task.