1 00:00:00,490 --> 00:00:03,090 So I want to introduce one more operation on strings, which 2 00:00:03,090 --> 00:00:05,930 we'll find very useful, which is the Find operation. It gives 3 00:00:05,930 --> 00:00:08,980 us the way in a big string to find some sub-string 4 00:00:08,980 --> 00:00:12,770 that we're looking for. The way we use Find is a little 5 00:00:12,770 --> 00:00:15,910 different from the way we've used other operators so far. Because 6 00:00:15,910 --> 00:00:18,320 Find is actually a method, and what that means is it's 7 00:00:18,320 --> 00:00:21,430 a built in procedure provided by Python. We'll be able to 8 00:00:21,430 --> 00:00:25,860 define our own procedures soon, we'll get to that in unit two. 9 00:00:25,860 --> 00:00:29,630 Find is a procedure that operates on strings, so we use it by having 10 00:00:29,630 --> 00:00:32,189 a string followed by .find, followed by 11 00:00:32,189 --> 00:00:34,500 a parentheses, then we pass in another string. 12 00:00:36,570 --> 00:00:38,700 Which is the string that we want to find in 13 00:00:38,700 --> 00:00:41,330 the first string. And the output of Find is the 14 00:00:41,330 --> 00:00:46,550 position in the string where that sub-string is found, the 15 00:00:46,550 --> 00:00:49,720 first occurrence of the string. So, if that string happens 16 00:00:49,720 --> 00:00:52,290 to occur in more places than one in the 17 00:00:52,290 --> 00:00:54,380 input string, the result of find is always going to give 18 00:00:54,380 --> 00:00:57,900 us the position. That's the number where the first occurrence 19 00:00:57,900 --> 00:01:01,630 of the sub-string occurs. So the output of using Find 20 00:01:01,630 --> 00:01:04,060 will be the first position in the search string, which 21 00:01:04,060 --> 00:01:07,390 is this blue string right here, where the target string, 22 00:01:07,390 --> 00:01:10,520 which is the purple string, occurs. So that will be 23 00:01:10,520 --> 00:01:14,200 a number. If the target string is not a found anywhere 24 00:01:14,200 --> 00:01:16,760 in the search string, then the output would be negative 25 00:01:16,760 --> 00:01:19,140 1. So let's try a few examples to understand how 26 00:01:19,140 --> 00:01:21,970 that works and we'll do this in the Python interpreter. 27 00:01:21,970 --> 00:01:27,210 Here I've initialized the variable Pythagoras to hold the string here 28 00:01:27,210 --> 00:01:29,360 that's been attributed to Pythagoras. We don't know if 29 00:01:29,360 --> 00:01:32,190 he really said it. But it says there's a geometry 30 00:01:32,190 --> 00:01:34,930 in the humming of strings, there is music in 31 00:01:34,930 --> 00:01:39,100 the spacing of spheres. So now, we have that variable 32 00:01:39,100 --> 00:01:43,710 initialized, so I'm going to invoke Find, using Pythagoras as 33 00:01:43,710 --> 00:01:45,970 the string that we're searching in, and that's the value 34 00:01:45,970 --> 00:01:48,800 that we initialized it to with a string, passing in 35 00:01:48,800 --> 00:01:52,290 as the search string the string string. When we run 36 00:01:52,290 --> 00:01:59,450 this, we see that we get 40 as the result. If we counted, this is position 0, we 37 00:01:59,450 --> 00:02:02,600 would see string starting at position 40. Since I 38 00:02:02,600 --> 00:02:05,110 don't want to count that far, we can use our indexing 39 00:02:05,110 --> 00:02:07,560 to see if that's right. So let's print Pythagoras 40 00:02:09,110 --> 00:02:11,880 starting from index 40, we could print all the 41 00:02:11,880 --> 00:02:14,980 way to the end using a colon. And, when 42 00:02:14,980 --> 00:02:17,870 we run that, we see that it starts with string 43 00:02:17,870 --> 00:02:21,820 which is what we found with the Find. We can search 44 00:02:21,820 --> 00:02:27,380 for other positions if we search for Pythagoras the single letter T. 45 00:02:27,380 --> 00:02:30,515 Well that matches the beginning, so we should find the resulted position 46 00:02:30,515 --> 00:02:34,930 0 which is what we get and we can look for sphere. 47 00:02:34,930 --> 00:02:38,210 [BLANK_AUDIO] 48 00:02:38,210 --> 00:02:41,565 That will match sphere at the end. We get 49 00:02:41,565 --> 00:02:45,410 position 86. Let's print the quote from position 86. And 50 00:02:48,590 --> 00:02:53,130 we see the end of the quote starting from sphere. [SOUND] 51 00:02:53,130 --> 00:02:57,470 If we search for a string that's not in the string 52 00:02:57,470 --> 00:03:00,840 that we're using as the search string, so let's look for 53 00:03:00,840 --> 00:03:06,770 say, algebra, which was not in the quote from Pythagoras, we 54 00:03:06,770 --> 00:03:09,800 get the output negative one. That means the string was not found.