Python Program to Check If Two Strings are Anagram

To understand this example, you should have the knowledge of the following Python programming topics:


Two strings are said to be anagram if we can form one string by arranging the characters of another string. For example, Race and Care. Here, we can form Race by arranging the characters of Care.


Python program to check if two strings are anagrams using sorted()

Output

race and care are anagram.

We first convert the strings to lowercase. It is because Python is case sensitive (i.e. R and r are two different characters in Python).

Here,

  • lower() - converts the characters into lower case
  • sorted() - sorts both the strings

If sorted arrays are equal, then the strings are anagram.