[Script Info] Title: [Events] Format: Layer, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text Dialogue: 0,0:00:00.49,0:00:03.09,Default,,0000,0000,0000,,So I want to introduce one more operation on strings, which Dialogue: 0,0:00:03.09,0:00:05.93,Default,,0000,0000,0000,,we'll find very useful, which is the Find operation. It gives Dialogue: 0,0:00:05.93,0:00:08.98,Default,,0000,0000,0000,,us the way in a big string to find some sub-string Dialogue: 0,0:00:08.98,0:00:12.77,Default,,0000,0000,0000,,that we're looking for. The way we use Find is a little Dialogue: 0,0:00:12.77,0:00:15.91,Default,,0000,0000,0000,,different from the way we've used other operators so far. Because Dialogue: 0,0:00:15.91,0:00:18.32,Default,,0000,0000,0000,,Find is actually a method, and what that means is it's Dialogue: 0,0:00:18.32,0:00:21.43,Default,,0000,0000,0000,,a built in procedure provided by Python. We'll be able to Dialogue: 0,0:00:21.43,0:00:25.86,Default,,0000,0000,0000,,define our own procedures soon, we'll get to that in unit two. Dialogue: 0,0:00:25.86,0:00:29.63,Default,,0000,0000,0000,,Find is a procedure that operates on strings, so we use it by having Dialogue: 0,0:00:29.63,0:00:32.19,Default,,0000,0000,0000,,a string followed by .find, followed by Dialogue: 0,0:00:32.19,0:00:34.50,Default,,0000,0000,0000,,a parentheses, then we pass in another string. Dialogue: 0,0:00:36.57,0:00:38.70,Default,,0000,0000,0000,,Which is the string that we want to find in Dialogue: 0,0:00:38.70,0:00:41.33,Default,,0000,0000,0000,,the first string. And the output of Find is the Dialogue: 0,0:00:41.33,0:00:46.55,Default,,0000,0000,0000,,position in the string where that sub-string is found, the Dialogue: 0,0:00:46.55,0:00:49.72,Default,,0000,0000,0000,,first occurrence of the string. So, if that string happens Dialogue: 0,0:00:49.72,0:00:52.29,Default,,0000,0000,0000,,to occur in more places than one in the Dialogue: 0,0:00:52.29,0:00:54.38,Default,,0000,0000,0000,,input string, the result of find is always going to give Dialogue: 0,0:00:54.38,0:00:57.90,Default,,0000,0000,0000,,us the position. That's the number where the first occurrence Dialogue: 0,0:00:57.90,0:01:01.63,Default,,0000,0000,0000,,of the sub-string occurs. So the output of using Find Dialogue: 0,0:01:01.63,0:01:04.06,Default,,0000,0000,0000,,will be the first position in the search string, which Dialogue: 0,0:01:04.06,0:01:07.39,Default,,0000,0000,0000,,is this blue string right here, where the target string, Dialogue: 0,0:01:07.39,0:01:10.52,Default,,0000,0000,0000,,which is the purple string, occurs. So that will be Dialogue: 0,0:01:10.52,0:01:14.20,Default,,0000,0000,0000,,a number. If the target string is not a found anywhere Dialogue: 0,0:01:14.20,0:01:16.76,Default,,0000,0000,0000,,in the search string, then the output would be negative Dialogue: 0,0:01:16.76,0:01:19.14,Default,,0000,0000,0000,,1. So let's try a few examples to understand how Dialogue: 0,0:01:19.14,0:01:21.97,Default,,0000,0000,0000,,that works and we'll do this in the Python interpreter. Dialogue: 0,0:01:21.97,0:01:27.21,Default,,0000,0000,0000,,Here I've initialized the variable Pythagoras to hold the string here Dialogue: 0,0:01:27.21,0:01:29.36,Default,,0000,0000,0000,,that's been attributed to Pythagoras. We don't know if Dialogue: 0,0:01:29.36,0:01:32.19,Default,,0000,0000,0000,,he really said it. But it says there's a geometry Dialogue: 0,0:01:32.19,0:01:34.93,Default,,0000,0000,0000,,in the humming of strings, there is music in Dialogue: 0,0:01:34.93,0:01:39.10,Default,,0000,0000,0000,,the spacing of spheres. So now, we have that variable Dialogue: 0,0:01:39.10,0:01:43.71,Default,,0000,0000,0000,,initialized, so I'm going to invoke Find, using Pythagoras as Dialogue: 0,0:01:43.71,0:01:45.97,Default,,0000,0000,0000,,the string that we're searching in, and that's the value Dialogue: 0,0:01:45.97,0:01:48.80,Default,,0000,0000,0000,,that we initialized it to with a string, passing in Dialogue: 0,0:01:48.80,0:01:52.29,Default,,0000,0000,0000,,as the search string the string string. When we run Dialogue: 0,0:01:52.29,0:01:59.45,Default,,0000,0000,0000,,this, we see that we get 40 as the result. If we counted, this is position 0, we Dialogue: 0,0:01:59.45,0:02:02.60,Default,,0000,0000,0000,,would see string starting at position 40. Since I Dialogue: 0,0:02:02.60,0:02:05.11,Default,,0000,0000,0000,,don't want to count that far, we can use our indexing Dialogue: 0,0:02:05.11,0:02:07.56,Default,,0000,0000,0000,,to see if that's right. So let's print Pythagoras Dialogue: 0,0:02:09.11,0:02:11.88,Default,,0000,0000,0000,,starting from index 40, we could print all the Dialogue: 0,0:02:11.88,0:02:14.98,Default,,0000,0000,0000,,way to the end using a colon. And, when Dialogue: 0,0:02:14.98,0:02:17.87,Default,,0000,0000,0000,,we run that, we see that it starts with string Dialogue: 0,0:02:17.87,0:02:21.82,Default,,0000,0000,0000,,which is what we found with the Find. We can search Dialogue: 0,0:02:21.82,0:02:27.38,Default,,0000,0000,0000,,for other positions if we search for Pythagoras the single letter T. Dialogue: 0,0:02:27.38,0:02:30.52,Default,,0000,0000,0000,,Well that matches the beginning, so we should find the resulted position Dialogue: 0,0:02:30.52,0:02:34.93,Default,,0000,0000,0000,,0 which is what we get and we can look for sphere. Dialogue: 0,0:02:34.93,0:02:38.21,Default,,0000,0000,0000,,[BLANK_AUDIO] Dialogue: 0,0:02:38.21,0:02:41.56,Default,,0000,0000,0000,,That will match sphere at the end. We get Dialogue: 0,0:02:41.56,0:02:45.41,Default,,0000,0000,0000,,position 86. Let's print the quote from position 86. And Dialogue: 0,0:02:48.59,0:02:53.13,Default,,0000,0000,0000,,we see the end of the quote starting from sphere. [SOUND] Dialogue: 0,0:02:53.13,0:02:57.47,Default,,0000,0000,0000,,If we search for a string that's not in the string Dialogue: 0,0:02:57.47,0:03:00.84,Default,,0000,0000,0000,,that we're using as the search string, so let's look for Dialogue: 0,0:03:00.84,0:03:06.77,Default,,0000,0000,0000,,say, algebra, which was not in the quote from Pythagoras, we Dialogue: 0,0:03:06.77,0:03:09.80,Default,,0000,0000,0000,,get the output negative one. That means the string was not found.