[Script Info] Title: [Events] Format: Layer, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text Dialogue: 0,0:00:00.20,0:00:02.82,Default,,0000,0000,0000,,Hello, and welcome to Chapter Six. Dialogue: 0,0:00:02.82,0:00:05.24,Default,,0000,0000,0000,,This chapter we're going to\Ntalk about strings, and Dialogue: 0,0:00:05.24,0:00:08.61,Default,,0000,0000,0000,,stuff is going to start to get real now. Dialogue: 0,0:00:08.61,0:00:12.61,Default,,0000,0000,0000,,So, as always, this material, this video,\Nthese Dialogue: 0,0:00:12.61,0:00:15.55,Default,,0000,0000,0000,,slides and book are copyright Creative\NCommons Attribution. Dialogue: 0,0:00:15.55,0:00:16.87,Default,,0000,0000,0000,,I want you to use these materials. Dialogue: 0,0:00:16.87,0:00:18.80,Default,,0000,0000,0000,,I want you to, somebody else, I want to Dialogue: 0,0:00:18.80,0:00:21.72,Default,,0000,0000,0000,,make more teachers, so everyone can teach\Nthis stuff. Dialogue: 0,0:00:21.72,0:00:22.79,Default,,0000,0000,0000,,Use it however you like. Dialogue: 0,0:00:24.01,0:00:25.28,Default,,0000,0000,0000,,Okay, so we've been playing with Dialogue: 0,0:00:25.28,0:00:26.73,Default,,0000,0000,0000,,strings from the beginning. Dialogue: 0,0:00:26.73,0:00:28.32,Default,,0000,0000,0000,,I mean, literally, if we didn't work Dialogue: 0,0:00:28.32,0:00:31.04,Default,,0000,0000,0000,,with strings, we could've never printed\NHello World. Dialogue: 0,0:00:31.04,0:00:35.81,Default,,0000,0000,0000,,And, and lord knows, we need to print\NHello World in a programming language. Dialogue: 0,0:00:35.81,0:00:39.61,Default,,0000,0000,0000,,And so, we've been using them, especially\Nconstants. Dialogue: 0,0:00:39.61,0:00:41.65,Default,,0000,0000,0000,,Now, in this chapter, we're going to dig in. Dialogue: 0,0:00:41.65,0:00:46.99,Default,,0000,0000,0000,,So, oops, so a string is a sequence of\Ncharacters. Dialogue: 0,0:00:46.99,0:00:50.41,Default,,0000,0000,0000,,You can use either use single quotes or\Ndouble quotes in Python Dialogue: 0,0:00:50.41,0:00:51.36,Default,,0000,0000,0000,,to delimit a string. Dialogue: 0,0:00:51.36,0:00:54.53,Default,,0000,0000,0000,,And so here's two string constants, Hello\Nand there, Dialogue: 0,0:00:54.53,0:00:58.14,Default,,0000,0000,0000,,and stuck into the variables str1\Nand str2. Dialogue: 0,0:00:58.14,0:01:00.52,Default,,0000,0000,0000,,We can concatenate them together\Nwith a plus sign. Dialogue: 0,0:01:00.52,0:01:03.10,Default,,0000,0000,0000,,Python is smart enough to look and say, Dialogue: 0,0:01:03.10,0:01:05.69,Default,,0000,0000,0000,,oh, those are strings, I know what to\Ndo with those. Dialogue: 0,0:01:05.69,0:01:09.59,Default,,0000,0000,0000,,And you'll notice that the plus doesn't\Nadd any space here, because when Dialogue: 0,0:01:09.59,0:01:13.57,Default,,0000,0000,0000,,we print bob out, Hello and there are right\Nnext to one another. Dialogue: 0,0:01:13.57,0:01:17.01,Default,,0000,0000,0000,,If, for example, we've done some\Nconversions, Dialogue: 0,0:01:17.01,0:01:18.80,Default,,0000,0000,0000,,so when we were, like, reading pay, Dialogue: 0,0:01:18.80,0:01:20.64,Default,,0000,0000,0000,,and rate, and hours, and stuff,\Nwe've done some conversions. Dialogue: 0,0:01:20.64,0:01:23.38,Default,,0000,0000,0000,,So this is an example of the,\Na string 1 2 3 Dialogue: 0,0:01:23.38,0:01:27.21,Default,,0000,0000,0000,,Not 123, but the string, quote 1 2 3\Nquote. Dialogue: 0,0:01:27.21,0:01:29.27,Default,,0000,0000,0000,,And we can't add 1 to this, we get Dialogue: 0,0:01:29.27,0:01:32.91,Default,,0000,0000,0000,,a traceback, kind of, at this point, as we\Nexpected. Dialogue: 0,0:01:32.91,0:01:37.02,Default,,0000,0000,0000,,And we would convert that to an integer\Nusing the int function that's built in. Dialogue: 0,0:01:37.02,0:01:39.90,Default,,0000,0000,0000,,See how much Python you already know?\NI mean, this is awesome, right? Dialogue: 0,0:01:39.90,0:01:40.97,Default,,0000,0000,0000,,I can just say, Dialogue: 0,0:01:40.97,0:01:42.80,Default,,0000,0000,0000,,oh, you call the int function,\Nand you know what that is. Dialogue: 0,0:01:42.80,0:01:46.22,Default,,0000,0000,0000,,That's, sorry, sorry, I'm just\Nawesomed out. Dialogue: 0,0:01:46.22,0:01:50.80,Default,,0000,0000,0000,,So you convert this to an integer, and\Nthen you add 1 to it, and then we get 124. Dialogue: 0,0:01:50.80,0:01:52.27,Default,,0000,0000,0000,,So, there you go. Dialogue: 0,0:01:52.27,0:01:54.74,Default,,0000,0000,0000,,We've been doing strings all along, had to. Dialogue: 0,0:01:54.74,0:01:57.00,Default,,0000,0000,0000,,I mean, literally, strings and numeric data Dialogue: 0,0:01:57.00,0:01:59.93,Default,,0000,0000,0000,,are the two things that programs deal with. Dialogue: 0,0:01:59.93,0:02:03.12,Default,,0000,0000,0000,,So, we've been reading and converting. Dialogue: 0,0:02:03.12,0:02:05.18,Default,,0000,0000,0000,,Again, this is sort of a pattern from some\Nof the earlier programs Dialogue: 0,0:02:05.18,0:02:08.66,Default,,0000,0000,0000,,where we do a raw input, you know? Dialogue: 0,0:02:08.66,0:02:10.89,Default,,0000,0000,0000,,And the raw input just takes a string and\Nputs it in a variable. Dialogue: 0,0:02:10.89,0:02:14.56,Default,,0000,0000,0000,,So if I take Chuck, then the\Nvariable contains the string C-h-u-c-k. Dialogue: 0,0:02:15.99,0:02:18.97,Default,,0000,0000,0000,,Even if we type numbers, that is a string. Dialogue: 0,0:02:18.97,0:02:23.66,Default,,0000,0000,0000,,We can't, just because I put 1 0 0 in,\NI still can't subtract 10. Dialogue: 0,0:02:23.66,0:02:28.27,Default,,0000,0000,0000,,We get a happy little traceback, oh, happy\Nlittle, sad-faced traceback. Dialogue: 0,0:02:28.27,0:02:31.29,Default,,0000,0000,0000,,And, and, but of course, if we convert it Dialogue: 0,0:02:31.29,0:02:34.05,Default,,0000,0000,0000,,into float or something like that. Dialogue: 0,0:02:35.19,0:02:38.67,Default,,0000,0000,0000,,We convert int or float, we can do that\Nand subtract 10, and we can do that. Dialogue: 0,0:02:38.67,0:02:41.68,Default,,0000,0000,0000,,So, so we've been doing this for a while. Dialogue: 0,0:02:41.68,0:02:45.13,Default,,0000,0000,0000,,We've been doing strings and manipulating\Nstrings and converting strings all along. Dialogue: 0,0:02:45.13,0:02:49.05,Default,,0000,0000,0000,,So the thing we're going to start doing\Nnow is we're going to dive into strings. Dialogue: 0,0:02:49.05,0:02:53.10,Default,,0000,0000,0000,,We realize that strings are addressable at\Na character-by-character basis, Dialogue: 0,0:02:53.10,0:02:56.35,Default,,0000,0000,0000,,and we can do all kind of cool\Nthings with that. Dialogue: 0,0:02:56.35,0:02:59.100,Default,,0000,0000,0000,,And so, a string is a sequence of\Ncharacters, and we Dialogue: 0,0:02:59.100,0:03:04.45,Default,,0000,0000,0000,,can look inside them using what we call\Nthe index operator, Dialogue: 0,0:03:04.45,0:03:06.72,Default,,0000,0000,0000,,the square brackets. And we've seen\Nsquare brackets in Dialogue: 0,0:03:06.72,0:03:08.23,Default,,0000,0000,0000,,lists, and you'll see that there's sort of Dialogue: 0,0:03:08.23,0:03:11.61,Default,,0000,0000,0000,,similarities between lists of numbers,\Nand, in effect, a Dialogue: 0,0:03:11.61,0:03:14.35,Default,,0000,0000,0000,,string is a special kind of list of\Ncharacters. Dialogue: 0,0:03:14.35,0:03:17.20,Default,,0000,0000,0000,,So if we take this string banana, Dialogue: 0,0:03:17.20,0:03:21.24,Default,,0000,0000,0000,,the string banana starts, the first\Ncharacter starts at 0. Dialogue: 0,0:03:21.24,0:03:24.89,Default,,0000,0000,0000,,So, we call this operator sub, so \Nletter equals Dialogue: 0,0:03:24.89,0:03:28.38,Default,,0000,0000,0000,,fruit sub 1 and that is the second\Ncharacter. Dialogue: 0,0:03:28.38,0:03:30.60,Default,,0000,0000,0000,,Now this may seem a little weird that the\Nfirst character Dialogue: 0,0:03:30.60,0:03:33.96,Default,,0000,0000,0000,,is a 0 and the second character is a 1. Dialogue: 0,0:03:33.96,0:03:38.50,Default,,0000,0000,0000,,It actually is kind of similar to the old\Nelevator thing, where in Europe they're Dialogue: 0,0:03:38.50,0:03:41.12,Default,,0000,0000,0000,,called, the first floor is zero, then\Nnegative one, Dialogue: 0,0:03:41.12,0:03:43.56,Default,,0000,0000,0000,,and the second floor is one, right? Dialogue: 0,0:03:43.56,0:03:46.09,Default,,0000,0000,0000,,It's kind of the same thing.\NActually, it turns out that Dialogue: 0,0:03:46.09,0:03:50.46,Default,,0000,0000,0000,,internally zero was a better way\Nto start than one. Dialogue: 0,0:03:50.46,0:03:54.16,Default,,0000,0000,0000,,It, you'll get used to it and then after\Na while there's Dialogue: 0,0:03:54.16,0:03:58.54,Default,,0000,0000,0000,,some little cool advantages to it, but for\Nnow, beginning is zero. Dialogue: 0,0:03:58.54,0:04:01.94,Default,,0000,0000,0000,,Just, beginning is zero, it is the rule,\Njust remember it. Dialogue: 0,0:04:02.97,0:04:08.79,Default,,0000,0000,0000,,Okay, so the 0 is b, the 1 is a, the 2 is\Nn, et cetera, et cetera. Dialogue: 0,0:04:08.79,0:04:11.16,Default,,0000,0000,0000,,And we call this syntax Dialogue: 0,0:04:11.16,0:04:12.54,Default,,0000,0000,0000,,fruit sub 1, okay? Dialogue: 0,0:04:12.54,0:04:17.12,Default,,0000,0000,0000,,So that is the sub 1 character of fruit,\Nand then that is an a. Dialogue: 0,0:04:17.12,0:04:21.25,Default,,0000,0000,0000,,So that fruit sub 1 says, look up in\Nbanana, find the 1 position, Dialogue: 0,0:04:21.25,0:04:25.87,Default,,0000,0000,0000,,and give me what's in that 1\Nposition, that's what's the sub. Dialogue: 0,0:04:25.87,0:04:29.57,Default,,0000,0000,0000,,And what's inside these brackets can be\Nan expression. Dialogue: 0,0:04:29.57,0:04:33.69,Default,,0000,0000,0000,,So if we set n to 3, n minus 1, well\Nthat'll compute to 2. Dialogue: 0,0:04:33.69,0:04:36.66,Default,,0000,0000,0000,,And then fruit sub 2 is the letter n, Dialogue: 0,0:04:36.66,0:04:39.98,Default,,0000,0000,0000,,right? So that's fruit sub 2, okay? Dialogue: 0,0:04:39.98,0:04:42.32,Default,,0000,0000,0000,,It's the third character, fruit sub 2. Dialogue: 0,0:04:42.32,0:04:47.34,Default,,0000,0000,0000,,So the index starts at 0, the, we read the\Nbrackets as sub, fruit sub 1, Dialogue: 0,0:04:47.34,0:04:52.75,Default,,0000,0000,0000,,fruit sub 2. Now, Python will Dialogue: 0,0:04:52.75,0:04:57.86,Default,,0000,0000,0000,,complain to you if you use this sub\Noperator too far down the string. Dialogue: 0,0:04:57.86,0:05:01.32,Default,,0000,0000,0000,,Here is a character with 3, which\Nis 0, 1, and 2. Dialogue: 0,0:05:01.32,0:05:05.42,Default,,0000,0000,0000,,And if we go to sub 5, it blows up. Dialogue: 0,0:05:05.42,0:05:10.26,Default,,0000,0000,0000,,Now, you know, by now I hope that you're\Nnot freaking out about traceback errors. Dialogue: 0,0:05:10.26,0:05:14.07,Default,,0000,0000,0000,,Remember, traceback errors are just Python\Ntrying to inform you. Dialogue: 0,0:05:14.07,0:05:18.93,Default,,0000,0000,0000,,And if we just stop looking at that as\Nmean Python face, and Dialogue: 0,0:05:18.93,0:05:24.19,Default,,0000,0000,0000,,instead look at that as, oh, index error,\Nstring index out of range. Dialogue: 0,0:05:24.19,0:05:27.36,Default,,0000,0000,0000,,Oh yeah, I stuck a five in there and\Nthere's only three, oh, Dialogue: 0,0:05:27.36,0:05:31.33,Default,,0000,0000,0000,,my bad, thank you, Python, appreciate it,\Nthanks for the help. Dialogue: 0,0:05:31.33,0:05:34.87,Default,,0000,0000,0000,,So, think of this as like, it's not a\Nsmiley face Dialogue: 0,0:05:34.87,0:05:38.69,Default,,0000,0000,0000,,but it's kind of like a, a quizzical face,\Nright, it's like [SOUND]. Dialogue: 0,0:05:38.69,0:05:39.66,Default,,0000,0000,0000,,I don't know. Dialogue: 0,0:05:39.66,0:05:42.95,Default,,0000,0000,0000,,Python's confused and it's trying to tell\Nyou what it's confused, okay? Dialogue: 0,0:05:42.95,0:05:46.78,Default,,0000,0000,0000,,So don't look at these as sad faces.\NPython doesn't hate you, Python loves you. Dialogue: 0,0:05:48.17,0:05:52.42,Default,,0000,0000,0000,,And loves me too.\NSo, strings have individual Dialogue: 0,0:05:52.42,0:05:54.42,Default,,0000,0000,0000,,characters that we can address with the\Nindex operator. Dialogue: 0,0:05:54.42,0:05:56.16,Default,,0000,0000,0000,,They also have length. Dialogue: 0,0:05:56.16,0:06:00.40,Default,,0000,0000,0000,,And there is a built-in function called\Nlen, that we can call and pass in Dialogue: 0,0:06:00.40,0:06:03.98,Default,,0000,0000,0000,,as a parameter the variable or a\Nconstant, Dialogue: 0,0:06:03.98,0:06:05.94,Default,,0000,0000,0000,,and it will tell us how many characters. Dialogue: 0,0:06:05.94,0:06:10.04,Default,,0000,0000,0000,,Now this banana has six characters in it\Nthat are 0 through 5. Dialogue: 0,0:06:10.04,0:06:12.52,Default,,0000,0000,0000,,So don't get a little confused, the last\Ncharacter is Dialogue: 0,0:06:12.52,0:06:15.75,Default,,0000,0000,0000,,the fifth, is sub 5, but it's also the\Nsixth character. Dialogue: 0,0:06:15.75,0:06:17.45,Default,,0000,0000,0000,,So the length is really the length, it's Dialogue: 0,0:06:17.45,0:06:22.15,Default,,0000,0000,0000,,not length minus 1, okay?\NSo len is like a built-in function. Dialogue: 0,0:06:22.15,0:06:23.84,Default,,0000,0000,0000,,It's not a function we have to write, Dialogue: 0,0:06:23.84,0:06:26.57,Default,,0000,0000,0000,,as we talked in chapter the functions\Nchapter. Dialogue: 0,0:06:26.57,0:06:28.63,Default,,0000,0000,0000,,There are things that are part of Python\Nthat are just sitting there. Dialogue: 0,0:06:28.63,0:06:31.17,Default,,0000,0000,0000,,And so we are passing banana, the\Nvariable Dialogue: 0,0:06:31.17,0:06:35.01,Default,,0000,0000,0000,,fruit, into function, we're passing it\Ninto function. Dialogue: 0,0:06:35.01,0:06:36.59,Default,,0000,0000,0000,,And then, into the len function. Dialogue: 0,0:06:36.59,0:06:42.25,Default,,0000,0000,0000,,Then len [SOUND] does magic stuff.\NAnd then out comes the answer. Dialogue: 0,0:06:42.25,0:06:48.32,Default,,0000,0000,0000,,And that 6 replaces this and then the 6 goes\Ninto the variable x, and so x is 6. Dialogue: 0,0:06:48.32,0:06:51.07,Default,,0000,0000,0000,,I sure made that a messy looking slide. Dialogue: 0,0:06:51.07,0:06:55.08,Default,,0000,0000,0000,,And so, think of inside the len function,\Nthere's a def. Dialogue: 0,0:06:55.08,0:06:59.89,Default,,0000,0000,0000,,len takes a parameter, does some loopy\Nthings, and it does its thing. Dialogue: 0,0:06:59.89,0:07:02.35,Default,,0000,0000,0000,,So, it's a function that we might write\Nexcept we don't Dialogue: 0,0:07:02.35,0:07:07.16,Default,,0000,0000,0000,,have to because it's already written and\Nbuilt in to Python. Dialogue: 0,0:07:07.16,0:07:10.38,Default,,0000,0000,0000,,Okay. So that's the length of the Dialogue: 0,0:07:10.38,0:07:12.46,Default,,0000,0000,0000,,string, that's getting it individual\Ncharacters. Dialogue: 0,0:07:12.46,0:07:15.55,Default,,0000,0000,0000,,We can also loop through strings. Dialogue: 0,0:07:15.55,0:07:18.71,Default,,0000,0000,0000,,Obviously, if we can use the index\Noperator, and we Dialogue: 0,0:07:18.71,0:07:21.97,Default,,0000,0000,0000,,can put a variable in there, we can\Nwrite a loop. Dialogue: 0,0:07:21.97,0:07:23.52,Default,,0000,0000,0000,,This is an indefinite loop. Dialogue: 0,0:07:23.52,0:07:27.14,Default,,0000,0000,0000,,So we have this variable fruit, has the\Nstring banana in it. Dialogue: 0,0:07:27.14,0:07:29.58,Default,,0000,0000,0000,,We set the variable index to 0. Dialogue: 0,0:07:29.58,0:07:32.92,Default,,0000,0000,0000,,We make a little while loop.\NAnd we ask, Dialogue: 0,0:07:32.92,0:07:35.46,Default,,0000,0000,0000,,as long as index is less than the length\Nof fruit. Dialogue: 0,0:07:35.46,0:07:37.51,Default,,0000,0000,0000,,Now remember, the length of fruit is\Ngoing to be 6. Dialogue: 0,0:07:37.51,0:07:39.52,Default,,0000,0000,0000,,But we don't want to make that less than\Nor equal to Dialogue: 0,0:07:39.52,0:07:43.63,Default,,0000,0000,0000,,because then we would crash, because\Nthe last character is 5. Dialogue: 0,0:07:43.63,0:07:46.44,Default,,0000,0000,0000,,We can say letter is equal to fruit sub\Nindex, so that's going to Dialogue: 0,0:07:46.44,0:07:50.04,Default,,0000,0000,0000,,start out being index of, is going to be\N0, so that's fruit sub 0. Dialogue: 0,0:07:50.04,0:07:53.30,Default,,0000,0000,0000,,Then we print index and letter, so that\Nmeans the Dialogue: 0,0:07:53.30,0:07:56.22,Default,,0000,0000,0000,,first time through the loop we're\Ngoing to see 0 b. Dialogue: 0,0:07:56.22,0:07:58.06,Default,,0000,0000,0000,,Then we increment our Dialogue: 0,0:07:58.06,0:08:04.45,Default,,0000,0000,0000,,iteration operator, and go up.\NAnd then we come out with 1 a. Dialogue: 0,0:08:04.45,0:08:13.56,Default,,0000,0000,0000,,And index advances until index is 6, but\Nhas printed out each of the letters. Dialogue: 0,0:08:13.56,0:08:15.79,Default,,0000,0000,0000,,Now, we're not doing this just to Dialogue: 0,0:08:15.79,0:08:18.62,Default,,0000,0000,0000,,print them out, we will do something\Na little more valuable, Dialogue: 0,0:08:21.54,0:08:23.15,Default,,0000,0000,0000,,valuable inside that loop. Dialogue: 0,0:08:23.15,0:08:28.74,Default,,0000,0000,0000,,But this gives the sense that we can work\Nthrough a loop just like we, we, Dialogue: 0,0:08:28.74,0:08:35.78,Default,,0000,0000,0000,,we can work through a string just like\Nwe work through a list of numbers, okay? Dialogue: 0,0:08:35.78,0:08:38.63,Default,,0000,0000,0000,,Now, that was how you do it with an\Nindefinite loop. Dialogue: 0,0:08:38.63,0:08:42.87,Default,,0000,0000,0000,,In a definite loop, it's just far more\Nawesome, okay? Dialogue: 0,0:08:42.87,0:08:44.88,Default,,0000,0000,0000,,Just like we did with the list of numbers, Dialogue: 0,0:08:46.11,0:08:49.32,Default,,0000,0000,0000,,Python understands strings and allows us\Nto write Dialogue: 0,0:08:49.32,0:08:53.41,Default,,0000,0000,0000,,for loops, using for and in, that go through\Nthe strings. Dialogue: 0,0:08:53.41,0:08:56.91,Default,,0000,0000,0000,,So basically, for letter in fruit, now\Nremember, I'm using letter as a Dialogue: 0,0:08:56.91,0:09:01.22,Default,,0000,0000,0000,,mnemonic variable here, it's just a\Nchoice, a wise choice of a variable name. Dialogue: 0,0:09:01.22,0:09:05.68,Default,,0000,0000,0000,,So that says, run this little block of\Ntext once for Dialogue: 0,0:09:05.68,0:09:08.20,Default,,0000,0000,0000,,each letter in the variable fruit, which\Nmeans that letter's going to Dialogue: 0,0:09:08.20,0:09:13.96,Default,,0000,0000,0000,,take on the successive b-a-n-a-n-a. Dialogue: 0,0:09:13.96,0:09:16.08,Default,,0000,0000,0000,,When I look at that I always worry that I\Nmisspelled it. Dialogue: 0,0:09:16.08,0:09:18.92,Default,,0000,0000,0000,,I think I got these right. Dialogue: 0,0:09:18.92,0:09:22.42,Default,,0000,0000,0000,,If I rewrite this book, I'm not going to\Nuse banana as the example because I'm Dialogue: 0,0:09:22.42,0:09:24.65,Default,,0000,0000,0000,,terrified that I misspelled banana,\Nbecause I don't Dialogue: 0,0:09:24.65,0:09:27.19,Default,,0000,0000,0000,,know how many n's banana has in it. Dialogue: 0,0:09:27.19,0:09:32.28,Default,,0000,0000,0000,,But, be that as it may, we are\Nabstracting, we are letting Python say, Dialogue: 0,0:09:32.28,0:09:36.30,Default,,0000,0000,0000,,run this little block of text once, in\Norder, for each of the letters in Dialogue: 0,0:09:36.30,0:09:40.99,Default,,0000,0000,0000,,the variable fruit, which is b-a-n-a, and\Nso it prints out each of the letters. Dialogue: 0,0:09:40.99,0:09:46.11,Default,,0000,0000,0000,,So this is a much prettier version of the,\Nthe looping, Dialogue: 0,0:09:46.11,0:09:50.69,Default,,0000,0000,0000,,so using the definite, the for keyword\Ninstead of the while keyword. Dialogue: 0,0:09:50.69,0:09:54.06,Default,,0000,0000,0000,,And so, we can just kind of compare these\Ntwo things. Dialogue: 0,0:09:54.06,0:09:55.57,Default,,0000,0000,0000,,They kind of do the exact same thing. Dialogue: 0,0:09:55.57,0:09:57.68,Default,,0000,0000,0000,,And it also is kind of a, gives you a Dialogue: 0,0:09:57.68,0:10:01.12,Default,,0000,0000,0000,,sense of what the for is doing for us,\Nright? Dialogue: 0,0:10:01.12,0:10:01.53,Default,,0000,0000,0000,,The for is Dialogue: 0,0:10:01.53,0:10:05.10,Default,,0000,0000,0000,,setting up this index, the for is\Nlooking up Dialogue: 0,0:10:05.10,0:10:07.89,Default,,0000,0000,0000,,inside of fruit, and the for is advancing\Nthe index. Dialogue: 0,0:10:07.89,0:10:10.22,Default,,0000,0000,0000,,So the for's doing a bunch of work for us Dialogue: 0,0:10:10.22,0:10:12.39,Default,,0000,0000,0000,,and I've characterized that, sort of, in\Nthe previous lecture. Dialogue: 0,0:10:12.39,0:10:14.89,Default,,0000,0000,0000,,How the for is sort of doing a bunch of\Nthings for us Dialogue: 0,0:10:14.89,0:10:19.51,Default,,0000,0000,0000,,and that's, it allows our code to\Nbe more Dialogue: 0,0:10:19.51,0:10:22.50,Default,,0000,0000,0000,,expressive and, and instead of, so this\Nis, a lot of Dialogue: 0,0:10:22.50,0:10:26.50,Default,,0000,0000,0000,,this is just kind of bookkeeping crap that\Nwe don't really need. Dialogue: 0,0:10:26.50,0:10:29.58,Default,,0000,0000,0000,,And so the for loop helps us by doing some\Nof the bookkeeping for us. Dialogue: 0,0:10:31.92,0:10:34.96,Default,,0000,0000,0000,,Okay, so we can do all those loops again. Dialogue: 0,0:10:34.96,0:10:38.76,Default,,0000,0000,0000,,We can find the largest letter, the\Nsmallest letter, the, how many times. Dialogue: 0,0:10:38.76,0:10:45.39,Default,,0000,0000,0000,,So, I think, what, how many n's are in\Nthis, or how many a's are in this. Dialogue: 0,0:10:45.39,0:10:49.69,Default,,0000,0000,0000,,So this is a simple counting pattern and,\Nand a looking pattern. Dialogue: 0,0:10:49.69,0:10:52.72,Default,,0000,0000,0000,,And so, our word is banana, our count is 0. Dialogue: 0,0:10:52.72,0:10:54.98,Default,,0000,0000,0000,,For the letter in word, again, boop, boop, Dialogue: 0,0:10:54.98,0:10:56.94,Default,,0000,0000,0000,,boop, boop, boop, that comes out like that. Dialogue: 0,0:10:56.94,0:11:01.32,Default,,0000,0000,0000,,So it's going to run this little block.\NIf the letter is a, add 1 to the count. Dialogue: 0,0:11:02.33,0:11:07.58,Default,,0000,0000,0000,,So this is going to basically print out at\Nthe end the number of a's in banana. Dialogue: 0,0:11:07.58,0:11:10.36,Default,,0000,0000,0000,,It would probably be more useful, for me,\Nto print out the number Dialogue: 0,0:11:10.36,0:11:13.91,Default,,0000,0000,0000,,of n's in banana, because I never know how\Nmany n's are in banana. Dialogue: 0,0:11:13.91,0:11:15.48,Default,,0000,0000,0000,,But it looks like there's supposed to be two, Dialogue: 0,0:11:15.48,0:11:17.44,Default,,0000,0000,0000,,or otherwise I have a typo on this slide. Dialogue: 0,0:11:18.79,0:11:21.23,Default,,0000,0000,0000,,So the in, again, I, I love the in. Dialogue: 0,0:11:21.23,0:11:22.12,Default,,0000,0000,0000,,I just absolutely Dialogue: 0,0:11:22.12,0:11:24.70,Default,,0000,0000,0000,,love this in.\NI love this syntax. Dialogue: 0,0:11:24.70,0:11:30.76,Default,,0000,0000,0000,,This for each letter in the word banana.\NJust, to me, it reads very smoothly. Dialogue: 0,0:11:30.76,0:11:33.25,Default,,0000,0000,0000,,Cognitively, it fits in my mind what's\Ngoing on. Dialogue: 0,0:11:33.25,0:11:37.11,Default,,0000,0000,0000,,For each letter in banana, run this little\Nindented block of text. Dialogue: 0,0:11:37.11,0:11:42.99,Default,,0000,0000,0000,,Again, very pretty, I love in, it's one of\Nmy favorite little pieces of Python. Dialogue: 0,0:11:46.49,0:11:49.43,Default,,0000,0000,0000,,So, again, with the for, it takes care of Dialogue: 0,0:11:49.43,0:11:52.42,Default,,0000,0000,0000,,all the iteration variables for us, and it\Ngoes through the sequence. Dialogue: 0,0:11:52.42,0:11:54.85,Default,,0000,0000,0000,,And so here's, here's an animation, right? Dialogue: 0,0:11:54.85,0:11:57.91,Default,,0000,0000,0000,,Remember that the for is going to do all\Nthis work for us, right? Dialogue: 0,0:11:57.91,0:12:00.71,Default,,0000,0000,0000,,Letter is going to advance through the Dialogue: 0,0:12:00.71,0:12:04.72,Default,,0000,0000,0000,,successive values, the successive letters\Nin banana. Dialogue: 0,0:12:04.72,0:12:12.09,Default,,0000,0000,0000,,So letter is being moved for us by the for\Nstatement, okay? Dialogue: 0,0:12:12.09,0:12:14.64,Default,,0000,0000,0000,,So that's looping through. Dialogue: 0,0:12:14.64,0:12:16.66,Default,,0000,0000,0000,,Now it turns out there's a lot of\Ncommon things that Dialogue: 0,0:12:16.66,0:12:18.73,Default,,0000,0000,0000,,we want to do that are already built into\NPython for us. Dialogue: 0,0:12:20.10,0:12:24.49,Default,,0000,0000,0000,,Clear the screen there.\NWe call these slicing. Dialogue: 0,0:12:24.49,0:12:28.87,Default,,0000,0000,0000,,So the index operator looks up various\Nthings in a string, but we Dialogue: 0,0:12:28.87,0:12:33.47,Default,,0000,0000,0000,,can also pull substrings out, using the\Ncolon in addition to the square brackets. Dialogue: 0,0:12:33.47,0:12:35.02,Default,,0000,0000,0000,,Again, this is called slicing. Dialogue: 0,0:12:36.35,0:12:37.20,Default,,0000,0000,0000,,So the Dialogue: 0,0:12:37.20,0:12:43.01,Default,,0000,0000,0000,,colon operator, basically, takes a\Nstarting position, and then an ending Dialogue: 0,0:12:43.01,0:12:47.80,Default,,0000,0000,0000,,position, but the ending position is up to\Nbut not including the second one. Dialogue: 0,0:12:47.80,0:12:51.66,Default,,0000,0000,0000,,So this is, it's up to but not including,\Nup to but not including. Dialogue: 0,0:12:51.66,0:12:54.41,Default,,0000,0000,0000,,Just like the zero, you get used to it\Npretty quick, Dialogue: 0,0:12:54.41,0:12:56.02,Default,,0000,0000,0000,,but the first time you see it, it's a\Nlittle bit Dialogue: 0,0:12:58.24,0:12:59.22,Default,,0000,0000,0000,,wonky. Dialogue: 0,0:12:59.22,0:13:03.48,Default,,0000,0000,0000,,So, if we're going 0 through 4, that's how\NI read this print, s sub 0 Dialogue: 0,0:13:03.48,0:13:08.96,Default,,0000,0000,0000,,through 4, or, or better, better said,\Ns 0, up to but not including 4. Dialogue: 0,0:13:08.96,0:13:14.16,Default,,0000,0000,0000,,That is, print me out the chunk that is up\Nto, but not including, 4. Dialogue: 0,0:13:14.16,0:13:18.51,Default,,0000,0000,0000,,So, it doesn't include 4, and so out comes\NMont, right? Dialogue: 0,0:13:19.63,0:13:23.32,Default,,0000,0000,0000,,So the next one is 6 up to but not\Nincluding 7, so it starts at 6, Dialogue: 0,0:13:23.32,0:13:30.01,Default,,0000,0000,0000,,up to but not including 7, so\Nout comes the P. Dialogue: 0,0:13:30.01,0:13:32.08,Default,,0000,0000,0000,,And, even though you might expect that it Dialogue: 0,0:13:32.08,0:13:35.77,Default,,0000,0000,0000,,would traceback on this, Python is a\Nlittle forgiving. Dialogue: 0,0:13:35.77,0:13:37.31,Default,,0000,0000,0000,,So here's a moment where Python is a\Nlittle Dialogue: 0,0:13:37.31,0:13:40.17,Default,,0000,0000,0000,,forgiving, saying, you know, I'll give you\Na break here. Dialogue: 0,0:13:40.17,0:13:42.63,Default,,0000,0000,0000,,If you go 6, but up to, but not including 20, Dialogue: 0,0:13:42.63,0:13:45.51,Default,,0000,0000,0000,,I'll just stop at the end of the string. Dialogue: 0,0:13:45.51,0:13:48.70,Default,,0000,0000,0000,,So it's 6 to the end, so it, it, you can\Nover-reference here and Dialogue: 0,0:13:48.70,0:13:51.53,Default,,0000,0000,0000,,you can not, you won't get yourself in\Ntrouble. Dialogue: 0,0:13:51.53,0:13:53.28,Default,,0000,0000,0000,,So that comes out, Python. Dialogue: 0,0:13:53.28,0:13:57.68,Default,,0000,0000,0000,,So, again, the second character is\Nup to but not including, Dialogue: 0,0:13:57.68,0:13:59.81,Default,,0000,0000,0000,,and that's the, kind of the\Nweird thing there. Dialogue: 0,0:13:59.81,0:14:01.54,Default,,0000,0000,0000,,Of course once you remember that\Nthe first character Dialogue: 0,0:14:01.54,0:14:04.59,Default,,0000,0000,0000,,is 0, 0 up through but not including.\NNice. Dialogue: 0,0:14:08.57,0:14:12.38,Default,,0000,0000,0000,,If we leave off the first or the last\Nnumber, leaving off the first number, the Dialogue: 0,0:14:12.38,0:14:17.10,Default,,0000,0000,0000,,last number and both of them, they mean\Nthe beginning and end of the string, Dialogue: 0,0:14:17.10,0:14:23.86,Default,,0000,0000,0000,,respectively.\NAnd so, up to but not including 2 is M-o. Dialogue: 0,0:14:23.86,0:14:30.66,Default,,0000,0000,0000,,8 colon means starting at 8 to the end of\Nthe string. Dialogue: 0,0:14:30.66,0:14:33.73,Default,,0000,0000,0000,,So that's, thon.\NAnd then, that means Dialogue: 0,0:14:33.73,0:14:36.97,Default,,0000,0000,0000,,the beginning to the end, and so it's\Njust the whole string, Monty Python. Dialogue: 0,0:14:38.11,0:14:39.83,Default,,0000,0000,0000,,Now we've already played with string Dialogue: 0,0:14:39.83,0:14:43.01,Default,,0000,0000,0000,,concatenation, just a thing to\Nemphasize here is, Dialogue: 0,0:14:43.01,0:14:48.74,Default,,0000,0000,0000,,the concatenation operator does not\Nadd a space, does not add a space. Dialogue: 0,0:14:48.74,0:14:51.95,Default,,0000,0000,0000,,If you want a space, you explicitly add it. Dialogue: 0,0:14:51.95,0:14:55.74,Default,,0000,0000,0000,,So here there's no space in between the o\Nand the t, but here Dialogue: 0,0:14:55.74,0:14:59.69,Default,,0000,0000,0000,,there is a space between the o and the t\Nbecause we explicitly added it. Dialogue: 0,0:14:59.69,0:15:02.28,Default,,0000,0000,0000,,So you can concatenate more than one\Nthing. Dialogue: 0,0:15:02.28,0:15:05.36,Default,,0000,0000,0000,,And you add your spaces as you want,\Nokay? Dialogue: 0,0:15:08.00,0:15:10.49,Default,,0000,0000,0000,,Another thing you can do is you can ask\Nquestions about a string. Dialogue: 0,0:15:10.49,0:15:14.52,Default,,0000,0000,0000,,Sort of like doing a string lookup, using\Nthe in operator. Dialogue: 0,0:15:14.52,0:15:17.79,Default,,0000,0000,0000,,This is a little different than how we use\Nit inside of a for loop. Dialogue: 0,0:15:17.79,0:15:20.69,Default,,0000,0000,0000,,This is a logical operation asking a\Nquestion Dialogue: 0,0:15:20.69,0:15:23.22,Default,,0000,0000,0000,,like less than or greater than or\Nwhatever. Dialogue: 0,0:15:23.22,0:15:25.10,Default,,0000,0000,0000,,So, here's an expression. Dialogue: 0,0:15:25.10,0:15:28.67,Default,,0000,0000,0000,,So fruit is banana, as always.\NIs n in fruit? Dialogue: 0,0:15:30.25,0:15:33.02,Default,,0000,0000,0000,,And the answer is yes it is, True.\NSo this Dialogue: 0,0:15:33.02,0:15:35.05,Default,,0000,0000,0000,,is a logical operation.\NIt's a question. Dialogue: 0,0:15:35.05,0:15:36.62,Default,,0000,0000,0000,,It's a true or false. Dialogue: 0,0:15:36.62,0:15:39.83,Default,,0000,0000,0000,,Is m in fruit?\NNo, False. Dialogue: 0,0:15:39.83,0:15:42.50,Default,,0000,0000,0000,,And you can, this can be a string, not\Njust a single character. Dialogue: 0,0:15:42.50,0:15:45.26,Default,,0000,0000,0000,,Is n-a-n in fruit?\NThe answer is True. Dialogue: 0,0:15:45.26,0:15:50.25,Default,,0000,0000,0000,,And you can put, sort of, you know, if,\Nparts of ifs, et cetera, et cetera. Dialogue: 0,0:15:50.25,0:15:53.50,Default,,0000,0000,0000,,So, this is a logical expression that can\Nbe on an if, Dialogue: 0,0:15:53.50,0:15:57.10,Default,,0000,0000,0000,,you can have a while, et cetera, et\Ncetera, et cetera. Dialogue: 0,0:15:57.10,0:15:58.41,Default,,0000,0000,0000,,So it's a logical, Dialogue: 0,0:15:58.41,0:16:00.67,Default,,0000,0000,0000,,logical expression and it returns\NTrue or False. Dialogue: 0,0:16:03.54,0:16:05.56,Default,,0000,0000,0000,,You can also do comparisons. Dialogue: 0,0:16:05.56,0:16:11.19,Default,,0000,0000,0000,,Now, the comparison operations, equals\Nmakes a lot of sense, less Dialogue: 0,0:16:11.19,0:16:15.45,Default,,0000,0000,0000,,than and greater than depend on the\Nlanguage that you're using Python. Dialogue: 0,0:16:15.45,0:16:20.20,Default,,0000,0000,0000,,And so, if you're using, like, a Latin\Ncharacter set, then alphabetical matters. Dialogue: 0,0:16:20.20,0:16:22.48,Default,,0000,0000,0000,,You know, the, the way the Latin character\Nset would do. Dialogue: 0,0:16:22.48,0:16:24.38,Default,,0000,0000,0000,,But if you're in a different character\Nset, Python is Dialogue: 0,0:16:24.38,0:16:28.89,Default,,0000,0000,0000,,aware of multiple character sets and will\Nsort strings based on Dialogue: 0,0:16:28.89,0:16:32.05,Default,,0000,0000,0000,,the sorting algorithm of the particular\Ncharacter set. Dialogue: 0,0:16:33.16,0:16:37.61,Default,,0000,0000,0000,,So you can do comparisons like equality,\Nless than, and greater than. Dialogue: 0,0:16:37.61,0:16:39.83,Default,,0000,0000,0000,,And we've seen some of these things in\Nprevious lectures, actually. Dialogue: 0,0:16:39.83,0:16:40.65,Default,,0000,0000,0000,,We've had to use them. Dialogue: 0,0:16:42.08,0:16:47.12,Default,,0000,0000,0000,,So in addition, to, sort of, these sort of\Nfundamental operations that we Dialogue: 0,0:16:47.12,0:16:54.26,Default,,0000,0000,0000,,can do on strings, there's a extensive\Nlibrary of built-in capabilities Dialogue: 0,0:16:54.26,0:16:55.31,Default,,0000,0000,0000,,in Python. Dialogue: 0,0:16:55.31,0:16:59.28,Default,,0000,0000,0000,,And so the, the way we see these built-in\Ncapabilities Dialogue: 0,0:16:59.28,0:17:03.32,Default,,0000,0000,0000,,are they're, they're actually sort of\Nbuilt in to strings. Dialogue: 0,0:17:03.32,0:17:05.76,Default,,0000,0000,0000,,So, let's go real slow here. Dialogue: 0,0:17:05.76,0:17:07.31,Default,,0000,0000,0000,,Here we have a variable called greet and Dialogue: 0,0:17:07.31,0:17:10.05,Default,,0000,0000,0000,,we're sticking the string Hello Bob\Ninto it. Dialogue: 0,0:17:10.05,0:17:12.62,Default,,0000,0000,0000,,Now greet is of type string, as a result Dialogue: 0,0:17:12.62,0:17:16.59,Default,,0000,0000,0000,,of this, and it contains Hello Bob as its\Nvalue. Dialogue: 0,0:17:16.59,0:17:18.30,Default,,0000,0000,0000,,But we can actually access Dialogue: 0,0:17:18.30,0:17:26.56,Default,,0000,0000,0000,,capabilities inside of this value. So we\Ncan say, greet.lower(). Dialogue: 0,0:17:26.56,0:17:30.65,Default,,0000,0000,0000,,This is calling something that's part of\Ngreet itself, it's a part of all strings. Dialogue: 0,0:17:30.65,0:17:34.66,Default,,0000,0000,0000,,The fact that greet contains a string,\Nmeans that we can ask for, Dialogue: 0,0:17:34.66,0:17:38.12,Default,,0000,0000,0000,,hey, give me greet, which just gives you\Nback what you're looking for. Dialogue: 0,0:17:38.12,0:17:40.98,Default,,0000,0000,0000,,Like here, print greet is Hello Bob. Dialogue: 0,0:17:40.98,0:17:45.50,Default,,0000,0000,0000,,Or you can say give me greet lower, so\Nthis is giving me a lowercase copy. Dialogue: 0,0:17:45.50,0:17:51.03,Default,,0000,0000,0000,,It doesn't convert it to lowercase.\NIt gives me a lowercase copy of Hello Bob. Dialogue: 0,0:17:51.03,0:17:53.58,Default,,0000,0000,0000,,So zap is hello bob, all lowercase. Dialogue: 0,0:17:54.66,0:17:59.95,Default,,0000,0000,0000,,Now, it didn't change greet, right?\NAnd, you can even put this .lower on the Dialogue: 0,0:17:59.95,0:18:05.28,Default,,0000,0000,0000,,end of constants so, why you'd do this, I don't\Nknow, but Hi There, with H and T capitalized, Dialogue: 0,0:18:05.28,0:18:10.64,Default,,0000,0000,0000,,.lower comes out as hi there.\NSo this bit is part of Dialogue: 0,0:18:10.64,0:18:11.56,Default,,0000,0000,0000,,all strings. Dialogue: 0,0:18:11.56,0:18:17.90,Default,,0000,0000,0000,,Both variables and constants have these\Nstring functions built into them. Dialogue: 0,0:18:17.90,0:18:21.12,Default,,0000,0000,0000,,And every instance of a string, whether it Dialogue: 0,0:18:21.12,0:18:23.72,Default,,0000,0000,0000,,be a variable or a constant, has these\Ncapabilities. Dialogue: 0,0:18:23.72,0:18:28.15,Default,,0000,0000,0000,,They don't modify it, they just give you\Nback a copy. Dialogue: 0,0:18:28.15,0:18:31.50,Default,,0000,0000,0000,,Now it turns out there is a, a Dialogue: 0,0:18:31.50,0:18:36.17,Default,,0000,0000,0000,,command inside Python called dir, to ask\Nquestions like Dialogue: 0,0:18:36.17,0:18:39.73,Default,,0000,0000,0000,,hey, well here's, you know, stuff\Nhas got Hello World. Dialogue: 0,0:18:39.73,0:18:42.96,Default,,0000,0000,0000,,We can say. Redo this. Dialogue: 0,0:18:42.96,0:18:45.56,Default,,0000,0000,0000,,Come here. Dialogue: 0,0:18:45.56,0:18:48.24,Default,,0000,0000,0000,,Stuff is a string.\NWe can ask, hey, what are you? Dialogue: 0,0:18:48.24,0:18:49.66,Default,,0000,0000,0000,,I am a string. Dialogue: 0,0:18:49.66,0:18:53.82,Default,,0000,0000,0000,,dir is another built-in Python that asks\Nthe question, hey, what are all Dialogue: 0,0:18:53.82,0:18:56.64,Default,,0000,0000,0000,,the things that are built into this that I\Ncan make use of? Dialogue: 0,0:18:56.64,0:18:57.78,Default,,0000,0000,0000,,And here they are. Dialogue: 0,0:18:57.78,0:19:01.25,Default,,0000,0000,0000,,That's kind of a raw dump of them.\NYou can also go look at Dialogue: 0,0:19:01.25,0:19:05.91,Default,,0000,0000,0000,,the online documentation for Python and\Nsee at the Pyth, oop, at Dialogue: 0,0:19:05.91,0:19:09.67,Default,,0000,0000,0000,,the Python website, you can see a whole\Nbunch of these things. Dialogue: 0,0:19:09.67,0:19:13.69,Default,,0000,0000,0000,,And they have the calling sequence, what\Nthe parameters are, et cetera. Dialogue: 0,0:19:13.69,0:19:17.80,Default,,0000,0000,0000,,So when you're looking these things up,\Nyou can go, go read about them. Dialogue: 0,0:19:17.80,0:19:19.14,Default,,0000,0000,0000,,Here's just a few that I've pulled out, Dialogue: 0,0:19:19.14,0:19:23.20,Default,,0000,0000,0000,,capitalize, which uppercases the\Nfirst characters, Dialogue: 0,0:19:23.20,0:19:27.22,Default,,0000,0000,0000,,center, endswith, find, there's stripping. Dialogue: 0,0:19:27.22,0:19:28.30,Default,,0000,0000,0000,,So I'll look through a couple of these, Dialogue: 0,0:19:28.30,0:19:30.74,Default,,0000,0000,0000,,just the kind of things to be looking for. Dialogue: 0,0:19:30.74,0:19:33.78,Default,,0000,0000,0000,,It'll be a good idea to take a look and read\Nthrough some of the things. Dialogue: 0,0:19:33.78,0:19:37.54,Default,,0000,0000,0000,,Here's a couple that, that we'll probably\Nbe using early on. Dialogue: 0,0:19:37.54,0:19:43.70,Default,,0000,0000,0000,,The find function, it's similar to in but\Nit tells you where it finds the, the Dialogue: 0,0:19:43.70,0:19:49.52,Default,,0000,0000,0000,,particular thing that it's looking for.\NAnd and so we'll put fruit is banana. Dialogue: 0,0:19:49.52,0:19:52.38,Default,,0000,0000,0000,,And I'm going to say pos, which is\Ngoing to be an integer variable, Dialogue: 0,0:19:52.38,0:19:54.00,Default,,0000,0000,0000,,equals fruit.find("na"). Dialogue: 0,0:19:54.00,0:19:57.84,Default,,0000,0000,0000,,So what it's saying is, go look inside\Nthis variable fruit, Dialogue: 0,0:19:57.84,0:20:01.55,Default,,0000,0000,0000,,hunt until you find the first occurrence\Nof the string na. Dialogue: 0,0:20:01.55,0:20:05.59,Default,,0000,0000,0000,,Hunt, hunt, hunt, hunt, whoop, got it.\NAnd then return it to me. Dialogue: 0,0:20:05.59,0:20:10.58,Default,,0000,0000,0000,,So that's going to give me back 2.\N2 is where it found it, right? Dialogue: 0,0:20:10.58,0:20:14.12,Default,,0000,0000,0000,,So, where is it in the string, that's what\Nfind does. Dialogue: 0,0:20:14.12,0:20:16.92,Default,,0000,0000,0000,,And if you don't find anything, like\Nyou're looking for z, Dialogue: 0,0:20:16.92,0:20:21.44,Default,,0000,0000,0000,,no, no, no, I didn't find a z, then it\Ngives me back negative 1. Dialogue: 0,0:20:21.44,0:20:27.27,Default,,0000,0000,0000,,So just, again, this is just one of many\Nbuilt-in functions in string. Dialogue: 0,0:20:27.27,0:20:30.13,Default,,0000,0000,0000,,The ability to find a substring, okay? Dialogue: 0,0:20:30.13,0:20:33.09,Default,,0000,0000,0000,,Or find, yeah, find a character or string\Nwithin another string. Dialogue: 0,0:20:35.33,0:20:37.11,Default,,0000,0000,0000,,There's a lower case, there's also an Dialogue: 0,0:20:37.11,0:20:40.71,Default,,0000,0000,0000,,upper case, This might be better named\Nshouting. Dialogue: 0,0:20:40.71,0:20:44.07,Default,,0000,0000,0000,,Upper means give me an uppercase copy of\Nthis variable. Dialogue: 0,0:20:44.07,0:20:49.73,Default,,0000,0000,0000,,So Hello Bob becomes HELLO BOB, and then\Nlower is hello bob, right? Dialogue: 0,0:20:49.73,0:20:55.92,Default,,0000,0000,0000,,So these are both ways to get copies of\Nuppercase and lowercase versions. Dialogue: 0,0:20:55.92,0:20:58.44,Default,,0000,0000,0000,,You might think these are kind of silly,\Nbut one of the things Dialogue: 0,0:20:58.44,0:21:01.45,Default,,0000,0000,0000,,that you tend to use lower for is if\Nyou're doing searching and Dialogue: 0,0:21:01.45,0:21:03.70,Default,,0000,0000,0000,,you want to ignore case, you convert the\Nwhole thing Dialogue: 0,0:21:03.70,0:21:06.38,Default,,0000,0000,0000,,to lowercase, and then you search for a\Nlowercase string. Dialogue: 0,0:21:06.38,0:21:08.71,Default,,0000,0000,0000,,So you, depends on if you want to ignore\Ncase or not. Dialogue: 0,0:21:08.71,0:21:11.72,Default,,0000,0000,0000,,So that's, that's one of the reasons that\Nyou have things like this. Dialogue: 0,0:21:14.28,0:21:19.22,Default,,0000,0000,0000,,There is a replace function.\NAgain, it doesn't change the value. Dialogue: 0,0:21:19.22,0:21:21.64,Default,,0000,0000,0000,,Greet is going to have Hello Bob. Dialogue: 0,0:21:21.64,0:21:28.35,Default,,0000,0000,0000,,And I'm going to say, greet.replace all\Noccurrences of Bob with Jane. Dialogue: 0,0:21:28.35,0:21:32.66,Default,,0000,0000,0000,,That gives me back a copy, in nstr, says\NHello Jane. Dialogue: 0,0:21:32.66,0:21:35.69,Default,,0000,0000,0000,,So, so greet is unchanged. Dialogue: 0,0:21:35.69,0:21:39.89,Default,,0000,0000,0000,,This replace says, make a copy and then\Nmake that following Dialogue: 0,0:21:39.89,0:21:43.25,Default,,0000,0000,0000,,edit that you, that, that we've requested. Dialogue: 0,0:21:43.25,0:21:46.45,Default,,0000,0000,0000,,[COUGH] Now we can also say, well, I\Nmean, the replace Dialogue: 0,0:21:46.45,0:21:50.49,Default,,0000,0000,0000,,is going to do all occurrences, so greet\Nis still Hello Bob. Dialogue: 0,0:21:50.49,0:21:51.66,Default,,0000,0000,0000,,This is kind of redundant here. Dialogue: 0,0:21:51.66,0:21:53.98,Default,,0000,0000,0000,,I'm just doing it so you remember what it is. Dialogue: 0,0:21:53.98,0:21:55.31,Default,,0000,0000,0000,,Greet is still Hello Bob. Dialogue: 0,0:21:55.31,0:21:57.50,Default,,0000,0000,0000,,I put Hello Bob back in it and replace Dialogue: 0,0:21:57.50,0:22:00.85,Default,,0000,0000,0000,,all the occurrences of lowercase o with\Nuppercase X. Dialogue: 0,0:22:01.92,0:22:05.10,Default,,0000,0000,0000,,And then that happens.\NSo this says, Dialogue: 0,0:22:05.10,0:22:11.93,Default,,0000,0000,0000,,go through the whole string [SOUND] doing\Nall those replaces, okay? Dialogue: 0,0:22:11.93,0:22:14.24,Default,,0000,0000,0000,,Now another common thing that we're\Ngoing to have to do Dialogue: 0,0:22:14.24,0:22:16.90,Default,,0000,0000,0000,,is we're going to have to throw away\Nwhitespace. Dialogue: 0,0:22:16.90,0:22:18.63,Default,,0000,0000,0000,,Sometimes you have a string that Dialogue: 0,0:22:18.63,0:22:21.89,Default,,0000,0000,0000,,has characters, blank characters, or other\Ncharacters, Dialogue: 0,0:22:21.89,0:22:26.33,Default,,0000,0000,0000,,at the beginning and the end, nonprintable\Ncharacters, and we can strip them. Dialogue: 0,0:22:26.33,0:22:30.46,Default,,0000,0000,0000,,And there's three charact, three functions\Nthat are built into Dialogue: 0,0:22:30.46,0:22:32.84,Default,,0000,0000,0000,,to Python strings that do this for us. Dialogue: 0,0:22:33.92,0:22:38.20,Default,,0000,0000,0000,,There is lstrip, which strips from the left. Dialogue: 0,0:22:38.20,0:22:43.68,Default,,0000,0000,0000,,There is rstrip, which strips from the right. Dialogue: 0,0:22:43.68,0:22:47.44,Default,,0000,0000,0000,,So it throws away these whitespaces, so,\NHello Bob is gone. Dialogue: 0,0:22:48.47,0:22:50.94,Default,,0000,0000,0000,,I mean, the, so it gets rid of these\Ncharacters. Dialogue: 0,0:22:50.94,0:22:53.37,Default,,0000,0000,0000,,Oops, these are the ones that are gotten\Nrid of there. Dialogue: 0,0:22:53.37,0:22:55.91,Default,,0000,0000,0000,,I need an eraser.\NAnd then Dialogue: 0,0:22:55.91,0:22:59.31,Default,,0000,0000,0000,,let's use white, and then strip\Nbasically, gets rid of Dialogue: 0,0:22:59.31,0:23:03.25,Default,,0000,0000,0000,,all the whitespace, both on the left and\Nthe right side. Dialogue: 0,0:23:03.25,0:23:04.14,Default,,0000,0000,0000,,And gets rid of that. Dialogue: 0,0:23:04.14,0:23:07.01,Default,,0000,0000,0000,,So we're going to, we're going to be using\Nthese a lot. Dialogue: 0,0:23:07.01,0:23:09.86,Default,,0000,0000,0000,,It, one of the things you tend to do in\NPython is cleaning up data. Dialogue: 0,0:23:09.86,0:23:11.79,Default,,0000,0000,0000,,Sometimes if you have spaces at the\Nbeginning or Dialogue: 0,0:23:11.79,0:23:13.96,Default,,0000,0000,0000,,the end, you just want to kind of ignore\Nthem. Dialogue: 0,0:23:13.96,0:23:15.79,Default,,0000,0000,0000,,So you strip them off, you throw them\Naway. Dialogue: 0,0:23:18.02,0:23:22.13,Default,,0000,0000,0000,,When we're looking for data, we sometimes\Nare looking for a prefix, and Dialogue: 0,0:23:22.13,0:23:27.40,Default,,0000,0000,0000,,there is a startswith function [COUGH]\Nthat gives you a true or a false. Dialogue: 0,0:23:27.40,0:23:31.37,Default,,0000,0000,0000,,We're asking here, does this variable line\Nstart with the string Please. Dialogue: 0,0:23:31.37,0:23:34.82,Default,,0000,0000,0000,,And the answer is True, because it does\Nstart with the string Please. Dialogue: 0,0:23:34.82,0:23:38.29,Default,,0000,0000,0000,,Or, and then next, we ask, does this start\Nwith the letter p? Dialogue: 0,0:23:38.29,0:23:41.06,Default,,0000,0000,0000,,And the answer is False, it does not start\Nwith the letter p. Dialogue: 0,0:23:42.07,0:23:43.29,Default,,0000,0000,0000,,Okay? So there's Dialogue: 0,0:23:43.29,0:23:44.88,Default,,0000,0000,0000,,lots more of these things. Dialogue: 0,0:23:48.48,0:23:52.70,Default,,0000,0000,0000,,And reading data and tearing it apart is\None of the things that we're going to Dialogue: 0,0:23:52.70,0:23:57.30,Default,,0000,0000,0000,,really focus on for the rest of these\Nfirst few chapters of the book, okay? Dialogue: 0,0:23:57.30,0:24:00.04,Default,,0000,0000,0000,,Because that's one thing that Python's\Nreally good at is Dialogue: 0,0:24:00.04,0:24:03.86,Default,,0000,0000,0000,,tearing data into pieces and pulling the\Npieces that you want. Dialogue: 0,0:24:03.86,0:24:06.84,Default,,0000,0000,0000,,So, so let's take a look at this line. Dialogue: 0,0:24:06.84,0:24:11.46,Default,,0000,0000,0000,,So this line that we've got here is a line\Nfrom an actual email box. Dialogue: 0,0:24:11.46,0:24:13.55,Default,,0000,0000,0000,,This is what, if you Dialogue: 0,0:24:13.55,0:24:15.58,Default,,0000,0000,0000,,looked at your email, sort of, on your hard Dialogue: 0,0:24:15.58,0:24:18.71,Default,,0000,0000,0000,,drive, email boxes would have this kind of\Na format. Dialogue: 0,0:24:18.71,0:24:23.87,Default,,0000,0000,0000,,And there's actually many lines, and soon\Nwe'll reading whole files full of email. Dialogue: 0,0:24:23.87,0:24:26.94,Default,,0000,0000,0000,,But for now, let's just say we've got this\None line, somehow. Dialogue: 0,0:24:26.94,0:24:29.40,Default,,0000,0000,0000,,And we're looking for, we don't know\Nhow long Dialogue: 0,0:24:29.40,0:24:31.91,Default,,0000,0000,0000,,these things are going to be, the first\Ncharac, the Dialogue: 0,0:24:31.91,0:24:34.52,Default,,0000,0000,0000,,first thing is from, then there's an\Nemail address, Dialogue: 0,0:24:34.52,0:24:38.00,Default,,0000,0000,0000,,then there's some detail about when the\Nmail was sent. Dialogue: 0,0:24:38.00,0:24:40.55,Default,,0000,0000,0000,,But what we actually want is Dialogue: 0,0:24:40.55,0:24:42.45,Default,,0000,0000,0000,,we want this part right here, Dialogue: 0,0:24:42.45,0:24:45.91,Default,,0000,0000,0000,,and that's the domain name of the mail\Naddress, right? Dialogue: 0,0:24:45.91,0:24:48.11,Default,,0000,0000,0000,,We want to extract this out. Dialogue: 0,0:24:48.11,0:24:52.78,Default,,0000,0000,0000,,We're faced with this line, in a variable,\Nand we want to extract that out. Dialogue: 0,0:24:52.78,0:24:55.68,Default,,0000,0000,0000,,So this is kind of putting all these\Nthings together. Dialogue: 0,0:24:55.68,0:24:59.33,Default,,0000,0000,0000,,So let's walk through how we do this. Dialogue: 0,0:24:59.33,0:25:02.03,Default,,0000,0000,0000,,So, here's this line, and it's a big long\Nstring. Dialogue: 0,0:25:02.03,0:25:03.95,Default,,0000,0000,0000,,Mostly we would've read this from a file, Dialogue: 0,0:25:03.95,0:25:05.87,Default,,0000,0000,0000,,rather than just put it in a constant, but\Nfor now we Dialogue: 0,0:25:05.87,0:25:08.48,Default,,0000,0000,0000,,put it in a constant, because we, files is\Nthe next chapter. Dialogue: 0,0:25:09.95,0:25:12.50,Default,,0000,0000,0000,,And so what we're going to do is we're\Ngoing to say, you Dialogue: 0,0:25:12.50,0:25:15.38,Default,,0000,0000,0000,,know what, I'm going to look at this line\Nand I'm going to go Dialogue: 0,0:25:15.38,0:25:18.05,Default,,0000,0000,0000,,find the @ sign, and I want to know where\Nthe @ sign is. Dialogue: 0,0:25:18.05,0:25:24.33,Default,,0000,0000,0000,,So I call data.find @ sign, and put\Nthe result in atpos. Dialogue: 0,0:25:24.33,0:25:26.51,Default,,0000,0000,0000,,And that gives me 21. Dialogue: 0,0:25:26.51,0:25:29.17,Default,,0000,0000,0000,,It hunts until it finds the @ sign, and Dialogue: 0,0:25:29.17,0:25:34.31,Default,,0000,0000,0000,,then tells me where I found it.\NThen what I want to look at is, starting Dialogue: 0,0:25:34.31,0:25:39.20,Default,,0000,0000,0000,,here, for the rest of the string, I want\Nto find the first space afterwards. Dialogue: 0,0:25:40.25,0:25:45.87,Default,,0000,0000,0000,,So what I say is, this, sppos is my\Nvariable for the position of the space, Dialogue: 0,0:25:45.87,0:25:51.13,Default,,0000,0000,0000,,data.find, a blank, starting\Nat the @. Dialogue: 0,0:25:51.13,0:25:54.22,Default,,0000,0000,0000,,So this is starting at 21.\NSo it says, I'll start Dialogue: 0,0:25:54.22,0:25:59.52,Default,,0000,0000,0000,,at 21 and I'll look for the next blank.\NAnd I find that at 31. Dialogue: 0,0:25:59.52,0:26:05.35,Default,,0000,0000,0000,,So now I know where the @ sign is and I\Nknow where the space is. Dialogue: 0,0:26:05.35,0:26:08.17,Default,,0000,0000,0000,,And so what I'm looking at is, I want the\Nstuff Dialogue: 0,0:26:08.17,0:26:14.19,Default,,0000,0000,0000,,one beyond the @ sign, up to but not\Nincluding the space. Dialogue: 0,0:26:14.19,0:26:20.14,Default,,0000,0000,0000,,So then I can use a slicing operation, I\Ncan use a slicing operation. Dialogue: 0,0:26:20.14,0:26:22.65,Default,,0000,0000,0000,,Start at the @ position, add 1 to it, Dialogue: 0,0:26:22.65,0:26:26.48,Default,,0000,0000,0000,,so advance 1, that's going to be the\Nletter u. Dialogue: 0,0:26:26.48,0:26:30.73,Default,,0000,0000,0000,,And then a slicing operation, up to but\Nnot including space. Dialogue: 0,0:26:30.73,0:26:36.19,Default,,0000,0000,0000,,Up to, this is going to work out nicely\Nall of a sudden, but not Dialogue: 0,0:26:36.19,0:26:41.77,Default,,0000,0000,0000,,including, okay?\NAnd then Dialogue: 0,0:26:41.77,0:26:45.80,Default,,0000,0000,0000,,I'm going to take that slice, which is\Nreally this little bit of data right here, Dialogue: 0,0:26:45.80,0:26:49.50,Default,,0000,0000,0000,,take that slice, and put in the variable\Nhost. Dialogue: 0,0:26:49.50,0:26:53.84,Default,,0000,0000,0000,,Then we print that out and we get the\Npiece, okay? Dialogue: 0,0:26:53.84,0:26:56.98,Default,,0000,0000,0000,,And so, here we have some data we want to\Ntear apart. Dialogue: 0,0:26:56.98,0:26:58.23,Default,,0000,0000,0000,,We hunt for the @. Dialogue: 0,0:26:58.23,0:27:00.28,Default,,0000,0000,0000,,We find it at position 21. Dialogue: 0,0:27:00.28,0:27:04.60,Default,,0000,0000,0000,,We start at 21 and we look for the, the\Nspace after that. Dialogue: 0,0:27:04.60,0:27:10.66,Default,,0000,0000,0000,,31, and then we pull from 22, up to but\Nnot including, 31. Dialogue: 0,0:27:10.66,0:27:13.38,Default,,0000,0000,0000,,And it, it wouldn't matter where this\Nthing was, because these aren't all Dialogue: 0,0:27:13.38,0:27:17.49,Default,,0000,0000,0000,,the same length when we start looking at\Nthem in files, but it Dialogue: 0,0:27:17.49,0:27:20.54,Default,,0000,0000,0000,,would have found the @ sign and the space\Nafter the @ sign, Dialogue: 0,0:27:20.54,0:27:24.26,Default,,0000,0000,0000,,and it would have reliably\Npulled out the host, okay? Dialogue: 0,0:27:24.26,0:27:29.65,Default,,0000,0000,0000,,So this is a basic pattern we call\Nparsing. Dialogue: 0,0:27:29.65,0:27:32.07,Default,,0000,0000,0000,,Parsing text. Dialogue: 0,0:27:32.07,0:27:35.62,Default,,0000,0000,0000,,Find this, find that other thing, grab\Nthis thing out, Dialogue: 0,0:27:35.62,0:27:40.04,Default,,0000,0000,0000,,then look inside that thing and [SOUND].\NSo it does all these things, right? Dialogue: 0,0:27:40.04,0:27:45.43,Default,,0000,0000,0000,,So, that's kind of like strings.\NUp next, we have files. Dialogue: 0,0:27:45.43,0:27:46.77,Default,,0000,0000,0000,,Files are going to be lots of strings. Dialogue: 0,0:27:46.77,0:27:49.32,Default,,0000,0000,0000,,So we're going to start putting all these\Nthings together. Dialogue: 0,0:27:49.32,0:27:52.49,Default,,0000,0000,0000,,And and so the next chapter is a really,\Nreally Dialogue: 0,0:27:52.49,0:27:55.60,Default,,0000,0000,0000,,important chapter, where it starts to\Nreally start coming together. Dialogue: 0,0:27:55.60,0:27:57.11,Default,,0000,0000,0000,,So see you soon.