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