Anagram Checker: A Python Challenge

PythonPythonBeginner
Practice Now

This tutorial is from open-source community. Access the source code

Introduction

An anagram is a word or phrase formed by rearranging the letters of a different word or phrase. For example, "listen" is an anagram of "silent". In this challenge, you will be asked to write a function that checks if two strings are anagrams of each other.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL python(("`Python`")) -.-> python/BasicConceptsGroup(["`Basic Concepts`"]) python(("`Python`")) -.-> python/ControlFlowGroup(["`Control Flow`"]) python(("`Python`")) -.-> python/DataStructuresGroup(["`Data Structures`"]) python(("`Python`")) -.-> python/FunctionsGroup(["`Functions`"]) python(("`Python`")) -.-> python/ModulesandPackagesGroup(["`Modules and Packages`"]) python/BasicConceptsGroup -.-> python/comments("`Comments`") python/BasicConceptsGroup -.-> python/booleans("`Booleans`") python/ControlFlowGroup -.-> python/conditional_statements("`Conditional Statements`") python/ControlFlowGroup -.-> python/for_loops("`For Loops`") python/DataStructuresGroup -.-> python/tuples("`Tuples`") python/FunctionsGroup -.-> python/function_definition("`Function Definition`") python/ModulesandPackagesGroup -.-> python/importing_modules("`Importing Modules`") python/ModulesandPackagesGroup -.-> python/using_packages("`Using Packages`") python/ModulesandPackagesGroup -.-> python/standard_libraries("`Common Standard Libraries`") subgraph Lab Skills python/comments -.-> lab-13666{{"`Anagram Checker: A Python Challenge`"}} python/booleans -.-> lab-13666{{"`Anagram Checker: A Python Challenge`"}} python/conditional_statements -.-> lab-13666{{"`Anagram Checker: A Python Challenge`"}} python/for_loops -.-> lab-13666{{"`Anagram Checker: A Python Challenge`"}} python/tuples -.-> lab-13666{{"`Anagram Checker: A Python Challenge`"}} python/function_definition -.-> lab-13666{{"`Anagram Checker: A Python Challenge`"}} python/importing_modules -.-> lab-13666{{"`Anagram Checker: A Python Challenge`"}} python/using_packages -.-> lab-13666{{"`Anagram Checker: A Python Challenge`"}} python/standard_libraries -.-> lab-13666{{"`Anagram Checker: A Python Challenge`"}} end

String Anagram

Write a function is_anagram(s1, s2) that takes two strings as arguments and returns True if they are anagrams of each other, and False otherwise. The function should be case-insensitive, ignore spaces, punctuation, and special characters.

To solve this problem, you can follow these steps:

  1. Use str.isalnum() to filter out non-alphanumeric characters and str.lower() to transform each character to lowercase.
  2. Use collections.Counter to count the resulting characters for each string and compare the results.
from collections import Counter

def is_anagram(s1, s2):
  return Counter(
    c.lower() for c in s1 if c.isalnum()
  ) == Counter(
    c.lower() for c in s2 if c.isalnum()
  )
is_anagram('#anagram', 'Nag a ram!')  ## True

Summary

In this challenge, you have learned how to check if two strings are anagrams of each other. You have used str.isalnum() to filter out non-alphanumeric characters, str.lower() to transform each character to lowercase, and collections.Counter to count the resulting characters for each string and compare the results.

Other Python Tutorials you may like