Hi, and welcome to this bonus video on string slicing. I decided to make this video as a result of several questions that I got in the q&a area of the course regarding the use of a step in a string slice. So without further ado, let me give you some examples to clarify this feature of Python. So let's consider this string. Now let's also see my string of zero, which is zero, my string of one which is one, my string of nine, which is nine. Okay, now let's remember the general syntax for string slicing.
That would be my string, and in between square brackets, we have start, colon, stop, colon and step. This translates to a slice starting as the start index going up to but not including the stop index with an optional step given by the step value. Actually, all three values in between square brackets are optional. This means you are not required to have the start the stop and the step value in a slice all at once. Instead, you can have only the start and stop indexes, as you've seen in the previous videos, or, for instance, only the start index and the step value without the stop index. Now let's consider a couple of examples.
To make this as clear as possible. Let's assume that we want a slice of my string that will contain only the characters representing the even numbers in the string. So that would be 02468. Right? To do that, we must consider the entire string and that is done using no start and stop indexes at all, just a colon, as you've learned in the previous videos in this section, so let me write this colon. Additionally, in order to add a step of two, we must insert a second colon, and then just add this parameter.
So colon again. And now to let's hit Enter, and the result is correct, we got all the even numbers from the string, because we started our search at index zero, up to the end of the string with a step of two. On the other hand, let's say that you want to extract all the odd numbers from this string, that would mean 13579. Right? Well, in that case, we know that we should start our slides at index one, which in this case, is the index of the value one in the string, and then go all the way up to the end of the string with a step have to again Okay, so let's try this as well. That would mean my string starting at index one until the end of the string with a step of two.
Let's hit Enter, and the result is correct. Again, notice that we didn't need a stop index, just the start index and the step because we wanted to search throughout the entire string until the last character. Finally, let's say that you want to extract all the characters representing odd numbers in the string that are less than seven. That would mean one, three, and 5am. I correct. In this case, we will need to use all three values for the slides in order to have a starting point and ending point.
And of course, a step. So let me write this, that would mean my string starting at index one up to but not including index seven after the first colon, and finally, with a step of two after the second colon, so let's hit enter once again. So this time we started our slice at the character at index one, which is one in this case, up to but not including the character at index seven with a step of two. Therefore we got the characters at indexes one, one plus two and one plus two plus two positioned at an index less than seven. Okay, that's it. Hope this all makes more sense now.
I'll see you in the next lecture.