 Hey, what's up, guys? RudeLinL here coming back at you with some more Python. Now, remember, we've been taking a look at string functions, and today we're going to check out some, a little pair of functions, one that will test whether a string ends with something and another that will test whether a string starts with something. So let's get into Idle and we'll take a look at what we can do with these. I'm going to create a new window here and save this as, what do we want to call this, baby, baby.python. Then we'll start with our shebang line, get a class going, define our constructor, the self keyword passed in, break out of that, test whether the name is equal to main, and then we can just set our root object. Now we should be set. Okay, let's create a string variable first off. String can be, this is right. And there we go. Now, let's check out, it's got to be self.string. Can't forget that. And now let's check out these functions. Let's print, first of all, because we want to get what these functions are going to return. So let's do, let's type in self.string, and then it would be ends with or starts with. And obviously you would pass in a substring, and it would return true or false, a boolean value, whether or not your string, self-string starts with that. So if we type in this, we had a display on the green. If we save that, it's true, because this is obviously the beginning of this. So if we change it to that though, it'll tell us false, because it doesn't happen. What if we did ends with? We can keep it that. And obviously that's not going to happen, but if we change it to the exclamation point, it's true, because our string ends in an exclamation point. We could have right in here. That's true. We could have right, and that's true. What if we change it to like light? Well, that's false, because it's not verbatim. What if we change it to two capital G and an H? That's false too. Obviously we haven't changed it to right yet, but even it's a right and it has the two capital G and an H. That's still wrong, because it's set up in the string. So let's play with these. Let's see if we can recreate this ourselves. I'm going to get started with ends with first, because that one's a little bit more complex. But let's define this function here. We need to pass in our self keyword, give it a string, and let's say end can be our argument here. And now we're going to start off with a Boolean variable. And this will keep track of whether or not it actually is ending with or not. So ends with is going to be false to begin with. But then obviously we're going to have to work with this. So let's test though, let's test if the ending of the string that's passed to us, we're going to index it with the negative numbers so we can start from the end. So in this case the exclamation point. If that's equal to the end minus one, so what we're looking for, they end with that same character, we can continue to do this. We can set up end length is going to be the length of end. And we're going to add one to this because there's a little bit of an offset here. So now we can work with a for loop. We can continue to loop through this sort of thing. We can test for the range in one and then end length. Because we can't start at zero. If we check for the index of zero, we would get started with the beginning of the string and we don't want to worry about that. We want to be looking at the ending. So we're going to be starting with the range of one and then going to the end of what we're looking for. So now we can test if the string and then negative, we're going to index it with a negative value of i. And if that is equal to end with the index with a negative value of i, if they're equal then that obviously means that yeah it does end with it. So ends with can be true. If it doesn't then we have to change it to ends with is equal to false. And then we can break out of the loop. Now the reason we have to repeatedly set it to be true is because if we find it and it looks good, it has to stay true. If we continue to continue to test for it because we're moving on in the character set, if it stays what we're looking for it has to be true. But if it suddenly turns corrupt and it's not what we're looking for, it only begins with the right letters, we have to say let's false and then break out of it. So when we're done looping though and even we're outside of our if statement, we can return ends with. So whether or not it ends with it or not. Remember we're working with Boolean variables here. So we're starting off ends with is false. And if the last character of both the string and what we've passed to it is the same thing, then we can bother actually looking for it. If not, we can just like say, no, that's just plain old false. But if it is, we're going to want to take note of the length of what we're looking for. So for everything, actually for every character in what we're looking for, and we're going to start with one though, because we're going to have to index it with a negative number. If that string is equal to that character that we've looked for in the string that we've passed to it as well, then it's going to stay true. And it's going to do this for each character. So now when it gets to a character that it finds isn't the same as what we're looking for, it's going to say false and break out of it and continue to just say, okay, no, this isn't going to work. It's false. It's corrupt or whatever. But it's not going to end with it. That's all it takes. So if we run this, if we do print, we'll use the last one here. Let's set up actually self.lookfor. We can just change it to it. We can change this to lookfor self.lookfor. Now I can print out another one, which would be the one that we're our function, self.endswith. We can pass in self.string. We can spell that right and then we can pass in self.lookfor. So hopefully, oh, I get an error, range. Oh, for I in range, my bad. If we run this, these are both going to return true. The original function that we look at normally and then the one that we created, they're both going to return true because it obviously is at the end of the string is right. So if we change it to right, it'll still return true. If we change it to only the exclamation point, that'll still return true. But if we change it to light, that'll return false on both accords. Sweet. That works. No errors or nothing like that. What if we change the string up a little bit? This is wrong with the period. Wrong. We'll leave the exclamation point and it'll still be false. If we change it to a period, it's both true. If we change it to on or shlong or thong. I'm just kidding. I'm sorry guys. I don't mean to be a silly guy, but I have to make you guys laugh somehow. I'm scraping the bottom of the barrel here and we'll change it back to wrong. We run that and that runs correctly. It finds it, it does end with it and it's correct. So now let's take a look at it with starting with though. This is a pretty similar setup, but we are going to change some variables here. And we are going to look at it in new light. So let's change it to starts with. We'll change this variable to start. Ends with can become starts with. I'll change to string. I'm sorry, start. And then start. And now start. Now we don't have an offset here because we aren't looking at the end. So we can remove this plus one. And we're also going to be starting from the beginning and we're going to want to index from zero. So we can get rid of this. We can only start to look counting from the range at zero. So we don't need that parameter, that one. But we can, we don't need these negative numbers either. We can change these to start. And we can remove these negative indexes because we're going to want to stay positive this time. We can set up these variables here. And I can return starts with. So now I've got some functions here. We've got ends with and starts with and we've examined them the way that it makes sense to us. So let's print some stuff out here. Let's get some output first of all. Let's use an escape character to make it a little bit prettier. Get a tab key and we can print first off. Ends with tests. Now we can do another one of these. And we'll do starts with tests. Add a new line here and then we can change this to starts with for the regular built-in function. And we can look for like this. Second. And we're going to change this one to second as well. And obviously we have to change that function name. So let's try and play with this. Cannot catenate string. Okay, I'm going to have to, oh man I gotta, I gotta convert all these. Are you kidding me? String. String. But yeah, it was giving us that error because we obviously can't concatenate a string variable and a Boolean variable. So we're working with these here. So ends with tests. And starts with tests. It's true and false. So we've got a problem here. It does start with this in the real one. Okay, and self starts with in the false and the one that we created is giving us a problem. So let's take a look at what we got here. Oh, we started looking at the negative values. We've got to change this to zero because we want to look at the first character. In our case it's going to be the zero when we index it inside the character here. So we can run it again. Now ends with tests true and true and starts with tests true and true because it ends with wrong and starts with this. So let's change it to that. We'll change this. We'll leave this as wrong. So now ends with test is true, starts with test is false just like the way it should be. We can change it to hat. We can change this one to dog. We can run it. And both of these return false. So good. What we have done here is we've created two functions that both are similar but a little bit different because of the way they're indexing and looking at each of these character strings. But they're allowing us to be able to check whether we have a string that ends with another substring or starts with another substring. And they have boolean variables so we can work with these in conditional statements and that makes things a whole lot easier. And we've just created some really cool code here. It looks like we have a nice logic flow, it's visual, it's great. So that's all. We've done some nice debugging and yeah, this is something to be proud of. So thank you guys for watching, thank you for listening and I hope you will tune in for the next video. Bye.