1 00:00:00,000 --> 00:00:04,500 Strings are what are called an iterable, and they are a sequence, 2 00:00:04,500 --> 00:00:08,400 meaning that we can break them apart, and we can take action 3 00:00:08,410 --> 00:00:10,050 based on every single letter in a string. 4 00:00:10,220 --> 00:00:14,780 So with that comes this idea of an index, and we can do this 5 00:00:14,790 --> 00:00:17,780 with a list or an array or a tuple as well. 6 00:00:18,080 --> 00:00:22,490 So let's go ahead and make a string. 7 00:00:22,500 --> 00:00:28,100 So the 'course = "Python for Everybody". 8 00:00:28,300 --> 00:00:31,750 And an index is literally just a number. 9 00:00:31,760 --> 00:00:34,810 So it's your variable name, and then it's got the hard brackets, 10 00:00:35,020 --> 00:00:37,030 and then you just put a number in there. 11 00:00:37,040 --> 00:00:40,180 So number 0, because computers always start counting at 12 00:00:40,180 --> 00:00:43,800 0, is going to be the 'P', and then we can just simply count from there. So 13 00:00:43,890 --> 00:00:49,950 if we go 0-1-2-3-4-5, the fifth letter in here, the fifth 14 00:00:49,960 --> 00:00:53,080 number rather is going to be the 'n'. 15 00:00:53,620 --> 00:00:55,810 So let's go ahead and test out these assumptions. 16 00:00:55,940 --> 00:01:00,620 So index 0, again, that's just with your variable name, 17 00:01:00,630 --> 00:01:05,019 hard bracket, then your index number, and then closing hard 18 00:01:05,030 --> 00:01:08,230 bracket. 'P', just as expected. 19 00:01:08,240 --> 00:01:13,200 And 'course[]', what I say it was? 5, I believe, is going to 20 00:01:13,210 --> 00:01:15,480 be 'n', just like that. 21 00:01:15,490 --> 00:01:19,080 So in most programming languages an index tends to start 22 00:01:19,090 --> 00:01:22,290 with the number 0, not all of them, but most of them. 23 00:01:22,400 --> 00:01:27,500 So to humans, 0 really just doesn't mean anything. 24 00:01:27,510 --> 00:01:30,770 0 actually means literally nothing. 25 00:01:30,780 --> 00:01:32,720 And it's kind of crazy to think about, 26 00:01:32,730 --> 00:01:35,260 but to a computer, 0 is a number. 27 00:01:35,270 --> 00:01:39,010 So if I said you have 5 apples and I have 0 apples, 28 00:01:39,180 --> 00:01:40,530 well, I have nothing. 29 00:01:40,540 --> 00:01:42,570 I have a basket of nothing. 30 00:01:42,580 --> 00:01:44,370 It's just an empty basket of nothing. 31 00:01:44,380 --> 00:01:46,260 You might have a basket of apples. 32 00:01:46,270 --> 00:01:52,270 I have a basket of zero apples, aka a blank or empty or void 33 00:01:52,270 --> 00:01:57,400 basket. But to a computer, there is always that 0 number, 34 00:01:57,400 --> 00:02:01,100 and that's why computer languages usually start an index with the number 35 00:02:01,190 --> 00:02:04,260 0, it's because it's actually, it's a real thing for a computer. 36 00:02:04,270 --> 00:02:06,750 So that is indexing through a string. 37 00:02:06,800 --> 00:02:11,000 If we want to index through a list, we could do 'lst', short for list 38 00:02:11,000 --> 00:02:16,100 Because if I do 'list', you can see it sort of glows green a little bit, 39 00:02:16,140 --> 00:02:17,420 that is syntax highlighting. 40 00:02:17,420 --> 00:02:19,100 That means it's a reserved keyword. 41 00:02:19,100 --> 00:02:29,300 So let's just do 'lst = ['Car', 'Truck', 'Airplane', 'Blimp']' 42 00:02:29,320 --> 00:02:31,740 I don't know why I'm using any of these in this example, 43 00:02:31,950 --> 00:02:33,570 but let's take a look at this anyways. 44 00:02:33,780 --> 00:02:37,050 So I have this variable called 'lst' short for list. 45 00:02:37,200 --> 00:02:41,800 And if this follows the exact same pattern as up here, 'Car' 46 00:02:41,800 --> 00:02:44,500 will be 0, 'Truck' will be 1, 'Airplane' will be 2, and 47 00:02:44,580 --> 00:02:45,770 'Blimp' will be 3. 48 00:02:46,060 --> 00:02:48,400 And we can actually test out our assumptions here. 49 00:02:48,400 --> 00:02:50,900 So we can do 'list', index 0, 'list[0]' 50 00:02:50,900 --> 00:02:54,900 And that's not going to work because I actually typed 'list', 51 00:02:54,900 --> 00:02:57,900 it should be 'lst[0]', and there we go, 52 00:02:57,950 --> 00:03:00,880 we got 'Car'. 'lst[2]'. 53 00:03:02,000 --> 00:03:06,010 Just take a sec, and think about this one. Which one is going to be 2? 54 00:03:06,100 --> 00:03:08,300 [no audio] 55 00:03:08,320 --> 00:03:12,590 If you are thinking that index 2 is going to be 'Airplane', 56 00:03:12,590 --> 00:03:13,640 you're absolutely correct. 57 00:03:13,760 --> 00:03:16,370 If you think index 2 is going to be 'Truck', well remember 58 00:03:16,380 --> 00:03:18,530 we start counting at 0, 0-1-2. 59 00:03:18,860 --> 00:03:22,280 And if you've made that sort of mistake already, that is 60 00:03:22,280 --> 00:03:25,610 okay, because I've been doing this for 20 years and indexing 61 00:03:25,620 --> 00:03:28,610 still occasionally it still comes out and bites me as well. 62 00:03:28,620 --> 00:03:31,280 So it's just something to sort of wrap your head around. 63 00:03:31,290 --> 00:03:34,190 But we can see that 'lst[2]' is 'Airplane'. 64 00:03:34,300 --> 00:03:37,800 Now, just for funsies, let's go ahead and see if we can get the reverse. 65 00:03:38,060 --> 00:03:40,160 So let's do list with a negative number. 66 00:03:40,360 --> 00:03:42,760 Which one do you think this is going to provide 67 00:03:43,300 --> 00:03:47,650 if we start counting backwards from 0? This one is 'Blimp'. 68 00:03:48,800 --> 00:03:52,000 negative one, negative 2, negative three, negative 4. 69 00:03:52,500 --> 00:03:54,200 Let's try that again. 70 00:03:54,200 --> 00:03:56,600 'lst[-3]'. 71 00:03:56,600 --> 00:04:01,100 And if this holds up, -1, -2, -3, should be 'Truck'. 72 00:04:02,400 --> 00:04:03,600 And there we go. 73 00:04:03,650 --> 00:04:06,430 So now we have indexing forwards and backwards. 74 00:04:06,440 --> 00:04:08,890 Now that's just part of indexing. 75 00:04:08,900 --> 00:04:11,290 The other part of indexing is this thing called slicing. 76 00:04:11,480 --> 00:04:16,640 So if I type out the word 'course' here, we have "Python for 77 00:04:16,649 --> 00:04:19,709 Everybody". In a string we can slice very easily. 78 00:04:19,720 --> 00:04:23,440 All we have to do is say, 'course', where do we want to 79 00:04:23,440 --> 00:04:25,399 start? Let's start at the very beginning. That's the 'P'. 80 00:04:25,459 --> 00:04:28,630 And for how long do we want to go for, or up until rather? 81 00:04:28,660 --> 00:04:31,610 And let's say we want to go up until the number 5. 82 00:04:31,610 --> 00:04:36,300 So 0-1-2-3-4-5. 83 00:04:36,300 --> 00:04:37,600 Let's take a look at this. 84 00:04:37,600 --> 00:04:41,010 Well, this one only gives us "Pytho", not "Python". 85 00:04:41,220 --> 00:04:45,070 So if we wanted the whole word, we would have to use 'course 86 00:04:46,210 --> 00:04:47,080 [0:6]', 87 00:04:47,110 --> 00:04:48,820 and that will give us the word "Python". 88 00:04:49,060 --> 00:04:53,180 But let's also, let's slice the word 'for' out of here. 89 00:04:53,190 --> 00:04:57,400 So we've got 0-1-2-3-4-5-6. 90 00:04:57,410 --> 00:05:01,810 So we have to start on index 7, 8-9-10. 91 00:05:01,810 --> 00:05:05,300 So let's do 'course[7:10]' 92 00:05:05,300 --> 00:05:06,370 Let's see what this gives us. 93 00:05:06,420 --> 00:05:10,770 Gives us the word 'for', and that's slicing a string. 94 00:05:10,800 --> 00:05:12,420 Now we also have that 'lst', right, 95 00:05:12,430 --> 00:05:13,830 and we can slice a list as well. 96 00:05:13,900 --> 00:05:16,100 So let's slice this list. Let's do 'lst', 97 00:05:16,780 --> 00:05:20,820 let's grab number 1 and go up until number 3, 'lst[1:3] 98 00:05:20,820 --> 00:05:26,100 So 0-1-2-3, we're going to start at 1, 0-1. 99 00:05:26,100 --> 00:05:27,100 We're going to start at 'Truck', 100 00:05:27,100 --> 00:05:28,700 we're going to grab 'Airplane' as well, 101 00:05:28,740 --> 00:05:31,010 and we're going to go up to the third index. 102 00:05:31,010 --> 00:05:33,440 So we're not going to get 'Blimp', but we're just going to 103 00:05:33,700 --> 00:05:35,440 get up until 'Blimp'. 104 00:05:35,450 --> 00:05:37,780 So 'Truck' and 'Airplane' is what we should be getting back. 105 00:05:37,860 --> 00:05:39,840 And sure enough, there we go. 106 00:05:39,840 --> 00:05:45,900 We have now indexed and sliced a string and a list, so sequences. 107 00:05:45,900 --> 00:05:50,470 Last but not least, we can also take these slices or 108 00:05:50,480 --> 00:05:54,100 any of these indexes, and we can put them into another variable. 109 00:05:54,110 --> 00:05:55,900 So we have this variable called 'course', 110 00:05:55,910 --> 00:05:58,150 but what if we wanted to get a variable called 'language'? 111 00:05:59,100 --> 00:05:59,800 'language = course[, 112 00:05:59,820 --> 00:06:01,410 start at 0, 113 00:06:01,600 --> 00:06:02,600 go to six, 114 00:06:03,000 --> 00:06:04,100 hit 'Enter', 115 00:06:04,100 --> 00:06:06,100 [no audio] 116 00:06:06,100 --> 00:06:07,600 and there we go. 117 00:06:07,600 --> 00:06:09,700 We have 'language' as "Python", 118 00:06:09,700 --> 00:06:12,810 and 'course' is still "Python for Everybody". 119 00:06:12,840 --> 00:06:16,080 So all it did there was it took out this little piece of text, 120 00:06:16,260 --> 00:06:18,840 and threw it into a new variable called 'language'. 121 00:06:19,120 --> 00:06:24,460 Now, in most Python cases, at least Python cases I run into 122 00:06:24,500 --> 00:06:30,400 you won't use this one too often, but you will use list slicing quite a bit. 123 00:06:30,420 --> 00:06:35,220 So even though slicing or indexing a particular string or 124 00:06:35,230 --> 00:06:38,430 a sentence, well it's not actually super useful, it is good to 125 00:06:38,440 --> 00:06:40,230 know because you can also apply it to lists, 126 00:06:40,230 --> 00:06:42,700 you can apply it to tuples and things like that.