 Hey, what's going on guys root of the knell here coming back at you with another python tutorial. Let's get idle started up And let's see what we can do here Let's save this as File.python Get our shebang line started environment Python create a new class here Oh random random dash Define a constructor Make sure you spell everything right that tends to be a problem of mine see case and point And now let's test here If the name is equal to main All right, let's set up a root object And now we can start working. I'm gonna create a new string variable I'm gonna call this self dot string and it's gonna be a this is a joke because that's a good string So I'm just gonna print that out self dot string and then we'll cut on some new line characters Now I can print this and again. This is a joke and those new line characters that we want it Okay, so now let's check out what we're gonna be looking at in this video This is I'm going to introduce a function to you guys today called split And what it does is when you use the dot selector with your string variable You will be able to split your your string and what it'll do is if we if we run it here It'll look for by default any white space characters and then every time it finds a white space character It will it will remove that and then add whatever it just found from that last position and add it to an array So it splits up your string into an array or a list So let's run this and I'll show you what I mean If you see this is a joke right up top down here We have a list called this is a joke. So for every space character then found it It at least it didn't it didn't bother with them and it just created a new a new list Filled of everything else. That's inside here So now you can think of it that the white space is our splitter And you can actually pass in some other things that we'd want to split with So if we just wanted to pass in that space character just like we did before It's gonna get that same output for but if we change it to a space character with an I after it It's only gonna get up to here without breaking this next bit. So let's try and run this here Let's see we get we get this and then it removes that space I and we have the rest of the string so joke So we can pass in what we want to use to split it up We can also pass in a number for how many times we want to split something we can pass in We can leave it a space again. We can pass in maybe two So now we get this is and then a joke is one whole string here We could pass it in maybe one and then we'll get this and then is a joke for the rest here Now we can pass in zero and we'll still get return this entire string that we had right up here except It's inside of an array So as you can see this is kind of a complex function, but anyway, we're gonna try and recreate this all on our own so I'm gonna get rid of all these parameters here and We'll create a new function I'm gonna call mine splits and we'll pass in self as always We're going to use a string to split variable obviously We need that and now we can type in a splitter and remember that's an optional parameter That's gonna be set to be none and then we can have count Which is how many times we're gonna do that and I'll remember that's and that's a default parameter again It's optional, so we're gonna set that to be none. Let's get our code block started And now we can test right off that if splitter is equal to none and we have to do something about that Splitter is equal to none We can pass in split equals. Oh, sorry splitter Remember we have our variable name to be correct here string Dot white space. So that's what we're gonna be doing here We're gonna be using the string module. So we have to remember to import that right up here At the top of our program and then we can use the white space That variable and what that is is if we import string in our interactive shell We can do string dot white space and check it out It's all of these space characters tabs new lines all these things here and then return Character feeds that sort of thing and even space characters. So now let's try and work with this here What we're gonna need inside of our function is obviously a new string because we can't modify the one that's passed So we have to recreate our own and we're going to need an array or a list that we're gonna be able to pass back once we finish Processing so let's do something here. Let's let's get started with the bare basics. First of all, we can test If actually we can begin to loop anyway, that's a good start We can do for character in string to split And now we're gonna begin to loop through it and then we can test if the character With this the current thing that we're looking at is inside that splitter thing. So if it's white space We can do is We can add to the array What we've currently found here new string and We haven't added anything to the string yet But that's gonna come up in the L statement or if it's or if what we found is not in that splitter list And then we can do a new string and we can reset it back to normal or at least back to nothing So when we have our L statement if we don't find anything we should add that to new string So it's something that we're going to be able to process and understand so new string plus equals character and Now that should be easy enough When we're done here we can and we're done looping anyway We can do array plus equals new string So when we're done looping we still have some characters left over But we're gonna want to add those back in new string and remember it has to be inside of this array Side of an array block here. So it it understands it as a list and then we can return array So now if we go back up to our constructor, we can try this all in our own print Self-dot splits we can pass in self-dot string And that's all the parameters that we've got set up now So let's try and run this we get this is a joke from the first the the regular function And then from our function we get that same thing. This is a joke So it's understanding our white space thing so far, but let's try and pass in some some things of our own Let's do this one here Can pass in a space character for the top one we can pass in a space character for the bottom one We get this is a joke the way we should and this is a joke the way we should but now let's try it with our iExample if we add an i right after that space The top joke understand the top the top function anyway understands it correctly It removes that space i and just gets the rest of the string But our function doesn't do it correctly and this is because we haven't added support for it yet So let's go ahead and try that and we'll see what we can do We can test right up here Let me make it start it. Let's close unity first of all. I don't know why that's doing its thing Unity go away. Thank you All right, so now we can go up here inside of before we start looping anyway, we can test If our splitter is equal to the string.whitespace So if it's the whitespace by default the way it should be we can loop the way we had beforehand We can we can pass this in here Oh Let's copy and paste that code right into our new if statement and we can tab this out Make sure everything is indented the way it should be and now it'll only now it'll only run this if our string has white space in it So if we run it one more time It's not going to return anything inside of that array because we haven't we haven't accommodated for that yet So let's add an L statement out to our our uppermost if statement our conditional statement here So if the splitter is not equal to the string.whitespace and it's something we have to be worrying about ourself So what we can do here is Let me check out my notes on that. I've got on the side We can set up a string length and this variable is going to Obviously have the length of the string that we're splitting So we get this and now we're going to need the splitter length and that's going to be the length of the splitter variable okay Now we're going to want to increment or loop through the string to split once more But we're going to be using it with a while loop rather than a for loop because we want to be able to Index or change wherever we're we're incrementing So we're going to use a while loop but before we do that we have to set up our incrementer So I can equal zero and now we can do while I is less than the string length So we're going to loop through it once more And what we can do is we can test if the string to split And we're going to slice here I To I plus splitter length And if that's equal to splitter or what we're going to use to to split with it What we can do here is we can do what we did up top array can plus an equal then an array form of the new string and New string can go back to being nothing So we can reset for the next time we find something else and we can add on to I splitter length So we can skip over everything that we found inside the splitter and then of course we'll need an else statement So we can add to the new string if we find something that isn't in there And what we can do is a new string plus equals string to split and Then we'll index the current character with I Our incrementer and now what we'll do is we'll add one to I so we can keep going forward and that should be set for now So now if we pass in our our I variable here This will run the way that we want it to if we pass in the the space in the I just the way We had done it beforehand it'll run just the way we want it to we get this it skips over the space in the I characters and then it gets the rest of the string is a joke It does it exactly the way it does in the original function and our function does it correctly as well So we've had a little more we've added a lot more code here But we still have to make up for that count variable or that maximum number of times we can do this So let's let's add that in now what we're going to be doing here is changing some new variables around though What we can do up at the top is a Comment out this new string equals this thing here And we're going to reset new string to be the string that we've passed to it And now this is only set up for defaults. So if they happen to pass in a zero for the number of times They want this done. We're going to want to return to them The regular string so if we see this up in the the function and regularly it returns this entire string just passed inside of an array So we have to be able to do this inside our function, too So what we're going to do is set new string to be equal to the string that we've passed to it by default And then if we process it will change it back to nothing like beforehand So new string can be equal to string to split and Now we can start testing Let's test in in front of everything if count is equal to none We can start up a code block and everything that we've done already Can be put inside that conditional statement Everything up until that last bit of code outside of anything else. So if we copy this in Remember everything should be an indented correctly so what we'll want to do is Indent these here and that should get us exactly what we need to do But remember we do need to set new string to be equal to zero or at least new string to be an empty string So new string can equal nothing and Now we have our good function. So now we're going to want to test for that else scenario though What if it's what if we haven't? What if we have passed in a count variable or how many times that we actually want to do this What we're going to do is test again But first we need to be able to loop through it and we're going to do this with our counter variable And what counter variable will do is it's going to be an integer and it'll keep testing whether or not Counter is less than count or the number of times we can do this So we have to be able to keep track of how many times that we've actually That we split the string up. So counter can equal zero and what we'll do is we'll do a while loop while counter is less than count We can create a new code block here And now we're going to reset our new string variable back to an empty string like we had done right up at the top and Now we can get the string length once more just like we did earlier string length can equal length of string to split and Splitter length because we're going to be doing that same sort of process that we did up top But now we're accommodating for that count variable. So splitter length can equal the length of splitter And we should be set now. We're going to use our I variable again. I can equal zero and while I is less than string length Remember to spell your variables right? Well, I is less than spring length What we're going to do is test if what we found is equal to what we're looking for once again. So string to split index with I to Splitter length and if that's equal to the splitter We can now we're going to get into something different though What we're going to do is we're going to test if counter is less than count That's kind of the same condition that we have up here in this while loop So we would normally be able to get away with it But because we're inside of another while loop it's going to keep going through this condition rather than that one So even if we change the counter variable We have to be able to test for it and break out of the loop if we go over that if we exceed it so we can test if Counter is less than count and what we'll do here is add to the array first of all array plus equals New string and the current one that we found and then new string can go back to being nothing and Then I will increment by the splitter length so we can skip over the splitter and then counter We'll add once more to it because we finally we finally replace something here So when our outside of this if this if statement though we can test if if we're not Less than count if we've done this too many times We should break out of the current while loop and that's this one here So that way we'll be on we'll continue with this condition and we will not be able to get anything else So now that we're done that we can break out of those two code blocks this else statement here And this another if statement and we're going to add an else statement to that if statement So now we can set up new string Get my cursor out of the way plus equals string to split index with I so we're adding all the characters that we found and We're going to increment I so we continue to loop Now we can break out of this extra while loop that we've got up at the top here And because we may or may not get to the entire string get through the entire string Anyway, what we're gonna have to do is continue to loop through it So we can use our for loop here for K in range We're going to start counting and we're going to continue right where we left off with that I variable since I was incrementing beforehand Now we're going to use K to increment, but we're going to start with I so I can go to a string length And then what we'll do here is Add on to the new string new string plus equals string to split And we're going to want to index with K because that's our new counter variable Now that we're done all that we should be set to go We've got our and then a ray will add up with anything else that we haven't we haven't added inside of our loop and If we pass in a zero for the count though, it'll still remember that the new string has been set to what it was beforehand So here is our rather lengthy function Let's try and experiment with a little bit more if we run this the way it should now We get exactly what we had what we'd hope for this and then sudden says a joke or such joke whatever now we can Change this to an I Just for potty words and giggles and we get th s in that space character as in a joke So we get the same output as we would with a regular function So let's try it without any any arguments here We get the same thing that we hope for now if we pass in that Space variable at least a string of it only a space we get the same output But now let's limit how many times we're gonna do this. Let's do this only only twice We run this we get this is and then a joke we get the same output with both functions So that means we're we've done a good job here. We can change this to one. How about that? And we get that same that exact same thing. What if we added that I right out to our space here? We get this and then such a joke such a joke just what we did beforehand now Let's change these to zero so we're not gonna go through it whatsoever We run this and we get this is a joke and this is a joke that both functions have the same output because we've considered Everything inside of our own our own handmade function or homemade function whatever you want to call it So now you can see though when we have this L statement for the count variable So if we have passed in a number or how many times we want to do this We're only considering If it's not Whitespace characters because up here in the first time we checked for the count variable if we haven't supplied that in what we're Doing is we're testing whether the splitter is the default one string dot whitespace But we know in the next else statement that it's not going to be that case because we haven't even We are passing in already what the splitter should become because if we just pass in a number without the splitter The original function will have a hissy fit so ours should have a hissy fit of a hissy fit, too We get self dot string dot split it it kind of expects what you should be searching for at first So let's just pass in the space character again And if we do this in our function It'll return the way that it should So there you go guys I mean this is a really really lengthy function and it's definitely incredibly complex So it's understandable if you guys don't understand this the first time and I don't think I did that great of a job explaining it But you have to be able to look at the variables and knowing where you are in your position while you're looping you have to know Whether or not it's been added to the final array that we're going to return that sort of thing So if you experiment with I with this idea and you try and build it on your own without my help You might have better luck with this But you can see we're doing a whole lot of testing and especially a whole lot of logic because we have so many code blocks here We're testing for so many things. We're breaking in and out of loops and that sort of thing So we have a lot going on here, but but hey We've done a good job with what we have what we finished so far So I want to thank you guys for watching. Thank you for listening. It'd 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 want to do, but after watching this video You guys should definitely give yourself a pat on the back. So thanks again, and I'll see you in the next tutorial. Bye