1 00:00:00,360 --> 00:00:02,100 -: Now up to this point, we've learned 2 00:00:02,100 --> 00:00:03,573 that str, 3 00:00:04,440 --> 00:00:08,520 is an ordered sequence of characters, right? 4 00:00:08,520 --> 00:00:10,920 It's essentially a piece of text 5 00:00:10,920 --> 00:00:13,932 where we can write our names, passwords, 6 00:00:13,932 --> 00:00:16,740 paragraphs and sentences. 7 00:00:16,740 --> 00:00:20,670 And strings underneath the hood are stored in memory, 8 00:00:20,670 --> 00:00:24,660 as I said, as an ordered sequence of characters. 9 00:00:24,660 --> 00:00:25,980 So for example, 10 00:00:25,980 --> 00:00:28,140 if I had, 11 00:00:28,140 --> 00:00:30,723 the string, me, me, me, 12 00:00:31,950 --> 00:00:35,593 then this will be stored in our computer's memory, 13 00:00:35,593 --> 00:00:37,680 in an order. 14 00:00:37,680 --> 00:00:41,070 So M will be stored in a place in memory, 15 00:00:41,070 --> 00:00:43,650 and after that place in memory, 16 00:00:43,650 --> 00:00:45,330 E will be stored, 17 00:00:45,330 --> 00:00:47,700 then the space will be stored, 18 00:00:47,700 --> 00:00:50,940 and then M, then E, then space, then M, then E. 19 00:00:50,940 --> 00:00:53,070 So if you think of a bookshelf, 20 00:00:53,070 --> 00:00:54,810 each one of these is ordered 21 00:00:54,810 --> 00:00:58,473 in a different part of the bookshelf, but one after another. 22 00:00:59,310 --> 00:01:02,763 And the way we can think about this, is like this. 23 00:01:03,990 --> 00:01:07,680 The M is stored in location zero, 24 00:01:07,680 --> 00:01:11,460 and then E is stored in location one, 25 00:01:11,460 --> 00:01:14,490 and then space is stored in location two, 26 00:01:14,490 --> 00:01:16,533 and so, on and so forth. 27 00:01:18,180 --> 00:01:21,730 And each shelf, now corresponds 28 00:01:22,680 --> 00:01:23,910 to, 29 00:01:23,910 --> 00:01:24,743 a value. 30 00:01:25,800 --> 00:01:27,697 So that, when we ask the computer, 31 00:01:27,697 --> 00:01:31,710 "Hey, I want you to grab this string", 32 00:01:31,710 --> 00:01:33,270 We know that, hey, 33 00:01:33,270 --> 00:01:37,047 we're going to look in the shelf zero to seven. 34 00:01:37,047 --> 00:01:40,050 Now, why is this important? 35 00:01:40,050 --> 00:01:42,600 Because one of the most useful things 36 00:01:42,600 --> 00:01:44,070 you can do with strings, 37 00:01:44,070 --> 00:01:48,120 is to access different part of a string 38 00:01:48,120 --> 00:01:51,630 using its, what we call, an index. 39 00:01:51,630 --> 00:01:53,010 So for example, 40 00:01:53,010 --> 00:01:56,370 when you have a string, and let's call this variable, 41 00:01:56,370 --> 00:01:59,223 or this piece of string, selfish. 42 00:02:00,240 --> 00:02:03,753 And selfish has me, me, me, me, me. 43 00:02:05,554 --> 00:02:07,533 And I'm going to comment this out, 44 00:02:09,120 --> 00:02:10,800 just like this, 45 00:02:10,800 --> 00:02:12,250 Make sure it's aligned, okay? 46 00:02:13,140 --> 00:02:16,770 And what I'm going to do here, is I'm going to say 47 00:02:16,770 --> 00:02:21,240 selfish, and then square brackets right here, 48 00:02:21,240 --> 00:02:23,103 and then type in zero. 49 00:02:24,540 --> 00:02:26,073 Now, if I print this, 50 00:02:29,250 --> 00:02:30,303 and I click run, 51 00:02:31,800 --> 00:02:32,730 check that out. 52 00:02:32,730 --> 00:02:35,550 I get the letter M. 53 00:02:35,550 --> 00:02:36,930 Why is that? 54 00:02:36,930 --> 00:02:39,060 Well, using the square brackets, 55 00:02:39,060 --> 00:02:39,967 I'm telling the computer, 56 00:02:39,967 --> 00:02:43,617 "Hey, grab the variable selfish." 57 00:02:44,490 --> 00:02:46,657 And then from that variable selfish, 58 00:02:46,657 --> 00:02:50,580 "grab whatever is an index of zero." 59 00:02:50,580 --> 00:02:53,160 And what's on that bookshelf of index of zero? 60 00:02:53,160 --> 00:02:55,230 Well, the letter M. 61 00:02:55,230 --> 00:02:57,660 What if I do seven? 62 00:02:57,660 --> 00:02:59,040 What do you think will happen? 63 00:02:59,040 --> 00:03:02,460 -: If I click run, I get E. 64 00:03:02,460 --> 00:03:07,140 Because on the bookshelf of number seven, that's E. 65 00:03:07,140 --> 00:03:11,130 And when I'm getting just selfish, 66 00:03:11,130 --> 00:03:12,900 I get the entire string. 67 00:03:12,900 --> 00:03:13,733 It's going to say, 68 00:03:13,733 --> 00:03:15,799 "all right, I'm gonna grab from the bookshelf, 69 00:03:15,799 --> 00:03:18,417 index of zero all the way to seven." 70 00:03:20,070 --> 00:03:22,410 Very, very cool. 71 00:03:22,410 --> 00:03:26,520 In this concept, we're gonna explore throughout the course, 72 00:03:26,520 --> 00:03:28,170 and you're going to see 73 00:03:28,170 --> 00:03:30,720 why it's important very soon. 74 00:03:30,720 --> 00:03:33,030 But using this knowledge we can do a lot 75 00:03:33,030 --> 00:03:35,220 of string manipulation. 76 00:03:35,220 --> 00:03:37,680 And Python has this unique feature, 77 00:03:37,680 --> 00:03:39,240 that we're gonna talk about right now, 78 00:03:39,240 --> 00:03:42,963 that allows us to grab different pieces of text. 79 00:03:43,830 --> 00:03:45,960 So let's have a look here. 80 00:03:45,960 --> 00:03:50,523 The rule here, is when we use square brackets in Python, 81 00:03:51,540 --> 00:03:56,540 the first item that we put between the square brackets, 82 00:03:56,580 --> 00:03:58,743 is what we call the start. 83 00:03:59,580 --> 00:04:03,150 The start is, "Hey, where do you want me to look?" 84 00:04:03,150 --> 00:04:05,280 In our case, we said we wanna look at zero. 85 00:04:05,280 --> 00:04:06,150 So we got M. 86 00:04:06,150 --> 00:04:09,000 We wanted to look at seven, we got E. 87 00:04:09,000 --> 00:04:11,550 But then there's an extra thing that we can do. 88 00:04:11,550 --> 00:04:14,883 If we do a colon here, 89 00:04:15,840 --> 00:04:18,060 we also have the option, 90 00:04:18,060 --> 00:04:20,817 to say, "Hey, where to stop?" 91 00:04:21,870 --> 00:04:22,950 What does that mean? 92 00:04:22,950 --> 00:04:25,260 Well, if I do something, 93 00:04:25,260 --> 00:04:27,220 and you know what let's change this to 94 00:04:28,440 --> 00:04:31,380 zero, one, two, three, four, five, 95 00:04:31,380 --> 00:04:32,213 six, seven, 96 00:04:32,213 --> 00:04:34,200 just so it's easier to understand. 97 00:04:34,200 --> 00:04:37,113 If I do, let's say zero, 98 00:04:38,130 --> 00:04:39,450 colon, 99 00:04:39,450 --> 00:04:40,680 two, 100 00:04:40,680 --> 00:04:42,660 and I click run, 101 00:04:42,660 --> 00:04:46,200 I get start at zero end at two. 102 00:04:46,200 --> 00:04:48,033 So I get to grab zero, 103 00:04:49,050 --> 00:04:50,040 to one. 104 00:04:50,040 --> 00:04:52,800 Because remember, I'm saying start here, 105 00:04:52,800 --> 00:04:55,650 but then stop as soon as you get to bookshelf two. 106 00:04:55,650 --> 00:04:57,390 We don't want that. 107 00:04:57,390 --> 00:05:00,120 So if I do zero to seven, 108 00:05:00,120 --> 00:05:01,740 what do you think will happen? 109 00:05:01,740 --> 00:05:03,033 Well, I click run, 110 00:05:04,500 --> 00:05:07,233 and I get all the way to six. 111 00:05:09,630 --> 00:05:12,000 So in order for us to grab the full string, 112 00:05:12,000 --> 00:05:14,670 we have to do zero to eight. 113 00:05:14,670 --> 00:05:16,890 Because there's actually eight characters here, right? 114 00:05:16,890 --> 00:05:18,390 Because we start from zero. 115 00:05:18,390 --> 00:05:21,483 If I click run, there you go. 116 00:05:22,350 --> 00:05:25,170 Now, there's a third thing that we can add here. 117 00:05:25,170 --> 00:05:29,910 And this, is called the step over. 118 00:05:29,910 --> 00:05:31,177 And the step over says, 119 00:05:31,177 --> 00:05:33,750 "Hey, start here, end here, 120 00:05:33,750 --> 00:05:36,180 and then step over a few things." 121 00:05:36,180 --> 00:05:37,980 So the default, is one, 122 00:05:37,980 --> 00:05:40,710 because we're going one by one, 123 00:05:40,710 --> 00:05:42,273 through our bookshelf. 124 00:05:43,560 --> 00:05:45,480 You see that we get the entire string. 125 00:05:45,480 --> 00:05:48,060 But if I add two here, 126 00:05:48,060 --> 00:05:50,010 it steps over by two. 127 00:05:50,010 --> 00:05:53,550 So I grab zero, then I step over, 128 00:05:53,550 --> 00:05:55,500 one, two, grab two, 129 00:05:55,500 --> 00:05:58,200 then step over, one, two, grab four, 130 00:05:58,200 --> 00:06:00,393 step over, and so on and so forth. 131 00:06:02,010 --> 00:06:06,720 So, let me ask you a couple of tricky questions here. 132 00:06:06,720 --> 00:06:09,483 What happens if I do this? 133 00:06:12,060 --> 00:06:12,900 Is this valid? 134 00:06:12,900 --> 00:06:15,030 If I click run, what will happen? 135 00:06:15,030 --> 00:06:15,963 Ready to guess? 136 00:06:17,610 --> 00:06:20,250 I get one, two, 137 00:06:20,250 --> 00:06:22,023 three, four, five, six, seven. 138 00:06:22,980 --> 00:06:24,960 This says, "Hey, start at one." 139 00:06:24,960 --> 00:06:27,000 So that is right here, 140 00:06:27,000 --> 00:06:29,340 but then afterwards, 141 00:06:29,340 --> 00:06:30,780 stop, well, 142 00:06:30,780 --> 00:06:31,620 there's nothing there. 143 00:06:31,620 --> 00:06:34,863 So the default is gonna say, go all the way to the end. 144 00:06:36,360 --> 00:06:39,090 I know this is confusing, but hang in there. 145 00:06:39,090 --> 00:06:40,620 Practice this often enough, 146 00:06:40,620 --> 00:06:42,603 and this will become second nature. 147 00:06:43,590 --> 00:06:45,450 Okay, what if I do something like this? 148 00:06:45,450 --> 00:06:47,190 Where I don't fill the start, 149 00:06:47,190 --> 00:06:49,140 and do five here. 150 00:06:49,140 --> 00:06:50,073 What happens now? 151 00:06:50,940 --> 00:06:51,773 Ready? 152 00:06:53,940 --> 00:06:56,430 I get zero, one, two, three, four. 153 00:06:56,430 --> 00:06:58,470 All the way until five. 154 00:06:58,470 --> 00:07:02,673 So, it starts as default zero, and goes to five. 155 00:07:04,170 --> 00:07:08,550 Okay, what if I do something, like this? 156 00:07:08,550 --> 00:07:10,860 Two semicolons, and then one. 157 00:07:10,860 --> 00:07:11,763 If I click run, 158 00:07:13,230 --> 00:07:16,863 all right, I get the default behavior. 159 00:07:18,030 --> 00:07:20,820 Because it's starting at zero, when there's nothing. 160 00:07:20,820 --> 00:07:23,460 It ends at whenever the string ends, 161 00:07:23,460 --> 00:07:25,860 when there's nothing, and then we're stepping over 162 00:07:25,860 --> 00:07:26,823 through it once. 163 00:07:28,380 --> 00:07:32,970 All right, but what if we do something like this? 164 00:07:32,970 --> 00:07:35,610 What if I do minus one here? 165 00:07:35,610 --> 00:07:37,770 Hmm, this is a tricky one. 166 00:07:37,770 --> 00:07:39,453 What will happen, let's find out. 167 00:07:41,010 --> 00:07:42,960 I get seven. 168 00:07:42,960 --> 00:07:46,380 In Python, the negative index, 169 00:07:46,380 --> 00:07:49,980 means, "hey, start at the end of the string." 170 00:07:49,980 --> 00:07:52,653 So that if I do minus two, 171 00:07:54,090 --> 00:07:56,793 I get six, If I do minus three, 172 00:07:58,050 --> 00:07:58,980 I get five. 173 00:07:58,980 --> 00:08:00,810 'Cause I'm going backwards. 174 00:08:00,810 --> 00:08:03,480 And a neat trick that you can do here, 175 00:08:03,480 --> 00:08:07,620 is if we do semicolon, semicolon, 176 00:08:07,620 --> 00:08:09,810 minus one, 177 00:08:09,810 --> 00:08:12,330 and this is actually quite a common operation, 178 00:08:12,330 --> 00:08:14,040 so you just have to memorize this even though 179 00:08:14,040 --> 00:08:15,300 it looks weird, 180 00:08:15,300 --> 00:08:18,570 it means start, stop, 181 00:08:18,570 --> 00:08:19,950 there's no limit here, 182 00:08:19,950 --> 00:08:22,680 but I wanna step over from the back. 183 00:08:22,680 --> 00:08:24,960 So what do you think will happen here? 184 00:08:24,960 --> 00:08:26,073 If I click run? 185 00:08:28,590 --> 00:08:30,000 Do you see that? 186 00:08:30,000 --> 00:08:32,880 We get the reverse, of the string. 187 00:08:32,880 --> 00:08:35,490 So this is a very useful notation, 188 00:08:35,490 --> 00:08:38,403 if you wanna, let's say, reverse an order. 189 00:08:39,780 --> 00:08:42,809 And if we wanna, let's say, skip by two, 190 00:08:42,809 --> 00:08:45,993 I click run, and you see that we're skipping, by two now. 191 00:08:47,760 --> 00:08:49,890 Now, hopefully your head doesn't hurt 192 00:08:49,890 --> 00:08:51,000 with all this notation. 193 00:08:51,000 --> 00:08:52,440 You just have to get used to it. 194 00:08:52,440 --> 00:08:54,960 It's something quite unique to Python, 195 00:08:54,960 --> 00:08:56,673 but it's very, very useful.