1
00:00:04,680 --> 00:00:07,760
So let's see some more things we can do with indexing,

2
00:00:07,760 --> 00:00:11,500
continuing with the strings2 program from the last video.

3
00:00:11,500 --> 00:00:15,600
Well, we can index backwards in a sequence, by using a negative number,

4
00:00:15,600 --> 00:00:20,500
so parrot square brackets -1 will give the last character e.

5
00:00:20,500 --> 00:00:23,200
So let's try this out.

6
00:00:23,200 --> 00:00:26,280
So I'm gonna come down here, and on line 14 I'm going to type print,

7
00:00:26,280 --> 00:00:29,680
and left and right parentheses just to make a blank line.

8
00:00:29,680 --> 00:00:34,340
Then on line 16 I'm going to type print parentheses parrot,

9
00:00:34,340 --> 00:00:39,860
and as I mentioned, using square brackets and put a -1 inside that square bracket.

10
00:00:39,860 --> 00:00:42,920
Now if we run that.

11
00:00:42,920 --> 00:00:46,260
Alright, so again an e printed after we ran the program, as you can see.

12
00:00:46,260 --> 00:00:49,979
Now note that minus zero doesn't really make sense here,

13
00:00:49,980 --> 00:00:54,000
so when counting from the end of the string, we start counting from -1.

14
00:00:54,000 --> 00:01:03,980
So continuing on, if I type print, on line 17, parentheses parrot and -14 in square brackets,

15
00:01:03,980 --> 00:01:07,960
and run the program,

16
00:01:07,960 --> 00:01:11,960
you can see we get the first N, the capital N, in the string.

17
00:01:11,960 --> 00:01:15,680
So I think a mini-challenge will help solidify negative indexes.

18
00:01:15,680 --> 00:01:19,120
It's really a mini mini challenge so I'm not going to use a slide.

19
00:01:19,120 --> 00:01:23,680
What I will do is take a copy of lines 7 through 12,

20
00:01:23,680 --> 00:01:29,300
and what I'm going to do is copy over lines 16 and 17.

21
00:01:29,300 --> 00:01:35,080
Alright so the mini-challenge is to change the indexes on lines 16 through 21,

22
00:01:35,080 --> 00:01:37,200
to use negative indexing.

23
00:01:37,200 --> 00:01:40,200
So we still want the message, we win, to be printed .

24
00:01:40,200 --> 00:01:46,300
So pause the video and come back, when you want to see my solution.

25
00:01:46,320 --> 00:01:52,620
Alright, welcome back. So to solve the challenge we have to use the index positions from the end of the string,

26
00:01:52,620 --> 00:01:55,740
where the last character is at position -1.

27
00:01:55,740 --> 00:02:00,040
So that means that w is at position -11.

28
00:02:00,040 --> 00:02:04,960
So if I change the 3 there to -11

29
00:02:04,960 --> 00:02:07,680
and run this,

30
00:02:08,160 --> 00:02:12,560
there's the first letter, and what we'll do is we'll go through and finish the rest now.

31
00:02:12,560 --> 00:02:16,720
The next one is -10,

32
00:02:16,720 --> 00:02:20,560
next one, -5,

33
00:02:20,560 --> 00:02:24,120
next one -11,

34
00:02:24,120 --> 00:02:29,360
next one - 8, and the last one, -6.

35
00:02:29,360 --> 00:02:35,980
We can run this and verify we still get the output, we win, one character at a time.

36
00:02:35,980 --> 00:02:39,300
Now you may have noticed that the negative index values can be obtained,

37
00:02:39,300 --> 00:02:43,300
by subtracting the string length 14, from the positive ones.

38
00:02:43,300 --> 00:02:49,340
So you may have actually done something like this, and what I'll do is I take another copy of lines 7 to 12.

39
00:02:49,340 --> 00:02:52,700
I'll come down here,

40
00:02:52,860 --> 00:02:56,600
and I'm gonna paste this down here, on line 23.

41
00:02:56,600 --> 00:03:02,360
So again, what I said was you may notice that the negative index values can be obtained by subtracting

42
00:03:02,360 --> 00:03:06,940
the string length, 14 in this case, from the positive numbers.

43
00:03:06,940 --> 00:03:11,740
So you may have done something like this, just basically, subtracting 14 from each value, so instead of 3,

44
00:03:11,740 --> 00:03:26,960
be 3 minus 14, 4 minus 14, 9 minus 14, 3 minus 14, 6 minus 14 and lastly 8-14

45
00:03:26,960 --> 00:03:29,600
Run the program

46
00:03:29,900 --> 00:03:33,060
and you can see there that we still get the output, we win,

47
00:03:33,060 --> 00:03:39,760
and what we could do to be consistent we could add another print there,

48
00:03:39,760 --> 00:03:43,600
and again, just to make sure we have got a space there and we can see that, we've still got we win.

49
00:03:43,600 --> 00:03:46,395
We've got basically three lots of we win showing on the screen,

50
00:03:46,400 --> 00:03:50,500
which is consistent with the fact that we've done this three times now.

51
00:03:50,500 --> 00:03:55,280
So experiment with negative index values to make sure you're comfortable with how to use them.

52
00:03:55,280 --> 00:04:01,740
You won't use negative index as often as positive ones, but when you do need to use them they're incredibly useful.

53
00:04:01,740 --> 00:04:05,700
In the next video, we'll continue with strings and look at how to slice a string.

54
00:04:05,700 --> 00:04:08,620
See you in the next video.

