Write a PYTHON function that checks whether two words are anagrams. Two words ar
ID: 3760744 • Letter: W
Question
Write a PYTHON function that checks whether two words are anagrams.
Two words are anagrams if they contain the same letters.
For example, silent and listen are anagrams. The header of the function is: def isAnagram(s1, s2):
(Hint: Create two lists from the two strings entered by the user. Sort the lists using sort method and check if two lists are identical.) Write a main function that prompts the user to enter two strings and displays the ouput: Sample output: The words silent and listen are anagrams The words silent and learning are not anagrams
Explanation / Answer
def anagrams(str1,str2):
str1List = list(str1)
str2List = list(str2)
str1List.sort()
str2List.sort()
if str1List == str2List:
print str1 + ' and '+str2+ ' are anagrams.'
else:
print str1 + ' and '+str2+ ' are not anagrams.'
str1 = raw_input('Enter string 1 : ')
str2 = raw_input('Enter string 2 : ')
anagrams(str1,str2)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.