Check If Two Strings Are Anagram in Python

Writing up a python code is pretty simple, however, in the end, it’s your problem-solving ability that makes the difference between a decent programmer and a good programmer. And when it comes to programming, even if you know everything it’s pretty much worthless if you don’t have practical knowledge. So to get good at Programming you must practice as many tricky questions as you can. The more tricky programs you solve the sharper your problem-solving skills get. Strings are some of the first things we learn to play with. Therefore in this article let’s learn how to Check If Two Strings Are Anagram or Not in Python.


Practising these types of questions also helps to get an upper edge in Competitive Programming. So open up your IDE and let’s get started real quick. Once you understand the logic practice this program on your own to make your brain work in a problem-solving way. This is a somewhat easy question so you might not get asked in an interview. But make sure to practice it on your own after reading this article.


What is An Anagram String?


  • An anagram of a string is a string that consists same no of characters representing the same letters as that of the string we’re comparing with. Though anagram strings have the same character count the order in which characters are written should be different in both the strings.


  • For example, “abcd” and “dcba” are an anagram of each other.


What’s The Approach?


  • We will define two strings that we are going to compare in this program. So we’re simply going to use the sorting method to check if the input strings are anagrams or not.


  • But before that, we will check the length of both the strings if the length is not similar then it will be clear that strings are not anagrams.


  • However, if the length of strings is equal then we will sort both of them in ascending order using the sorted() function. After that, we will check if both the sorted strings are equal to each other or not. If yes then strings are anagrams and vice versa.


Also Read: How To Run Python in Visual Studio Code on Mac OS


Python Program To Check If Two Strings Are Anagram or Not




str1 = "TRIANGLE"
str2 = "INTEGRAL"
The two strings are anagram of each other
# Python program to check whether two strings are
# anagrams of each other

# function to check whether two strings are anagram
# of each other

def areAnagram(str1, str2):
    # Get lengths of both strings
    n1 = len(str1)
    n2 = len(str2)

    # If the lenght of both strings is not the same, then
    # they cannot be an anagram
    if n1 != n2:
        return 0

    # Sort both strings
    str1 = sorted(str1)
    str2 = sorted(str2)

    # Compare sorted strings
    for i in range(0, n1):
        if str1[i] != str2[i]:
            return 0

    return 1

# Driver code
str1 = "TRIANGLE"
str2 = "INTEGRAL"

# Function Call
if areAnagram(str1, str2):

    print("The two strings are anagram of each other")
    print("The two strings are not anagram of each other")



I'm a coding geek interested in cyberspace who loves to write and read

Leave a Reply

Your email address will not be published. Required fields are marked *