Anagram Check in Python

Check whether two strings are anagrams of each other.

BeginnerString ProgramsExample 18 of 25
anagram-check.py
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.

After this program

If the idea is still fuzzy, read Day 4: Strings and String Methods in the Python course. For a short check, try the Python quiz (300 questions). Stay with all 25 string programs, or see every topic on Python exercises.