How to Check if Two Strings are Anagram with Python Code 🔥

Learn to build an anagram checker in Python using lower() and sorted(). The program converts words to lowercase, compares their lengths, sorts the letters, and checks if they match. Examples include "race" and "care", and "races" and "cares".
Chapters
All right, hey everyone. Um, today I'll be covering 2 things. I'll be covering the lower function and the sorted function in Python, and after that we're going to go into creating an example program out of these 2 that kind of uses these 2 lower function and sorted function as well, and the sorted function. So it's going to be an anagram, and you'll see later. But, uh, for now let's cover what lower function does. So the lower function, or the lower method here, converts all uppercase characters in a given string or word, you know, into lowercase characters and then returns it for you. So for example, let's say, you know, let's just ask the user to input any string, for example, right? So this input function here lets the user asks you, hey, enter some certain amount of text, or enter text basically, or do something. So you'll see here, so enter— we'll give the instructions to the user of this program. So enter some words with maybe with uppercases. For example, right? Um, it doesn't have to be like fully uppercase all of it, but it can be, you know, some uppercase, some lowercase. And then after that, we just want to print out the text that the, the person entered. So we'll do print and we'll do, you entered— we're trying to make it a little more, you know, conversational, um, so the user can enjoy the experience here. So, uh, you entered text, for example, right? Um, so it'll tell them, hey, you enter this text. So now we want to convert it to lowercase. So what do we do? We pass in— so let's do lowercase. And on the lowercase, we just give it the lower function. So we'll do text, which is the text the user entered. Dot, then this is where we put in the function lower. It's just that simple in Python. And that's really it. So lowercase text lower. So that's it. And now we just want to print it to our console. I'm going to say, um, converted string. So this is the string that was been— that has been converted to lowercase. Lowercase. And let's print that and see what, what we get, right? And we'll save it first, so Ctrl+S will save the file, and then we run it. And you'll see here it asks us in the terminal, hey, enter some words with uppercases. So here we could do, we are awesome. I just put some random words in there that have some extra caps in them, in And then we enter, and then it's going to tell us, hey, you entered we are awesome. And convert— converted string is now we are awesome. Just look at that. That's now we have it all in lowercase. So that's really what it does. And also another example, we can also do upper. And all you have to do is at the lower function, just pass in upper. And save again, Ctrl+S, or you can come up here, click File, and then click Save. It's just going to save the screen and then run it. And it's going to ask you here again, um, enter some words with uppercases. Well, this time with, with lowercases. So we can do, I don't know, we want to eat, for example, and then enter and see. So it says you entered we want to eat, and it will say— oh, by the way, let's just put colon here so that way we know which one We actually ran. So we'll do again, File, Run. Uh, we want to eat. I know we're just repeating here. This is— so we'll see what— this is what we entered and this is the output. It's now in caps, all caps here. So that's just basic functioning of upper and lower. Now let's go to something else. So also, if you want to clear this screen right here, you have a lot of stuff in your, in your command line, all you do is cls. Or clear, that clears everything out and then you have a fresh new console with you. So again, cls. Let's come to sorted. So how do you use the sorted function? I guess let me start first with, you know, the Python sorted function returns a sorted list from an object, iterable object. And this is, for example, let's say you have a We have here a list, right? And let's call— maybe the list is— let's put it in a variable x. And let's have stuff in the list, of course. Let's just put some random numbers in there: 5, 7, 1, 3, 6, 9, 10. I don't know. Yeah, you can— it can go all the way up to whatever. So, so x. And you can see this is out of order, right? And let's say, for example, we want to put it in a good order and we have it as 1, 3, 5, 7, you know, 1, 3, 5, 6, 7, 9, and 10, right? So it's very simple in Python. All you have to do is just call sorted on this x. So we can do print sorted, and this function we pass in what we want to sort. So we want to sort x, so you do print sorted. So sort this x for me here, then click save. And then run Python file. And— oh, sorry, my bad. I still have all this. We don't need that anymore. So let's save it again. And then let's run it again. Oh, let's clear this up here first. Let's just get out of here. And let's clear. Run it. And you can see here it arranged the list for us. It ordered it— 1, 3, 5, 7, 9, 10. OK, cool. So that's cool. Uh, we— what else can we do with it? We can also reverse this string. We can sort it and reverse it if you want to reverse it. So all we have to do here is pass in here, because it takes— it takes, uh, the syntax, it takes different, um, kind of parameters within the sorted function. So we could do reverse in there, and all we have to do is reverse is equals to true. We just have to tell it, hey, please reverse this. And then after that, you just click— you have to save it first and then run. Okay, so now you see we have 1, 2, 3, 1, 3, 5, 6, 7, 9, and 10. But when we do reverse, it sorts it but then it reverses it. Okay, so that's it in simple. And now we're going to switch over to the project that we're going to create, um, which is the anagram project. Um, stay tuned. 2 words are anagrams, and by anagrams, I just mean that For example, race and care here, if you rearrange care, it's going to form the word race. So we're just going to write a small Python program to do that. So we'll start by converting both strings to lowercase. So we have to convert both to lowercase so that it's easy for us to rearrange them and figure out if they actually form the other word. So word1, we'll just do word1.lower. So that's very easy in Python, and that's going to be lowercase. So again, we do word2, and we'll do word2.lower. So that should be lowercase now. And then the next step is we check if the length if the length is the same, if the length of both sentences— if length of the same words are the same. So we'll do if length word 1— the length of word 1 is equal to— so this is the double equal signs in Python means it's equal to. So it's not like the regular— if you, if you do one equal sign, that just means you're assigning a variable to specific, a specific word, a specific input to that variable. I hope I captured that correctly. But the next one is sort the strings, or sort the words in this case. So I'm just not trying to use a lot of coding words to make it confusing, but so we'll keep the words. So you sort the words. So we'll do sorted, and this is the one that's going to be sorting, shuffling the words. Um, sort word. So we'll do sort word 1 and word 1. And this as well is very simple in Python. All we have to do is just type in sorted, and then we pass in the the word that we need to sort, which is word1. And we repeat the same thing, um, with the second one as well. So we need to sort word2. sorted, and we pass in word2. All right, so both of them should be sorted now. And then the next thing is we instantiate, um, an if loop. So if sorted character in word. So char should be your character. If sorted character are same, do something, right? So we will tell it if sorted word 1 is equal to sorted word 2. So here we're just checking to make sure that Once we sort both, if they are the same, then it's an anagram. So here we print and we'll say word1, and the plus sign here, concatenation, is basically we're concatenating a variable that's already been instantiated with another string. So and plus word 2, and then we add another class and then r-anagram. So that will be the output. So that's the first if statement, and then we do the else. So if both are the same, equal, then it's an anagram. And if it's not equal, then we'll print. We have to give it something that— we have to give it an action for it to do. So word1 again plus space and then concatenate again word2 concatenate are not anagram. So if both are not equal, then it's an anagram. So it's basically telling it, you know, if, if this, do this. If not this, then, you know, it's this. So, and then we remember we also had the first if. So if length 1, word 1 is equal to word 2. So that, so that was the first one at the top. So this first else here, if, if loop up here, we're checking if the 2 words are equal in the first place. And that's just the first screening. If they're not equal in length, they cannot be reshuffled to form the same sentence— the same word, sorry. So that's the first check. And then if it's that, then continue to do this. Then if both of them are not equal, then do something, right? So we'll do print, and we'll still use the same as not_anagram, because if they're not equal, they cannot be an anagram. So we can just copy that and paste it here. For the same thing to happen. So, and then we're good to go here. So all we have to do is run the file and race and care are anagram. All right, let's give it a test here. Let's do races. And so when we do races, that means they're not equal in length, right? So races is going to be longer than care. So what we do here, it's gonna, it's gonna change it up and it will throw an error— or not an error really, it's just gonna say it's not an anagram. So it will, it will trigger this else statement here which will say, hey, they're not equal. So this one, if they're equal, it will do this, but since they're not equal, it will do this down here. And let's run just so you can see. We have to save it first, Ctrl+S to save it, and then go up here and run the Python file, and it will say races and care are not anagram. And if we do cares, let's see what hap— what happens to that, right? Races and cares, they should be same in length. And we'll do save again, Ctrl+S, that's on Windows, and then run. And races and care are anagram. So satisfies our, our plan here. So yeah, that's it in short. That just shows you how Python is very easy to learn. So if you enjoy this type of videos, follow me and, um, let's learn together.
More from Tony

Summarize Videos and Research Companies with CrewAI + Gemini Models [Quickstart Guide]

How to Make Your AI Agents More Reliable with CrewAI Task Guardrails (Step-by-Step Tutorial)








