Check if Two Strings are Anagrams in Python Using sort()
(Last Updated On: 03/04/2023)
Python Program to Check if Two Strings are Anagrams Using Inbuilt List & sort() Function
# Check If Two Strings are Anagram Using Inbuilt List and Sort() Methods in python
#Declare Inputs
inp1 = "listen"
inp2 = "silent"
#Sort Elements
x = [inp1[i] for i in range(0,len(inp1))]
x.sort()
y = [inp2[i] for i in range(0,len(inp2))]
y.sort()
# the sorted strings are checked
if (x == y):print("The strings are anagrams.")
else: print("The strings aren't anagrams.")
## Example 2 for "The strings aren't anagrams."
#Declare Inputs
inp1 = "listen"
inp2 = "silenti"
#Sort Elements
x = [inp1[i] for i in range(0,len(inp1))]
x.sort()
y = [inp2[i] for i in range(0,len(inp2))]
y.sort()
# the sorted strings are checked
if (x == y):print("The strings are anagrams.")
else: print("The strings aren't anagrams.")
Output:
The strings are anagrams.
The strings aren't anagrams.