This is a Python Program to detect if two strings are anagrams.
The program takes two strings and checks if the two strings are anagrams.
1. Take two strings from the user and store them in separate variables.
2. Then use sorted() to sort both the strings into lists.
3. Compare the sorted lists and check if they are equal.
4. Print the final result.
Here is source code of the Python Program to detect if two strings are anagrams. The program output is also shown below.
s1=raw_input("Enter first string:") s2=raw_input("Enter second string:") if(sorted(s1)==sorted(s2)): print("The strings are anagrams.") else: print("The strings aren't anagrams.")
1. User must enter both the strings and store them in separate variables.
2. The characters of both the strings are sorted into separate lists.
3. They are then checked whether they are equal or not using an if statement.
4. If they are equal, they are anagrams as the characters are simply jumbled in anagrams.
5. If they aren’t equal, the strings aren’t anagrams.
6. The final result is printed.
Case 1: Enter first string:anagram Enter second string:nagaram The strings are anagrams. Case 2: Enter first string:hello Enter second string:world The strings aren't anagrams.
Sanfoundry Global Education & Learning Series – Python Programs.
To practice all Python programs, here is complete set of 150+ Python Problems and Solutions.