 Hey, what's going on guys? Rudinal coming back at you with some more Python tutorials. Let's get idle fired up And let's see what we can do here today. I'm gonna create a new script Call this file.python I'm gonna get my shabang line started up. Create a new class as always Define our constructor Passing self keyword Just like you would do with any function inside of a class and now we can test if this is the current script that we are running Create an object of our new class and we are set to go So now back inside of our class back inside of our constructor. Let's define a string So self.string and I'm gonna call mine this string It's not gonna call it this but the value is going to be this string and I'm having new line character here has New line characters actually I'm gonna put a new line character here has new line characters New line again in it Then we'll and we'll call it good with that So the string that you got. I'm sorry the function that you guys can probably guess we're gonna be taking a look at today It's a self.string and then it's called split lines And what it will do is it will turn every single law every single thing up until your new line character Into an element inside of an array. So if we let's do it our output is first of all here. Let's do print Self.string now we can add on new line Some new lines here and we can print on Self.string and then we can run our new function here split lines now if we run this We get this string has new lines new line characters in let's remove that s here This string has new line characters in it and when you look at our function It has this string has new lines characters in it And this is because every single new line has been has been taken out And we have been able to report everything else inside the string inside the element So let's try to recreate this all on our own But before we do that, let's I want to show you guys an interesting Interesting perimeter you can pass to this it has a boolean variable if we check out a control backslash Oh, it looks like we can't see but if we check it out inside the Inside the interactive shell if we do string dot split lines We can do is pass in here and we can use control backslash to see what do we have here keep ends Actually will let you keep the new line character that you've removed So this is a boolean variable you can pass in true and let's do that right here Let's do if we do false. That's a default value. It doesn't keep anything But if we pass in true it will keep these backslash ends that we have here or the escape characters It'll allow you to create a new line. So let's try and recreate this all on our own now Let's say I was to find a new function. I'm gonna call mine split lines I'm gonna have an underscore because I like those and I will pass in self as always We'll pass in the string and then I'm gonna call mine keeping characters because I think that's a good name If especially if we're doing boolean tests and we can set this to false by default We can get started. So now we're gonna need is the length of the string because we're gonna be looping through it with numbers So string length equals length of string And now we're gonna need what denotes a new line. So new line Denoter and this is going to be our string that has an escape sequence backslash n And now we can do the new line length We can set that to be the length of backslash n. I'm sorry. No the new lines in order And now we'll need a new string So we'll be able to keep track of what we're adding what we aren't adding we this can be empty for now And then we're gonna need the array that we're gonna be returning Now we think we can begin to loop through off through our string here. We can do for eye and range string length Let's get our code block started and now we can we can test If the string that we're splitting from the index I up until I plus the new line length If that's equal to a new line character In our case our new line denoter is what our variable is called. What we're gonna do here Is we're going to do array plus equals and then array form of new string and then we can reset new string Back to nothing Now this might look like we don't have anything a new string yet But this is where our L statement is going to come in what we can do here is if we find anything that isn't a new line character What we'll do is we can do new string Plus equals that string character So we can pass an I when we index our string here and now when we break out of our loop what we can do is Add the remaining pieces of the new string into our array because it isn't going to get all of it So what we'll do is we'll add in new string just like we did up in here in our conditional statement And then we can return the array finally So if we return array Then it looks like for now our function is complete So let's go and put ours up at the top here and let's try and run this and see how it looks We can do self dot split lines because that's the name of our function pass in self dot string And then let's give it a go if we run this we get this string has new line characters and all of which are our strings inside of an array and it parsed it correctly it understood everything that we wanted it to But we what we still have to work with this new argument here Whether or not we want to keep characters if we pass this in as true Into the top function we try and pass this in as true into our function The top function will get it right correctly because obviously it's already programmed to do that But ours doesn't have support for it yet So let's try and add it in if we go back into our function We're going to be taking a look at our loop here And we're going to test if the string has the if the string has that new line denoted inside of it What we're going to do is we're going to test whether or not we're keeping characters and Remember since this is evaluated to true if we pass it in it will we can just you have the name of the variable be our condition and What we can do then is new string and we can add the new string denoter on here I'm sorry the new line denoter and then we don't have to worry about anything else if there isn't Because this way we'll have this new line or that backslash and add it on here So let's try this We can run our function now and this does this has the exact same output as the function that we have up at the top here the built-in one So there are a lot of interesting things you can do with this and one of the things that we try to make our function a little Bit more versatile and that you can change what this new line denoter is you can change what will represent a new line You might not ever have to do this But in case you ever want a function that has this sort of this sort of process or this sort of method and Well like you know the action that actually happens when you run this function if you want to be able to customize that and maybe change What represents a new line you can do this by simply changing the variable here or you could just pass it in as an argument Which would be even better So yeah, there are a lot of interesting things you can do with this But it's a short little function here and it's understandable because I think it's it's pretty simple You loop through the string you test whether or not you're keeping things or not if you are you can just add On what you would have lost to begin with and that sort of thing But hey, thank you guys for watching. I hope you enjoyed this. I hope you can understand it It's comprehensible and that sort of thing, but please like the video if you do it if you did enjoy it It'd be cool if you could leave me a comment. Let me know what you think and maybe subscribe I don't know. It's just an idea But if you want to see some more things for me, it's generally a good idea and hey, I will see you in the next tutorial Bye