---
title: "Write a python program to check if a string is an anagram of another string."  
description: "Write a python program to check if a string is an anagram of another string."  
author: "Steilla Mitchel"  
published: 2023-06-18  
updated: 2023-06-19  
canonical: https://www.mindstick.com/forum/158768/write-a-python-program-to-check-if-a-string-is-an-anagram-of-another-string  
category: "python"  
tags: ["python", "programs"]  
reading_time: 2 minutes  

---

# Write a python program to check if a string is an anagram of another string.

Write a [program to check](https://www.mindstick.com/forum/157542/write-a-java-program-to-check-if-a-list-of-integers-contains-only-odd-numbers) if a [string is an anagram](https://www.mindstick.com/forum/158804/write-a-rust-program-to-check-if-a-string-is-an-anagram-of-another-string) of another string.

## Replies

### Reply by Aryan Kumar

Sure, here is a Python [program](https://www.mindstick.com/blog/12337/scaling-up-your-mentorship-program) to [check if](https://www.mindstick.com/forum/12878/how-to-check-if-an-asp-dot-net-file-upload-control-has-a-file-in-jquery) a [string](https://www.mindstick.com/articles/1527/string-split-in-c-sharp) is an anagram of another string:

Python

```plaintext
def is_anagram(string1, string2):
  """
  Checks if a string is an anagram of another string.

  Args:
    string1: The first string to check.
    string2: The second string to check.

  Returns:
    True if the strings are anagrams, False otherwise.
  """

  if len(string1) != len(string2):
    return False

  character_counts = {}
  for character in string1:
    if character in character_counts:
      character_counts[character] += 1
    else:
      character_counts[character] = 1

  for character in string2:
    if character not in character_counts:
      return False
    else:
      character_counts[character] -= 1

  for count in character_counts.values():
    if count != 0:
      return False

  return True

if __name__ == "__main__":
  string1 = "anagram"
  string2 = "nagaram"
  is_anagram = is_anagram(string1, string2)
  print(is_anagram)
```

This program works by first checking if the two strings have the same length. If they do not have the same length, then the two strings cannot be anagrams.

Then, the program creates a dictionary to count the number of occurrences of each character in the first string. The program then iterates through the second string and decrements the count of each character in the dictionary. If the count of a character reaches 0, then the character is no longer present in the second string.

Finally, the program checks if all the counts in the dictionary are 0. If they are, then the two strings are anagrams. Otherwise, the two strings are not anagrams.

To run the program, you can save it as a Python file and then run it from the command line. For example, if you save the program as `is_anagram.py`, you can run it by typing the following command into the command line:

Code snippet

```plaintext
python is_anagram.py
```

This will print the boolean value of whether the two strings are anagrams to the console.

Here is an example of the output of the program:

Code snippet

```plaintext
$ python is_anagram.py
True
```

As you can see, the output of the program is True, which means that the two strings are anagrams.


---

Original Source: https://www.mindstick.com/forum/158768/write-a-python-program-to-check-if-a-string-is-an-anagram-of-another-string

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
