 In the last tutorial, we figured out how to identify the column position in a line of text or in a string. And in this tutorial, we're going to slice up that line based on a starting position and an ending position. The way we're going to do that is replicate the functionality that's over here in Scratch and show you how to do it in Python. And then at the end of the tutorial, I'm going to show you how to do it in just one line. That's right. One line. All right. Let's get stuck into it. Hello, world. It's the Serving Scratcher here. Teach a server programmer. And on this channel, I help curious learners just like you along on your learning journeys. Our current journey here is transitioning from Scratch into Python so that we can equip you with some mad text programming skills. We're creating a spelling game and we first created the game in Scratch and now we're creating it in Python. Okay. Straight up, we're going to create a new function definition here. So we're going to call it slice line between, we're going to feed it a line, a start index and an end index. That is quite hard to say. Just like we did over here in Scratch where we're feeding it a line, a start index and end index. There we go. I did it. Second time round. Don't forget to put that call on there at the end of your function definition. Okay. Let's start to create some of our local variables here. We're going to have text output and that's going to be a local variable. I'm not going to worry about the underscore because that's a bit cumbersome. We're going to initialize that as an empty string. You can tell that because I've got two double quotes here and there's nothing inside of it at the moment. Cool. Let's initialize our next variable. Instead of underscore I, I'm just going to call this one I and we're going to assign it to the start index and the start index is whatever we pass into it and that's going to be an int, okay, an integer. The next thing that we need to do is create this repeat until the I is equal to the end index. This on the last tutorial, do you remember how we could do that? I'll give you a hint. We're going to be using a while loop here. Remember while a condition is true then it's going to continue the set of instructions that we want to execute. The condition that we want if we're looking over here is I is equal to an index. Well we want to stop it when I is equal to an index. So when I is not equal to the end index and I'll just put the call on there at the end of the definition here. This might be pretty new for you. You can see the exclamation mark here. In the last video, you learned about the double equal sign which is just the maths equivalent of the equal sign or when we have an exclamation mark, that's the same thing as saying not. You'd be familiar with that in stretch where we've got this not boolean block. So when this is when I is not equal to the end index and basically when I becomes equal to the end index, well then we're going to stop executing this set of instructions. Alright we're going to make this an infinite loop. Let's get our I value and let's reassign it to itself plus one. Now there is a shorthand that we could use for this. We can put a plus operator here and plus equals is just doing going to do the same thing. So we can get rid of the I plus here. We can just say I plus equals one and this is just the same thing as saying alright we're going to take the current value of I and add whatever is on the right hand side to it. So this is nice shorthand. Okay we better refresh our minds with what this function is actually doing. So we have this value called text output which we've initially as up here to an empty string. And what we're doing is we're stepping through all the characters in the line from a starting position up until an ending position. And each time we step over a character we're going to add that character to the current value of text output. So let's do that over here in Python. So we're going to get that value of text output. We want to reassign it to its current value plus whatever character we're up to in line. Do you remember that syntax in the last tutorial with how we can read the character in the line based on an index? Well we better reference the line first because we're passing in the line inside the function. We need the square brackets. Remember we can refer to specific indexes here but we've got one here that we're counting and we're counting the value of I. Okay remember I is just a local variable. It's an integer and we are starting at an index. Next challenge for you is we just learned about shorthand and the cool thing is we can do shorthand with strings as well. So the equivalent to this, the shorthand, we could plus equal whatever we are up to line. These two lines they're exactly the same. This plus equals is just saying take the current value of text output and add whatever is on the right hand side to it. So whatever flavor you understand the moment I recommend you go for but I'm gonna use the shorthand. Okay I realize I've been doing lots of talking here and you need to also see some stuff visually. So let's just step through these shorthand. So I'm just going to initialize a variable here calling text output and let's make it that empty string value. And if I get text output I can take its current value and add another string to it. So let's add high and then if I check out the value of text output there you can see that we get the value high. Now let's check out the other shorthand. Let's do the plus equals variety and let's type in the surfing scratcher may need a space at the start of that one as well. Let's click enter and check the value of text output and boom, we get high surfing scratcher. So see how we're just taking the current value of text output and adding whatever is on the right hand side to it. Definitely encourage you to play around with that in the console just so you can get a good understanding of what's going on here. Presently if we were to call slice line between and fit it these parameters here then it wouldn't return anything. Remember functions should probably return something that don't always need to. They can just do some business. But this one we want to return whatever that text output value is. So let's make sure that we're being a good citizen and do that. Cool. Then we can click the run value here and let's just initialize a variable called line and let's call this function definition now. So slice line between we're going to feed it that line. We need a start index. Let's just start at the start and let's just slice it up to six spaces because it will include those characters there. Now if we click enter, you'll see that we get dash colon and is there a space there? I can't really tell. No, we're getting it also includes. So we've got the first five characters here. One, two, three, four and five. So this is up into but not including the fifth character. If you wanted this to be slightly more accurate, you'd probably want to loop this until we're at the end index plus one here because that will take us to the sixth position. Just depends on your flavor and what you define for this to be. Now we can pass at that line and we can say, all right, let's get the start index. And maybe we want to go up to where the colon position is and we can pass functions into other functions, which is pretty neat. For this line, we're going to start at this position and we're going to go right up until the colon position and let's check out what we're going to get here. Huh, we get an error. Get colon position for line. So I've just spelled the name of it incorrectly. It's in line, not for line. So let's just check that out again. And I'm just going to go in there and fix my typo. Click enter and you can see there you go. We've got dash now. We've got the result that we're after. All right, it's time for our one liner. We're at the end of this tutorial. So let's just put in a comment here for our one liner. So everything that we've just done up until now can be done in Python in one line. The way in which we'll do that is just like this. So let's check out our text output and we're going to assign it to whatever the line is. And there's a special slicing syntax that already exists in Python. Again, we're going to get the square brackets here. Remember the square brackets reference a particular position in a string or even a list, but we're not going to get into that right here. And our one liner is going to be like this. We're going to take the start index. We're going to put a colon character there and we're going to go right up until the end index. And that's it. That is our one liner. Let's jump into the console just to check out what's going on there. So I can assign a string to our line. And then for that line, let's get our syntax up there. Remember if we want the first character in the line, that D, we can just feed it that particular index. But say we want the start position and we want the up until the fifth position. So we're actually going to take a slice of that particular chunk of text, that string there. You can see that we've also included the colon character there, which is slightly different to the functionality of what our slice line between. Remember it doesn't include that fifth character. Some other special syntax is that we can take that line and we can remove the first character and just put in the end index. And end index is negative one. So this will take a slice of the whole string. We can say, okay, up until that fifth character, right until the end. So there we've got our sentence of that particular slice. So that's just a little taste into the slicing syntax in Python, where you've got your string here, you have your brackets, and then you have your start index. And it's separated by the colon character there. For more info on that, I recommend checking out the Python documentation or just Googling it, like how to slice a string in Python. And you'll probably get some good links on there. Okay, that's it for this particular tutorial. In the next one, we're going to zoom across and start in planning some more functions. This is where we're going to take a line and split it into its word and its sentence. We're going to use this function that we've just created, slice line, to extract a word and extract a sentence. We're going to be creating some new lists and we're going to be learning some new list functions. And of course, I'm going to have a special one liner to share with you as well. So I look forward to catching you in that next video. But until then, I'm off to catch a wave. I'll see you in the next one.