Index Tuples and Strings

00:00 Previously, I’ve mentioned that tuples and strings have things in common. Now, in this lesson, we’ll go a little bit deeper into this comparison between tuples and strings.

00:11 I’ll start off by showing you some similarities between tuples and strings. Let’s start with an overview. Tuples have a length, support indexing and slicing, tuples are immutable, and tuples are iterable.

00:23 All of these things they have in common with strings. Start off by looking at the length of a tuple. Just like with a string, you can call the len() function and pass it a tuple, and as a result, you’ll get how many elements are in the tuple.

00:38 Try that out in IDLE.

00:41 So you’ve seen a numbers tuple that just points to (1, 2, 3),

00:48 and then we can pass it to the len() function, and you can see that as an output you get the number 3. Now, let’s compare it to a string, right?

00:57 We worked with the word "Python" before,

01:02 and the word Python also has a length of six. Now, you could even turn the word Python into a tuple because a tuple can consist of any sort of data types, so it doesn’t have to be integers, it could be strings.

01:16 So what we did before was to split up the word Python into its characters, each a separate string, and make them the elements of a tuple. So that, again, I’ll call it the word tuple,

01:30 and just pass our word to the tuple() function. And now we have word_tuple consisting of the separate characters of the word Python.

01:42 And this also has a length, right? So this should be the same length as the word.

01:48 And here you go. It consists of six characters, just like the string "Python" consists of six characters.

01:56 Further, each element in a tuple has a numbered position called an index. That’s also the same as it is with strings. So you can access individual elements through their index.

02:07 And you do that by placing the index number inside of a pair of square brackets ([]) right after the tuple. You’ve probably done this with strings before, but here’s an example again with that numbers list.

02:18 Here we’ve defined a tuple called numbers, which consists of three elements: 1, 2, and 3. And then in the next line, you’re accessing the element at index position one, and that is the number two. As an output, you’re going to get the number 2.

02:34 And note that the index count starts with zero, like in most programming languages. So 1 is at the index zero, 2 is at index one, and 3 is at index two, which is why when you access the element at index one, you get back out the number 2.

02:51 So just a quick recap of indexes or indices: you can use either of the plurals, it’s just like toople and tupple, huh? Here’s a string and the relevant indexes for the characters in the string, the letter P would be at index zero, the letter y would be at index one, et cetera, up to the n, the last letter of the word Python, which is at index five.

03:13 The same goes for tuple indexing. So if you think of an expanded numbers list that has the first three numbers under ten, and then the last three numbers under ten, so it consists of 1, 2, 3, 7, 8, and 9.

03:25 Then you’d find the number 1 at index zero, the number 2 at index one, et cetera. And at index five, you’d find the number 9 in this case.

03:35 Also, Python supports negative indexing, which means that you can go backwards. This is also the same as with strings. So you can access the integer 9 in this example tuple also by using minus one as the index number in square brackets and all the way backwards so that you could access the first element in this specific tuple by passing minus six as the negative index.

03:59 Now, maybe this sounds a little abstract, so let’s try it out again. In IDLE, I’ll work with word and word_tuple. So if you remember string indexing, I’ll access the P, the first element, by passing in 0.

04:14 You can see that I get out the P, and with word_tuple, I can do the exact same thing. word_tuple, open up the square brackets, put in the 0, close the square brackets, and again, I get the P.

04:29 Now let’s take a look at something in between. Maybe we want to get out the t of Python. So in the word_tuple, I can pass in the index position.

04:41 That would be—P is at zero, y is at one, and t is at two—so if I pass in 2 as the index number, I’ll get out the t.

04:50 And again, this is the same if you do it with a string.

04:55 And finally, also the negative indexing. So if you want to get the last element here, you could pass in the index number 5, but you could also go backwards and say word_tuple, open up the square brackets and then pass in -1.

05:08 This is going to give you the last element in the tuple. And in this case, that’s an n.

05:14 Now, you may think, okay, so that’s exactly the same as with the string. What’s the point? Well, the point is tuples can’t only just contain characters. They can, for example, also contain numbers.

05:25 So we have this numbers tuple, right? And you can do the same thing that you did with the string elements in a tuple also with numbers. So I can access the last element of the numbers tuple by passing in -1, and this’ll give me the number 3 as an output.

05:43 So tuples and strings both support indexing.

Become a Member to join the conversation.