Moses Saygbe Moses Saygbe - 10 months ago 70
Python Question

Anagrams in python (list)

I have to write a program that takes 2 words and checks and sees if they are anagrams.

Similar to other post i just could not figure out hoe to do it. there are just some minor problems wrong with the code.

heres my code so far:

s1 = input("Enter in first word:")
s2 = input("Enter in second word:")

def isAnagram(s1, s2):
s1 = s1
s2 = s2
for i in range[0, len(s1),1]:
if i in range [0, len(s2), 1]:
print("Is in anagram!")
print("Is not anagram!")


I'm unable to figure out how to finish it and why it keeps saying isAnagram is missing 2 required positional arguments .


You need to count whether each word has each letter the same number of times. Simply iterating like that may require a few passes over the string. By sorting them alphabetically, you can easily check if they are anargrams.

def is_anagram(s1, s2):
    return sorted(s1) == sorted(s2)


>>> is_anagram('snake', 'kanes')

However, this is case sensitive, in that it considers S different from s.

>>> is_anagram('Snake', 'Kanes')

If you want this latter case to return True, then simply convert all letters to lower or upper case, i.e., return sorted(s1.lower()) == sorted(s2.lower())