 Hey, what's up guys Rudlinal here coming back at you with another Python tutorial. Let's fire up idle and not waste any time here Let's get a control and bring up a new window save this as a grandfather Let's least that's what I'm gonna call it as grandfather and we'll get our shebang line started Set up our skeleton for the program. I don't think there has ever been a time in my life Where I've actually typed all of that out correctly But anyway, let's get started. Let's get it created Let's create a new a new variable here This is gonna be another string because we're gonna be looking with string functions in this tutorial and we'll call this Stealth dot string and we can change it to a this is a string Isn't it? It is right and that's our string variable. All right So now what we're gonna be looking for today is at least what we're gonna be looking at anyway Is the a string dot count function now the string dot count function allows you to Get the number of how many times Another substring or a different string occurs within that string So we call that the substring and the string that we're searching for is obviously just to note it as a string So in self dot string dot counts where we pass in like is is is the substring and it's gonna count How many times we see that and it is case sensitive So we're gonna find once in this word and then we have is Then we have it we think we have it here for isn't it but see that I is capital So it's not gonna recognize that then we have that is right here So we only have three occurrences of is if we actually print this out Let's see what we got here. We got three. Okay, cool. So let's try and recreate this on our own Let's get a new function here. We're gonna call this What is it was count? That's right count self and then we need the string and then we need We'll call this the the haystack And we'll look for the needle needle in the haystack So what we'll do here is we're going to need the length of each. So haystack Length Can be the length of a stack and now the needle Can obviously be the needle length and that sort of thing. So now that we've got all the information We're gonna look for we're gonna need to keep track of what we find of how many times we found it So let's try a total total can be our integer that's gonna keep track of how many times we find this So we'll loop through it and we'll count through the through these through these characters here for I in range So while we're going through the haystack length, so we're gonna loop through the original thing that we found here At least what we're searching for and then we can test if the haystack one Actually I if what we found is equal to what we're looking for also known as the needle the needle That's the start of it anyway So if haystack I what we've what the current thing that we're looking at is equal to the starting of the needle We are under the impression that this is going to be another This is going to be a kind of currents of that needle. So we have to check for it So what we're going to do here is we can set end can be I plus and then look for length So needle length So this is the current position and then we add on the length of what we're looking for So if we get a substring of that though we can do if Let's see haystack And then we're gonna use our slicing here when you use I to the end and if that's equal to the needle or what we're looking for Then we can create a new thing here and just a new code block and Increment the total and then we're done looping we can return the total to see how many we've got So if we do It looks like that's all done now if we can add it up in our constructor here. We can print self count We can pass in self string And we can look for is If we run this we get three Now if we uncomment the top one it looks like they're both gonna get three. So what if we look for um T? Five of them. Okay, that works. What if we looked for a space? What if we look for a single space character? Nice we get eight. What if we look for something that isn't in there? What if we look for a Z? Well, both your return zero because they can't find it. So there you go guys This is the function it all it does or at least this is one way of building that sort of thing The way I've set it up. So we have the number or at least we can loop through it We can count through the the current thing that we're looking through and then if we find something or at least we find a character It's the same as the start of what we're looking for then we're gonna be under the impression that okay This might be what we're actually looking for so what we're gonna do is we're gonna take that current position of our loop and Add the needle's length to it. So then we're actually slicing it We can start from what we were we were we're beginning and then we can add on that Piece that could possibly very well be the needle So we're checking to make sure it is the needle and if it is we can add to it because we found it So that's the way that works Hope that it's understandable. I hope that's easy to figure out and that sort of thing But looks like that's all that we've got to cover today. So, uh, thank you guys for watching Thank you for listening. It'd be cool if you could like the video. Maybe leave a comment Let me know what you think and maybe subscribe I don't know it's whatever you'd like to do, but hey, thanks again guys, and I'll see you in the next tutorial