Python program to check if two strings are anagram or not
Problem: This is a python program that takes two strings and checks if given strings are anagram or not.
Input: string1 = Education string2 = Cautioned Output: The strings are anagrams Input: string1 = cat string2 = rat Output: The strings are not anagrams
- Take two strings from the user and store them in a different variable.
- Sort both strings into a list using the sorted() function.
- Compare both the result and check if both are equal.
- Print the result.
The following is a source code to detect string anagram in Python:
#Program checks if two strings are anagram or not #Take input from user string1 = input("Enter first string:") string2 = input("Enter second string:") if sorted(string1) == sorted(string2): print("The strings are anagrams.") else: print("The strings aren't anagrams.")
Explanation: In this code, we first need to enter the two strings and store them in a separate variable. Second, both string's characters are sorted into separate lists and then use if statements to verify whether strings are anagram or not. If both the sorted strings matches, it will display the strings are an anagram. If they are not matched, it will display the string is not an anagram.