Python program to check if two Strings are Anagram or not

Check if two Strings are Anagram or not

Strings are said to be anagrams only if all the characters present in 1st string are also present in 2nd string and no single characters should be more or less. When strings share same no of characters and also same characters then strings are called anagrams. Rearrangement of similar characters or letters in a string even if they don’t have the same meanings are anagrams. Only one strict rule is followed if we create anagrams of a string ‘count of every character available in 1st string should be equals to count of character in 2nd string for the same character’.

Python program to check if two Strings are Anagram or not


  • Step 1:- Start.
  • Step 2:- Take user inputs.
  • Step 3:- Check for length of both the strings.
  • Step 4:- If length do not matches Print Strings are not anagrams.
  • Step 5:- Else use sorted() function to sort the string.
  • Step 6:- Now check if String1 == String2.
  • Step 7:- If TRUE print Strings are anagrams.
  • Step 8:- If FALSE print Strings are not anagrams.
  • Step 9:- End.

Python program to check the strings are anagram or not

#take user input
String1 = input('Enter the 1st string :')
String2 = input('Enter the 2nd string :')
#check if length matches
if len(String1) != len(String2):
    #if False
    print('Strings are not anagram')
    #sorted function sort string by characters
    String1 = sorted(String1)
    String2 = sorted(String2)
    #check if now strings matches
    if String1 == String2:
        #if True
        print('Strings are anagram')
         print('Strings are not anagram')
Enter the 1st string :justiceforchutki
Enter the 2nd string :jfucoshtriuctiek
Strings are anagram