You've got the right idea, but since word1
and word2
are strings, they don't have the .sort()
attribute. You can still sort the letters within that string:
>>> w='hello'
>>> sorted(w)
['e', 'h', 'l', 'l', 'o']
The result of sorted()
is a list, we can turn it back to a string my joining them using an empty string:
>>> ''.join(sorted(w))
'ehllo'
Armed with this knowledge, your program might look something like:
word1 = input("Enter a string: ")
word2 = input("Enter a second string: ")
def isAnagram(s1, s2):
s1 = ''.join(sorted(word1))
s2 = ''.join(sorted(word2))
if s1 == s2:
print("This is an anagram")
else:
print("This is not an anagram")
isAnagram(word1, word2)