Anagram Check in Python

Check whether two strings are anagrams of each other.

BeginnerString ProgramsExample 18 of 25
anagram-check.py
Run in browser
1# Program to check anagram strings
2
3s1 = input("Enter first string: ").replace(" ", "").lower()
4s2 = input("Enter second string: ").replace(" ", "").lower()
5
6if sorted(s1) == sorted(s2):
7 print("Anagram")
8else:
9 print("Not an anagram")

Output

Enter first string: listen
Enter second string: silent
Anagram

What's going on

We normalize by removing spaces and lowercasing, then sort the characters and compare.