How to Check If Two Strings are Anagram in Python
In this tutorial, we’ll learn how to program "How to Check If Two Strings are Anagrams in Python." The objective is to carefully determine whether two strings are anagrams of each other. This tutorial will guide you through the process step by step, ensuring a clear understanding of how to implement an effective anagram check. So, let’s get started!
This topic is straightforward to understand. Just follow the instructions I provide, and you’ll complete it with ease. The program I’ll demonstrate will show you the proper way to check whether two strings are anagrams. So, let’s dive into the coding process!
Getting Started:
First you will have to download & install the Python IDLE's, here's the link for the Integrated Development And Learning Environment for Python https://www.python.org/downloads/.
Creating Main Function
This is the main function of the application. The following code will display a simple GUI in terminal console that will display program. To do this, simply copy and paste these blocks of code into the IDLE text editor.- def check(s1, s2):
- if(sorted(s1)== sorted(s2)):
- print("The strings are anagrams.")
- else:
- print("The strings aren't anagrams.")
- while True:
- print("\n================= Check If Two Strings are Anagram =================\n\n")
- s1 = input("Enter first string: ")
- print("First string: ", s1)
- s2 = input("Enter second string: ")
- print("Second string: ", s2)
- print("\n")
- check(s1, s2)
- opt = input("\nDo you want to try again?(yes/no): ")
- if opt.lower() == 'yes':
- ret=False
- elif opt.lower() == 'no':
- ret=True
- print("Exiting program....")
- else:
- print("Please enter yes/no:")
- break
- if ret == False:
- continue
This program checks if two input strings are anagrams of each other by comparing their sorted versions. If the sorted strings match, it outputs that the strings are anagrams; otherwise, it states they are not. The user can choose to repeat the process or exit the program.
Output:
There you have it we successfully created How to Check If Two Strings are Anagram in Python. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!
More Tutorials for Python Language
Add new comment
- 15 views