codescracker
python

Python Program to Check Two Strings are Anagram or Not



« Python Examples Python Tutorial »


This article is created to cover some programs in Python, that receives two strings by user at run-time, and checks whether two strings are anagram or not. Here are the list of programs:

Before created these programs, let's first understand about anagram.

What are Anagram Strings ?

Two strings can be called as anagram, when:

For Example, abc and cba are two anagram strings. Similarly, creative and reactive are also anagram. That is, characters of creative can be rearranged to form reactive and vice-versa.

Check Two Strings are Anagram using for Loop

This program is created using for loop to check whether the two strings entered by user are anagram or not. The question is, write a Python program to check Anagram or not without sorting the string. Here is its answer:

# ----codescracker.com----

print("Enter the First String:")
textOne = str(input())
print("Enter the Second String:")
textTwo = str(input())
found=0
notFound=0
lenOne = len(textOne)
lenTwo = len(textTwo)
if lenOne == lenTwo:
    for i in range(lenOne):
        found = 0
        for j in range(lenOne):
            if textOne[i] == textTwo[j]:
                found = 1
                break
        if found==0:
            notFound = 1
            break
    if notFound==1:
        print("\nStrings are Not Anagram")
    else:
        print("\nStrings are Anagram")
else:
    print("\nLength of Strings are not Equal")

Here is its sample run:

python check anagram strings

Now supply two strings input say listen as first and silent as second string and press ENTER key to check and print whether these two strings are anagram or not as shown in the snapshot given below:

check strings are anagram or not python

Here is another sample run with user input listen and silenx as first and second string:

check anagram strings python

And here is the third sample run with user input codes and cracker as first and second string:

check anagram string program python

Note - The str() method is used to convert any type of value to a string type. And len() method returns length of string passed as its argument.

The dry run of above program with user input listen and silent, goes like:

Modified Version of Previous Program

This program uses sorted() method to sort the string and compares strings in character by character manner, to check whether two strings are anagram or not. The end= is used to skip inserting an automatic newline using print()

# ----codescracker.com----

print(end="Enter the First String: ")
textOne = str(input())
print(end="Enter the Second String: ")
textTwo = str(input())
lenOne = len(textOne)
lenTwo = len(textTwo)
notFound=0
if lenOne == lenTwo:
    textOne = sorted(textOne)
    textTwo = sorted(textTwo)
    for i in range(lenOne):
        if textOne[i] != textTwo[i]:
            notFound=1
            break
    if notFound==0:
        print("\nStrings are Anagram")
    else:
        print("\nStrings are Not Anagram")
else:
    print("\nLength of Strings are not Equal")

Here is its sample run with user input creative and reactive as two strings:

check anagram string python

Check Anagram Strings using sorted()

This program uses sorted() method to do the same job as previous program does. The two strings entered by user gets compared directly using == operator.

# ----codescracker.com----

print("Enter First String: ", end="")
textOne = str(input())
print("Enter Second String: ", end="")
textTwo = str(input())

lenOne = len(textOne)
lenTwo = len(textTwo)

if lenOne == lenTwo:
    if sorted(textOne) == sorted(textTwo):
        print("\nStrings are Anagram")
    else:
        print("\nStrings are Not Anagram")
else:
    print("\nLength of Strings are not Equal")

Here is its sample run with user input sadder and dreads:

python check anagram string using sorted

Check Anagram Strings using Function

This program is created using user-defined function named CheckAnag(). The function receives two strings as its argument and returns 1 if both strings are anagram.

# ----codescracker.com----

def CheckAnag(sOne, sTwo):
    if sorted(sOne) == sorted(sTwo):
        return 1

print("Enter First String: ", end="")
textOne = str(input())
print("Enter Second String: ", end="")
textTwo = str(input())

rVal = CheckAnag(textOne, textTwo)
if rVal==1:
        print("\nStrings are Anagram")
else:
    print("\nStrings are Not Anagram")

Check Anagram Strings using Class

This is the last program created using class, an object-oriented feature of Python. To access member function of a class, an object is required. Therefore an object named obj is created of class CodesCracker to access its member function named CheckAnag() using dot (.) operator.

# ----codescracker.com----

class CodesCracker:
    def CheckAnag(self, sOne, sTwo):
        if sorted(sOne) == sorted(sTwo):
            return 1

print("Enter First String: ", end="")
textOne = str(input())
print("Enter Second String: ", end="")
textTwo = str(input())

obj = CodesCracker()
rVal = obj.CheckAnag(textOne, textTwo)

if rVal==1:
        print("\nStrings are Anagram")
else:
    print("\nStrings are Not Anagram")

« Python Examples Python Tutorial »