This post shows how to check if two strings are anagrams in Python. Two strings are anagrams if they contain exactly the same letters, just rearranged — like "listen" and "silent".

Check anagram strings in Python:

Explanation:
If two strings are anagrams, sorting the letters of both strings produces identical results. We remove spaces, convert both strings to lowercase for a case-insensitive comparison, sort each one, and compare.


Python Program to Check Anagram

str1 = input("Enter first string: ").replace(" ", "").lower()
str2 = input("Enter second string: ").replace(" ", "").lower()

if sorted(str1) == sorted(str2):
    print("The strings are anagrams")
else:
    print("The strings are not anagrams")

Output:

Enter first string: listen
Enter second string: silent
The strings are anagrams

Anagram checks come up often in coding interviews — practice similar problems in our Interview Prep section.