How Do You Write a Python Program to Test If Two Words Are Anagrams?

Problem scenario
You want a program to test if two words are anagramous with each other. You want the test to be case insensitive. How do you write a program that will interactively prompt the user for two different words and test if they are anagrams?

Solution
Use this Python 3 program (and it will interactively prompt you to enter two words):

word1 = input("Enter one word: ").lower()
word2 = input("Enter a second word: ").lower()

wL1 = list(word1)
wL2 = list(word2)

wL1.sort()
wL2.sort()

if (wL1 == wL2):
  print("The two programs are anagrams!")
else:
  print("The two words are NOT anagrams!")

It will not work with Python 2.

Leave a comment

Your email address will not be published. Required fields are marked *