Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set your subtitle preferences in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please see our video player troubleshooting guide to resolve the issue.

Slice Tuples and Strings

00:00 Tuples also support slicing, so I can extract portions of a tuple just like you can do with a string by inserting a colon between two index numbers. For example, here with square brackets, 0:2, closing square brackets, you would slice out the first two items of a tuple.

00:20 Or here, let’s look at another numbers list. For example, the odd numbers under ten would be a tuple that consists of 1, 3, 5, 7, and 9.

00:29 And if you were to slice out just 5 and 7, you could use the notation [2:4].

00:37 Let’s try that out in IDLE.

00:45 So here I’ll define the odd_numbers list, and now let’s do some slicing on it. Odd numbers starting from 2 up to, but not including 4, is going to give me 5 and 7.

00:57 The index always starts at the lower index and includes that one, and then goes up to, but doesn’t include, the ending index that you put on the other side of the colon.

01:08 So we start at 0, 1, 2, we start here at the number 5, and then this is index 3, which is number 7.

01:16 That’s included in the slice. And 9 would be at index 4, and this is where it stops. And it doesn’t include the stopping index anymore, so you get 5 and 7 as the slice from this odd_numbers list.

01:28 You can do the same with a string as well. So word is our "Python" string, and here you can slice it and see what we get if we do [2:4].

01:38 Here you get t and h, and it works in the same way.

01:46 To recap, the slice [x:y] starts from the element at index x and the slice goes up to, but does not include, the element at index y.

Become a Member to join the conversation.