Sunday, 12 January 2025

Day 88: Python Program to Check whether two Strings are Anagrams

 


def are_anagrams(str1, str2):

    str1 = str1.replace(" ", "").lower()

    str2 = str2.replace(" ", "").lower()

    return sorted(str1) == sorted(str2)

string1 = input("Enter the first string: ")

string2 = input("Enter the second string: ")

if are_anagrams(string1, string2):

    print(f'"{string1}" and "{string2}" are anagrams.')

else:

    print(f'"{string1}" and "{string2}" are not anagrams.')

#source code --> clcoding.com 


Code Explanation:

1. Function Definition: are_anagrams(str1, str2)
def are_anagrams(str1, str2):
This defines a function called are_anagrams, which takes two arguments: str1 and str2. These are the two strings that we will compare to check if they are anagrams.

2. Removing Spaces and Converting to Lowercase
    str1 = str1.replace(" ", "").lower()
    str2 = str2.replace(" ", "").lower()
Removing Spaces: The replace(" ", "") method removes any spaces from the strings. This ensures that spaces do not affect the comparison. For example, "listen " and "silent " will both become "listen" and "silent" without spaces.
Convert to Lowercase: The lower() method converts the entire string to lowercase. This ensures that the comparison is case-insensitive, meaning "Listen" and "listen" are treated as the same string.

3. Sort and Compare the Strings
    return sorted(str1) == sorted(str2)
Sorting the Strings: The sorted() function takes a string and returns a sorted list of characters. For example, "listen" becomes ['e', 'i', 'l', 'n', 's', 't'].

Comparing Sorted Strings: The function checks if the sorted versions of str1 and str2 are the same. If they are the same, it means the strings are anagrams because they contain the same characters in the same frequency.

Example:
"listen" → ['e', 'i', 'l', 'n', 's', 't']
"silent" → ['e', 'i', 'l', 'n', 's', 't']
Since both sorted lists are equal, "listen" and "silent" are anagrams.

4. Input for the First String
string1 = input("Enter the first string: ")
This line prompts the user to enter the first string and stores it in the variable string1.

5. Input for the Second String
string2 = input("Enter the second string: ")
Similarly, this line prompts the user to enter the second string and stores it in the variable string2.

6. Check if the Strings are Anagrams
if are_anagrams(string1, string2):
    print(f'"{string1}" and "{string2}" are anagrams.')
else:
    print(f'"{string1}" and "{string2}" are not anagrams.')
Calling the are_anagrams Function: The if statement calls the are_anagrams function with the two input strings (string1 and string2) and checks the result.
If the function returns True (meaning the strings are anagrams), it prints that the strings are anagrams.
If the function returns False (meaning the strings are not anagrams), it prints that the strings are not anagrams.

0 Comments:

Post a Comment

Popular Posts

Categories

100 Python Programs for Beginner (87) AI (35) Android (24) AngularJS (1) Assembly Language (2) aws (17) Azure (7) BI (10) book (4) Books (179) C (77) C# (12) C++ (83) Course (67) Coursera (231) Cybersecurity (24) Data Analytics (1) data management (11) Data Science (132) Data Strucures (8) Deep Learning (21) Django (14) Downloads (3) edx (2) Engineering (14) Euron (14) Excel (13) Factorial (1) Finance (6) flask (3) flutter (1) FPL (17) Generative AI (4) Google (34) Hadoop (3) HTML Quiz (1) HTML&CSS (47) IBM (30) IoT (1) IS (25) Java (93) Java quiz (1) Leet Code (4) Machine Learning (62) Meta (22) MICHIGAN (5) microsoft (4) Nvidia (4) Pandas (4) PHP (20) Projects (29) pyth (1) Python (958) Python Coding Challenge (398) Python Quiz (54) Python Tips (3) Questions (2) R (70) React (6) Scripting (1) security (3) Selenium Webdriver (4) Software (17) SQL (42) UX Research (1) web application (8) Web development (4) web scraping (2)

Followers

Person climbing a staircase. Learn Data Science from Scratch: online program with 21 courses