 Hey, what's going on guys? Ruthenelle here coming back at you with another Python tutorial Let's get idle fired up and let's see what we can do here. I'm going to create a new script file.python Save this as Obviously file.python, but type in your shebang line if you need to Create a new class because we like object-oriented programming or OOP Create our constructor and that will happen automatically when we create an object Let's test if this is the current script. We are running and if it is we can create an object Alright now let's get started. I'm gonna create a new string variable here self.string And I'm gonna say this is a string Okay now the function that I want to introduce you guys today is called the the string Replace function and what this will do is it will replace any occurrence that it finds of a Substring inside your string and you can replace it with whatever you'd like So we can run self.string and you can use our dot selector and we can type in replace and call that function So the first parameter is going to be what it is that you're looking for and you want to replace So we'll say is and then we can change it to whatever we like. So let's say at and let's run this that at a string So it changed the first is that it found inside the this and then it changed the next one that it find as the regular is so that at a string Now there's another parameter we could put on here And that is a count or a number of times that this function will run or at least the number of times It will replace that that thing in the string. So let's try that. Let's if we added if we added one It'll only do this once That is a string So it's going to disregard the second one that it finds But if we leave this blank just the way it would it does before it'll repeat it as many times It needs to and replace every occurrence of it if we change it to two obviously it'll do all of them because uh That add a string and that's all it'll find but if we change it to like an eye a AA that Though that's as a string and this eye is being disregarded because we're only changing it two times So if we remove this that has a string and then if we do three times it'll do it all So let's try and recreate this all on our own It's going to be a little tough to work with this This let's see this maximum count number But hey, you know we'll work with it and we'll see what we can do this one might this tutorial might be a little hefty And it's good. It's definitely gonna be a little bit complex, but let's start anyway. Let's get replaced Let's start our function name. Let's call this a replace. We're gonna need some variables here We'll call itself of course Working string and then I'll replace Let's spell working right first of all replace with And then we'll do count and count is since it's a default parameter. It's an optional. We're just gonna change that to none I'm gonna make this a little bit bigger so you can see everything that we're gonna do here And we'll check it out. So the first thing we're gonna need is the length of the things that we're working with So we need working string length working String length we can just get the length with the len function of the working string that we passed to it and Then we need the replace length This is going to be the length of replace Now since we can't add Anything or we can't change things inside of a string We have to create a new string and this new string will start off being blank or an empty string And then we're gonna be able to add to it while we loop and now we're gonna create a new Incrementor variable and this is going to be I so I can be zero and Now let's get started First we're gonna try it without worrying about the count variable and then we're gonna add that in later So let's create a while loop now now We're gonna use a while loop rather than a for loop because the for loop when we're using it in conjunction with the operator If we did a for I in range Working string length if we did this because range has that static call if we change it to like Maybe 20 or something. It's going through every single one of these inside For a value of I we can't really change I and have it become whatever we want It's static it has to be any one of these every time it iterates So instead we're gonna use a while loop so we can control the flow and how our I variable is going to operate So if we change this while loop now while I is less than working string length So it's the same as if we were going through a That for loop there we can we can test if the working string with the index of I And then we can slice here I plus the replace length So we're testing if the current thing is is what we're going to be replacing or at least Something that we would want to replace what we would do Is we would set that new string and we would add on what we're replacing with And Then we can increment I will at least add it to the current Replace length so we can skip over all the characters that are inside what we were replacing now if not We still want to add to to our new string So we get the the original string and we can add on working string and then index with that I variable So every character that isn't something that we're replacing will be added to that string and now we're gonna want to At least increment I So now we can run this and let's see is there anything else we had to do here. I Think when we're done that while loop we can just return new string and Let's see if it'll work the way we want it to let's go ahead and put it up in our constructor First of all, let's print the original one. We can print out self-dot string And I'm gonna you can catnate on here a couple new lines. So it's a little clean We're gonna use your rid the the real function real built-in one and Then we can print with our function self dot replace And we're gonna pass in self dot string We want to remove the at least replace the eyes with a's and we run this That's as a strang and that's as a strang Now if we reply and we put it in the original built-in function that we wanted a maximum number We change it to like to We get that as as a string now if we did this with ours though Nothing would happen because we haven't added support for that. Yeah, just yet So we're gonna try and do that now. This is gonna be a little bit more dirty or a way to fix it It was what we should do is we should change this while loop process into a function But for the time being we don't have to worry about that. We're just gonna see what we can do But a fault so let's test If if count is equal to none first of all Then we can run our original function or at least what we have here This this while loop and copy and paste that in and we can select all of it And if you're an idol you can use the control and then the the ending brace key the ending bracket key So now we have that all set and if count is not equal to none You can add an L statement in here else We can do something else first you want to test that for the type of count. So if the type of count is Not an Integer We're gonna want to change that so we can set counts to the integer form of count So now let's get to work. Let's gonna set a counter Counter is going to be the number that we're going to keep repeating up until we get to the the specified count or the specified number of times they want this done so we can set counter to be zero and then we can Create our while loop that we had right up at the top here I'm just gonna paste that in and now if we check all through that We're gonna what we're gonna want to do is replace Or at least down here when we if we find something that we're replacing We're gonna want to increase the counter variable obviously because we're only gonna do it so many times And if not we're still gonna want this and then we can break out of here But first we need to test if counter is actually less than count if it is We can test for this other if statement here Along with the else so let's copy and cut those in Okay, and now we're gonna want to include this l statement Alongside the top most if statement here too because even if we're finished replacing things We still want to get the entire string. So we're gonna want to copy and paste this and Put one right at the end of Our top if statements so we can cut this here and that should be okay And now when we're done we've broken out of everything we can return that new string So we're sort of repeating this process again But making up for the fact that we do have a count variable now or at least we're limiting how many times we can add Onto this really out. We're limiting how many times we can replace something. So let's try and call our function now That's as a string and that's as a string it works in both cases now because we've added for the fact for that count variable We're going to keep increasing the counter variable every time we replace something and only if it's less than count will replace something But we're still going to want to add on the rest of the string no matter what so This is why we need all these l statements inside here It's a little bit complex and a little bit hard to follow But you do have to keep track of all the variables that you're looking at We still have to keep track of count you have to keep track of your your looping variable Whether it's I and the incrementer is working with you have counter whether that's less than count and that sort of thing So it's easy to get lost in this code But it still solves that same problem as a replace function and we've created it and turned it into our own So thank you guys for watching. I hope you enjoyed this I know this one is a little bit more in-depth and crazy, but you know there's a lot we can do with it Let's let's experiment just a little bit more but let's a little bit more before I let you guys go Let's turn this into See we replace as many times as we want that as as a string we can change the is to an at If we run this it changes that at a string We can only do this once if we want to We can even to put in something that isn't found in there it it It'll replace it with the they're the same old regular string So look what you've done guys. This is a pretty pretty handy function And this is how it works on the back end. So I hope you guys enjoyed this I know it was a bit more lengthy and there's a lot to it, but hey the replace function is definitely helpful So thank you guys for watching and be cool if you could like the video maybe leave me 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. Bye