1 00:00:00,390 --> 00:00:03,390 Now, up to this point, we've learned that Esther. 2 00:00:04,430 --> 00:00:08,260 Is an ordered sequence of characters, right? 3 00:00:08,510 --> 00:00:15,920 It's essentially a piece of text where we can write our names, passwords, paragraphs and sentences 4 00:00:16,700 --> 00:00:24,100 and strings underneath the hood are stored in memory, as I said, as an ordered sequence of characters. 5 00:00:24,650 --> 00:00:30,560 So, for example, if I had the string mi mi mi. 6 00:00:31,950 --> 00:00:40,680 Then this will be stored in our computers memory in an order so and will be stored in a place in memory, 7 00:00:41,070 --> 00:00:48,870 and after that place and memory, it will be stored, then the space will be stored and then M, then 8 00:00:48,870 --> 00:00:50,510 e the space than the need. 9 00:00:50,940 --> 00:00:56,970 So if you think of a bookshelf, each one of these is ordered in a different part of the bookshelf, 10 00:00:56,970 --> 00:00:58,370 but one after another. 11 00:00:59,340 --> 00:01:02,580 And the way we can think about this is like this. 12 00:01:03,980 --> 00:01:13,340 The M is stored in location zero and then E is stored in location one, and then space is stored in 13 00:01:13,340 --> 00:01:16,400 location two and so on and so forth. 14 00:01:18,120 --> 00:01:21,630 And each shelf now corresponds. 15 00:01:22,660 --> 00:01:24,520 To a value. 16 00:01:25,800 --> 00:01:33,480 So that when we ask the computer, hey, I want you to grab this string, we know that, hey, we're 17 00:01:33,480 --> 00:01:36,470 going to look in the shelf zero to seven. 18 00:01:37,440 --> 00:01:39,360 Now, why is this important? 19 00:01:40,050 --> 00:01:47,850 Because one of the most useful things you can do with strings is to access different part of a string 20 00:01:48,120 --> 00:01:50,660 using it's what we call an index. 21 00:01:51,630 --> 00:01:59,070 So, for example, when you have a string and let's call this variable or this piece of string selfish. 22 00:02:00,210 --> 00:02:03,720 And selfish has me, me, me, me, me. 23 00:02:05,870 --> 00:02:07,400 And I'm going to comment this out. 24 00:02:09,130 --> 00:02:11,660 Just like this, make sure it's aligned. 25 00:02:11,710 --> 00:02:12,070 OK? 26 00:02:13,090 --> 00:02:21,760 And what I'm going to do here is I'm going to say selfish and then square brackets right here and then 27 00:02:21,760 --> 00:02:22,990 type in zero. 28 00:02:24,540 --> 00:02:25,890 Now, if I print this. 29 00:02:31,790 --> 00:02:38,870 Check that out, I get the letter M, why is that wall using the square brackets? 30 00:02:39,050 --> 00:02:43,370 I'm telling the computer, hey, grab the variable selfish. 31 00:02:44,500 --> 00:02:52,120 And then from that variable selfish grab whatever is an index of zero and what's on that bookshelf of 32 00:02:52,120 --> 00:02:52,960 index of zero? 33 00:02:53,170 --> 00:02:56,830 Well, the letter and what if I do seven? 34 00:02:57,640 --> 00:03:00,010 What do you think will happen if I click run? 35 00:03:00,970 --> 00:03:10,180 I get it, because on the bookshelf of number seven, that's it, and when I am getting just selfish, 36 00:03:11,080 --> 00:03:12,640 I get the entire string. 37 00:03:12,880 --> 00:03:18,250 It's going to say, all right, I'm going to grab from the bookshelf index of zero all the way to seven. 38 00:03:20,020 --> 00:03:27,160 Very, very cool, and this concept we're going to explore throughout the course and you're going to 39 00:03:27,160 --> 00:03:34,840 see why it's important very soon, but using this knowledge, we can do a lot of string manipulation. 40 00:03:35,200 --> 00:03:41,650 And Python has this unique feature that we're going to talk about right now that allows us to grab different 41 00:03:41,650 --> 00:03:42,820 pieces of text. 42 00:03:43,840 --> 00:03:45,260 So let's have a look here. 43 00:03:45,970 --> 00:03:50,380 The rule here is when we use square brackets in Python. 44 00:03:51,550 --> 00:03:58,630 The first item that we put between the square brackets is what we call the start. 45 00:03:59,570 --> 00:04:05,110 The start is, hey, where do you want me to look in our case, we said we want to look at zero. 46 00:04:05,270 --> 00:04:07,220 So we got in, we want to look at seven. 47 00:04:07,400 --> 00:04:08,080 We got it. 48 00:04:08,960 --> 00:04:12,830 But then there's an extra thing that we can do if we do a. 49 00:04:13,780 --> 00:04:14,710 Colin here. 50 00:04:15,810 --> 00:04:20,610 We also have the option to say, hey, where to stop? 51 00:04:21,860 --> 00:04:22,550 What does that mean? 52 00:04:22,940 --> 00:04:27,140 Well, if I do something and you know what, let's change this to. 53 00:04:28,440 --> 00:04:35,610 Zero, one, two, three, four, five, six, seven, just so it's easier to understand if I do, let's 54 00:04:35,610 --> 00:04:36,960 say the zero. 55 00:04:38,110 --> 00:04:47,860 Colin to and I click run, I get start at zero and at two, so I get to grab zero. 56 00:04:49,030 --> 00:04:55,030 To one, because remember, I'm saying start here, but then stop as soon as you get to bookshelf, 57 00:04:55,030 --> 00:04:55,410 too. 58 00:04:55,660 --> 00:04:56,560 We don't want that. 59 00:04:57,370 --> 00:05:02,890 So if I do zero to seven, what do you think will happen while I click run? 60 00:05:04,420 --> 00:05:07,060 And I get all the way to six. 61 00:05:09,630 --> 00:05:15,870 So in order for us to grab the full strength, we have to do zero to eight because there's actually 62 00:05:15,870 --> 00:05:16,950 a character here, right? 63 00:05:16,950 --> 00:05:19,350 Because we start from zero, in fact, like Ron. 64 00:05:22,360 --> 00:05:28,960 Now, there's a third thing that we can add here, and this is called the step over. 65 00:05:29,970 --> 00:05:35,640 And a step over says, hey, start here and here and then step over a few things. 66 00:05:36,210 --> 00:05:42,180 So the default is one because we're going one by one through our bookshelf. 67 00:05:43,560 --> 00:05:51,980 You see that we get the entire string, but if I add to here, it steps over by two, so I grab zero, 68 00:05:52,290 --> 00:06:00,230 then I step over one to grab to, then step over one to grab for step over and so on and so forth. 69 00:06:01,990 --> 00:06:09,310 So let me ask you a couple of tricky questions here, what happens if I do this? 70 00:06:12,030 --> 00:06:15,780 Is this valid, if I click run, what will happen, ready to guess? 71 00:06:19,200 --> 00:06:21,930 One, two, three, four, five, six, seven. 72 00:06:22,980 --> 00:06:24,890 This says, hey, start at 1:00. 73 00:06:24,960 --> 00:06:31,640 So that is right here, but then afterwards, stop, well, there's nothing there. 74 00:06:31,650 --> 00:06:34,770 So the default is going to say go all the way to the end. 75 00:06:36,310 --> 00:06:42,130 I know this is confusing, but hang in there, practice this often enough, and this will become second 76 00:06:42,130 --> 00:06:42,490 nature. 77 00:06:43,550 --> 00:06:49,960 OK, what if I do something like this where I don't fill the start and do five here, what happens now? 78 00:06:53,890 --> 00:07:02,230 I get zero one, two, three, four all the way until five, so it starts as default zero and goes to 79 00:07:02,230 --> 00:07:02,560 five. 80 00:07:04,140 --> 00:07:11,610 OK, what if I do something like this to semicolons, and then what if I click run? 81 00:07:13,200 --> 00:07:16,710 All right, I get the default behavior. 82 00:07:18,070 --> 00:07:24,350 Because it's starting at zero when there's nothing it and whenever the string ends, when there's nothing, 83 00:07:24,370 --> 00:07:26,680 and then we're stepping over through it once. 84 00:07:28,310 --> 00:07:32,340 All right, but what if we do something like this? 85 00:07:32,930 --> 00:07:35,240 What if I do minus one here? 86 00:07:36,350 --> 00:07:37,270 This is a tricky one. 87 00:07:37,940 --> 00:07:38,640 What will happen? 88 00:07:38,780 --> 00:07:39,340 Let's find out. 89 00:07:40,970 --> 00:07:50,690 I get seven in Python, the negative index means, hey, start at the end of the string so that if I 90 00:07:50,690 --> 00:07:52,520 do minus two. 91 00:07:54,050 --> 00:07:56,630 I get six if I do minus three. 92 00:07:58,010 --> 00:08:06,260 I get five because I'm going backwards and a neat trick that you can do here is if we do semicolon, 93 00:08:06,260 --> 00:08:12,320 semicolon minus one, and this is actually quite a common operation. 94 00:08:12,330 --> 00:08:17,960 So you just have to memorize this, even though it looks weird, it means start, stop. 95 00:08:18,570 --> 00:08:19,810 But there's no limit here. 96 00:08:19,910 --> 00:08:22,280 But I want to step over from the back. 97 00:08:22,670 --> 00:08:23,980 So what do you think will happen here? 98 00:08:24,940 --> 00:08:25,930 If I click, run. 99 00:08:28,580 --> 00:08:36,110 Do you see that we get the reverse of the string, so this is a very useful notation if you want to, 100 00:08:36,110 --> 00:08:38,240 let's say, reverse an order. 101 00:08:39,750 --> 00:08:45,900 And if we want to, let's say skip by two, I click run and you see that we're skipping by two now. 102 00:08:47,740 --> 00:08:52,440 Now, hopefully your head doesn't hurt with all this notation, you just have to get used to it. 103 00:08:52,450 --> 00:08:56,560 It's something quite unique to Python, but it's very, very useful.