Python program to check if the given strings are anagram

To check if two strings are anagrams of each other, you need to compare if they have the same characters in the same frequency. You can achieve this by sorting both strings and then comparing them. Here's a Python program to check if two given strings are anagrams:

def are_anagrams(str1, str2):

# Remove spaces and convert to lowercase for case-insensitive comparison

str1 = str1.replace(" ", "").lower()

str2 = str2.replace(" ", "").lower()

# Sort the characters of both strings

sorted_str1 = sorted(str1)

sorted_str2 = sorted(str2)

# Compare the sorted strings

return sorted_str1 == sorted_str2

# Example usage:

string1 = input("Enter the first string: ")

string2 = input("Enter the second string: ")

if are_anagrams(string1, string2):

print("The strings are anagrams.")


print("The strings are not anagrams.")


This program prompts the user to input two strings, removes spaces and converts them to lowercase, sorts the characters of both strings, and then compares them. If the sorted strings are equal, it prints that the strings are anagrams; otherwise, it prints that they are not anagrams.