Pythondex
Python Programs

Making An Python Anagram Solver With Code

Last updated April 24, 2023 by Jarvis Silva

Today in this python tutorial we will see how to make a python anagram solver. If you are looking to build an anagram solver using python then this is the only tutorial you need to follow.

An anagram is a word that is rephrased or rearranged to form another word, for example anagram of arc is car, dam is mad, etc. Another good example is “I’m a dot in place” will become “A decimal point”.

I hope you got what is an anagram so we want to build an anagram solver which will ask the user to enter a word that they want anagrams of then it will provide a list of suitable anagrams words.

Making An Python Anagram Solver

Making an anagram solver in python looks complicated and difficult by the way but we will use python libraries which will help us solve anagram in python with just a few lines of code.

So before we go further if you don’t have python installed on your computer then you need to first download and install it on your computer for this you can refer to this guide: Installing and setting up python.

Install pyenchant library

This is the python library which we will use, It is a spell checking library it suggests correct words when miss-spelled so you need to install this library so to install this library use the below command


pip install pyenchant

After you install the library you need the code so create a python file and copy and paste the below code

Anagram Solver Python Code



from itertools import permutations,combinations
import enchant

d = enchant.Dict("en_US")
word = input('Enter word: ')
letters = [chr for chr in word]
repeat_check = []

for number in range(3,len(letters)+1): #For Loop
    for current_set in combinations(letters,number): #Combinations Function
        #Code for the Basic Anagram Finder
        for current in permutations(current_set):
            current_word = ''.join(current)
            if d.check(current_word)and current_word not in repeat_check:
                print(current_word)
                repeat_check.append(current_word)

As you can see the code is just around 15 lines because we used the pyenchat library, now you can run this program to run it use the below command.


python filename.py

You have to run the command in your terminal and enter the name of your python filename you want to run, Below is an example output


Enter word: gainly
lying
inlay
gainly
laying

After running it will ask you to enter a word you want the anagram of. In my case I have entered “gainly” . It gave me many words as you can see above.

You can use this anagram program to solve any anagram puzzle. So this was it. I hope you found this program useful and helpful. Now let’s see more about this program.

Summary

This was the python anagram solver program. This program looks very simple because libraries remove a lot of code complexities.

Want more python tutorials like this then here are some more guides you may find helpful:

I hope you found what you were looking for in this python tutorial. If you want more guides like this do join our Telegram channel to stay updated.

Thanks for reading, have a nice day 🙂