Lokasi ngalangkungan proxy:   [ UP ]  
[Ngawartoskeun bug]   [Panyetelan cookie]                
Skip to content
No results
  • About AskPython
  • Blog
  • Contact Me
  • Free Online Developer Tools Hub
  • Home
  • Meet the Team
  • Privacy Policy
  • Python
  • Python Course for Beginners Online FREE
  • Python Tutorial – Mega Index
  • Terms and Conditions

🚀 Supercharge your YouTube channel's growth with AI.

Try YTGrowAI Free
  • Home
  • Blog
  • About AskPython
  • Python Course
  • Start Here

Introduction to Anagrams in Python

  • Isha BansalIsha Bansal
  • June 17, 2021
  • Python Programming Examples
Introduction to Anagrams in Python

Hey there! Today we are going to learn about implementing an interesting topic known as Anagrams in Python. Let us first understand what an Anagram is.

What is an Anagram?

Anagram is interesting suspense behind the words and sentences. If all the letters of a particular word or sentence can form other words or sentences after rearranging them, then all of them are anagrams to each other.

Some examples of anagrams are ‘sram’ and ‘mars’, ‘top’ and ‘otp’, and many more. But now the next question is why to even learn about anagrams?

Anagrams can be really helpful to writers as they add an extra layer of suspense to the writing and they are a clever and playful way to make the writing interesting. Using anagrams can be really interesting and fun.

Checking for Anagrams in Python

Let’s look at how we can identify anagrams in Python using a simple algorithm.

Algorithm for checking if two words are Anagrams or not

The steps below show how to check if two strings are anagrams or not.

STEP 1: Take input of the 1st string
STEP 2: Take input of the 2nd string
STEP 3: Sort all the letters of both the strings
STEP 4: Check if after sorting both the strings match.
if they match: Anagram
if not: Not an Anagram

Program to check if two strings are Anagrams or not

s1 = input()
s2 = input()
s1 = sorted(s1)
s2 = sorted(s2)
if(s1==s2):
    print("Anagram")
else:
    print("Not an Anagram")

The results for some sample strings are shown below. The first strings which were checked were tac and cat, and tic and cat. We can clearly see that the first pair is an anagram whereas the second pair is not an anagram.

tac
cat
Anagram
tic
cat
Not an Anagram

Conclusion

Congratulations! We learned about Anagrams and how to implement them in the Python programming language. I hope now you are clear with anagrams and can implement it yourself!

Happy coding! Thank you for reading!

Isha Bansal
Isha Bansal

Hey there stranger!
Do check out my blogs if you are a keen learner!

Hope you like them!

Articles: 185
3 Matplotlib Plotting Tips to Make Plotting Effective
Previous Post 3 Matplotlib Plotting Tips to Make Plotting Effective
Next Post Python fork() - How to create child processes using the fork() method?
Python fork() – How to create child processes using the fork() method?

Related Posts

How to Calculate Distance Between Two Points in Python

How to Calculate Distance Between Two Points in Python

  • November 26, 2026
CIFAR 10 Dataset: Load and Use in Python

CIFAR 10 Dataset: Load and Use in Python

  • November 24, 2026
Maximum A Posteriori Estimation in Python

Maximum A Posteriori Estimation in Python

  • November 24, 2026

Partner Websites

  • GoLangDocs
  • LinuxForDevices
  • CodeForGeek

Popular Categories

  • Python Programming
  • Python Modules
  • OOPS in Python
  • Python Programming Examples
  • Strings in Python

Hall of Fame

  • Python Programming
  • OOPS in Python
  • Creating Python Modules
  • Args and Kwargs in Python
  • Python Dictionary (Dict) Tutorial
  • Python List – 15 Things You MUST Know
  • Python Set – Things You MUST Know
  • Python String Functions: Methods by Task

Important Links

  • Home
  • Python Course
  • Start Here
  • Blog
  • About AskPython
Latest posts
  • np.linalg.eig: Eigenvalues and Eigenvectors
  • What Does %s Mean in a Python Format String?
  • How to Use np.sin() in NumPy
  • cv2.imread: Load Images with OpenCV
  • pd.read_json: Read JSON Into a DataFrame

Copyright © 2026 - AskPython. All rights reserved.