1 00:00:00,000 --> 00:00:01,410 Let's talk about strings. 2 00:00:01,410 --> 00:00:05,000 Strings are a thing called a sequence. 3 00:00:05,080 --> 00:00:08,890 So a sequence is really just a generic term for an ordered 4 00:00:08,900 --> 00:00:13,000 set. That means you have a list of things to work off. 5 00:00:13,020 --> 00:00:16,320 And so when we said, let's not do 'name', let's do 'courses 6 00:00:16,379 --> 00:00:18,180 = "Python 7 00:00:18,190 --> 00:00:21,980 for Everybody", this is called a string. 8 00:00:21,990 --> 00:00:26,540 And so if we run 'type', not string, 'type(course)', 9 00:00:26,540 --> 00:00:28,100 this will just say string, 'str'. 10 00:00:28,120 --> 00:00:30,940 But behind the scenes, Python is ripping this apart. 11 00:00:30,950 --> 00:00:34,930 It is saying, there's a P-y-t-h-o-n, space, f-o-r, space, 12 00:00:34,930 --> 00:00:40,100 E-v-e-r-y-b-o-d-y, and it is trying to map all of it. 13 00:00:40,400 --> 00:00:43,430 Now, before we get into the sequence of a string, let's actually 14 00:00:43,440 --> 00:00:44,420 break down what a string is. 15 00:00:44,420 --> 00:00:46,400 A string really is just a sentence. 16 00:00:46,430 --> 00:00:48,790 So let me actually write that out. 17 00:00:48,800 --> 00:00:52,900 A "string" is just a sentence. 18 00:00:52,960 --> 00:00:54,160 That's all it is. 19 00:00:54,170 --> 00:00:55,570 It can have spaces, 20 00:00:55,580 --> 00:00:58,660 it can have commas, underscores, special characters, 21 00:00:58,670 --> 00:00:59,740 uppercase, lowercase, 22 00:00:59,750 --> 00:01:02,380 it can have pretty much anything as long as it starts with 23 00:01:02,390 --> 00:01:03,340 one of three things. 24 00:01:03,340 --> 00:01:05,700 A string needs to start with quotation marks. 25 00:01:05,780 --> 00:01:09,830 It needs to start with either quotation marks, or apostrophes, 26 00:01:09,860 --> 00:01:15,660 or a set of three quotation marks, or three apostrophes, like 27 00:01:15,670 --> 00:01:18,720 this. Now, there are different reasons for using these, and 28 00:01:18,720 --> 00:01:20,600 let's explore some of them right now. 29 00:01:20,610 --> 00:01:25,630 So let's say I have a sentence, a 'sentence', and it says, 30 00:01:26,500 --> 00:01:27,400 "What's going on?" 31 00:01:28,450 --> 00:01:32,510 And when I display the 'sentence', it says, "What's going on?". Not a 32 00:01:32,520 --> 00:01:38,910 problem. But we can also create this sentence with apostrophes 33 00:01:38,910 --> 00:01:40,100 instead of quotation marks. 34 00:01:40,100 --> 00:01:42,200 Now you can actually see the syntax highlighting here is 35 00:01:42,280 --> 00:01:45,300 broken, and that's because Python says, "Oh, you're starting 36 00:01:45,310 --> 00:01:46,410 with an apostrophe? 37 00:01:46,410 --> 00:01:48,800 This whole thing is going to be a string, and I'm going to keep 38 00:01:48,800 --> 00:01:52,600 thinking that it's a string until it matches the next apostrophe. 39 00:01:52,600 --> 00:01:55,500 Now, we, as humans said, well, the last one should be over here, 40 00:01:55,500 --> 00:01:59,100 but because this is a, I think it's called a conjugated 41 00:01:59,100 --> 00:02:02,300 word, it has an apostrophe right in here. 42 00:02:02,380 --> 00:02:06,600 And instead of saying, "What is", we said, "What's", 43 00:02:07,140 --> 00:02:12,330 and this is going to break. 'invalid syntax', Python is actually 44 00:02:12,330 --> 00:02:14,600 telling us exactly what it is. It's a 'SyntaxError'. 45 00:02:14,600 --> 00:02:20,900 So what we can do here is use quotation marks instead, and that works. 46 00:02:20,950 --> 00:02:22,900 And that continues to work. 47 00:02:23,020 --> 00:02:27,550 We could also use three quotation marks or three apostrophes, 48 00:02:27,560 --> 00:02:29,050 either one here is going to work. 49 00:02:29,050 --> 00:02:31,000 So if I keep running these cells, you're going to see that 50 00:02:31,000 --> 00:02:36,300 nothing actually changes and it continues to work. 51 00:02:36,380 --> 00:02:39,520 Now, the thing that I was getting at before is, if I just 52 00:02:39,520 --> 00:02:44,700 copy this over and get rid of those, that's 'SyntaxError', 53 00:02:44,710 --> 00:02:46,420 because we're using the same quotations. 54 00:02:46,430 --> 00:02:50,440 Well, sometimes it's not super easy to just change your quotations, 55 00:02:50,450 --> 00:02:53,860 or your apostrophes to quotation marks, or to a thing called 56 00:02:53,870 --> 00:02:55,690 the Docstring, which is the three in a row. 57 00:02:55,880 --> 00:02:59,240 But what we can do is we can say, "Hey, Python, this one in 58 00:02:59,240 --> 00:03:02,300 here, right here, is not actually the end of the string". 59 00:03:02,300 --> 00:03:04,430 And we tell it that it's not the end of the string, 60 00:03:04,440 --> 00:03:07,880 and to render it as a regular character by adding a slash, '\' 61 00:03:07,900 --> 00:03:10,000 in front of it, and this is called escaping. 62 00:03:10,050 --> 00:03:14,380 So Python now sees "\'", and says, "Okay, well, 63 00:03:14,390 --> 00:03:17,170 when I render this, I'm going to make sure that "\'" 64 00:03:17,180 --> 00:03:19,120 shows up as just a regular apostrophe. 65 00:03:19,130 --> 00:03:21,380 So let's go ahead and and run this as well, 66 00:03:22,100 --> 00:03:23,200 and it works. 67 00:03:23,480 --> 00:03:26,990 Now, sometimes you're going to want a string or a sentence 68 00:03:27,000 --> 00:03:28,100 on multiple lines. 69 00:03:28,300 --> 00:03:31,180 That's a fairly common thing that you're going to see in 70 00:03:31,190 --> 00:03:33,550 Python. And there are a few different ways we could do this. 71 00:03:33,550 --> 00:03:38,500 We could say 'course = "Python", and we put that in a string, 72 00:03:38,570 --> 00:03:42,580 so starting and ending with quotations, then we add a slash, 73 00:03:42,610 --> 00:03:48,160 backslash, '\', and then on a new line, we could say, "for Everybody", 74 00:03:48,420 --> 00:03:50,110 and let's run this. 75 00:03:50,950 --> 00:03:55,040 And we can actually see that it put it all in one line because 76 00:03:55,040 --> 00:03:57,800 I didn't do the spacing properly in there. There we go. 77 00:03:57,860 --> 00:04:01,340 I needed that extra space inside of the string for it to 78 00:04:01,350 --> 00:04:03,890 basically say, "Hey, there's one here, and there's one here, 79 00:04:04,040 --> 00:04:06,680 but because they're on two different lines followed by this 80 00:04:06,690 --> 00:04:08,930 little slash, '\' in the middle, put them together. 81 00:04:08,990 --> 00:04:13,200 So that's one method of doing it. Another method would be 82 00:04:13,210 --> 00:04:17,550 to use parentheses. So we could use an opening and closing 83 00:04:17,560 --> 00:04:18,930 parentheses here. 84 00:04:19,060 --> 00:04:24,120 And so let's say "Hello", make sure we add spaces this time, 85 00:04:24,899 --> 00:04:28,900 "My name is", "Kalob". 86 00:04:29,260 --> 00:04:33,040 Let's run that, and let's not call that 'course', 87 00:04:33,050 --> 00:04:35,560 let's call that 'greeting', because that's not a course, 88 00:04:35,570 --> 00:04:39,750 and that's just a terrible way to improperly name variables. 89 00:04:39,760 --> 00:04:42,690 And so let's run 'greeting' in here, and we see, "Hello. 90 00:04:42,750 --> 00:04:43,560 My name is Kalob". 91 00:04:43,820 --> 00:04:46,100 So that's the second way, is with parentheses. 92 00:04:46,110 --> 00:04:49,190 And because there's an opening and a closing bracket in here, 93 00:04:49,960 --> 00:04:53,490 Python is automatically going to say, "Okay. Well, I understand 94 00:04:53,500 --> 00:04:56,200 what you're trying to get out here." So you don't need to 95 00:04:56,220 --> 00:04:57,690 have the slash in there. 96 00:04:57,700 --> 00:05:00,450 It's just going to understand that this is a string, beside 97 00:05:00,460 --> 00:05:02,490 a string, beside a string, and just puts them all together. 98 00:05:02,500 --> 00:05:05,890 And when Python puts things together like that, when it puts 99 00:05:05,900 --> 00:05:08,590 a string together with another string, with another string 100 00:05:08,590 --> 00:05:13,340 just like this, that's called concatenation. concatenation. 101 00:05:14,510 --> 00:05:17,090 Now there's one more way, and you're going to see this one a 102 00:05:17,240 --> 00:05:18,380 lot in the wild, 103 00:05:18,500 --> 00:05:20,960 and this is more of a thing called the Docstring. 104 00:05:21,240 --> 00:05:26,580 So let's actually call this a 'doctstring', 'docstring', and 105 00:05:26,590 --> 00:05:31,080 it is equal to three opening quotation marks, """ or three opening 106 00:05:31,080 --> 00:05:33,700 apostrophes,''', it can be either one, and it has to end the 107 00:05:33,700 --> 00:05:34,800 same way, too. That's important. 108 00:05:34,860 --> 00:05:38,170 Whatever it starts with, it needs to end with """Hello. 109 00:05:38,170 --> 00:05:41,400 "this", "is", "a", "docstring". 110 00:05:41,400 --> 00:05:43,700 And we're going to see something a little different with this one. 111 00:05:43,870 --> 00:05:45,430 "docstring" 112 00:05:45,690 --> 00:05:47,530 Let's go ahead and run that. 113 00:05:47,600 --> 00:05:49,300 And if I do, 114 00:05:49,300 --> 00:05:51,500 I didn't name that right either, 115 00:05:51,500 --> 00:05:53,500 [no audio] 116 00:05:53,500 --> 00:05:56,400 And I also typoed that one. Look at that. 117 00:05:56,470 --> 00:05:57,900 At least I'm consistent with my typos. 118 00:05:58,060 --> 00:06:04,770 And if I put 'docstring' in here and run it, you have these 119 00:06:04,770 --> 00:06:06,600 things with a '\n'. 120 00:06:06,820 --> 00:06:13,020 Now, in programming, you can make newlines like this, 121 00:06:13,020 --> 00:06:14,600 and you can have it displayed out. 122 00:06:14,610 --> 00:06:17,220 And whenever you see a newline, and actually you see it 123 00:06:17,220 --> 00:06:21,100 a lot in any code, any text editor, any website, you see 124 00:06:21,100 --> 00:06:22,400 newlines all over the place. 125 00:06:22,480 --> 00:06:26,260 This '\n' is a newline. 126 00:06:26,500 --> 00:06:27,500 That's what it means. 127 00:06:27,700 --> 00:06:30,700 It's called an invisible character, and it doesn't actually show up. 128 00:06:30,750 --> 00:06:33,650 But whenever a computer recognizes that there's a '\n', 129 00:06:33,680 --> 00:06:37,070 it says, "Oh, okay. Well, let's put that on a new line". 130 00:06:37,760 --> 00:06:39,840 And that's what this did here. 131 00:06:39,850 --> 00:06:42,370 So it said, "The docstring started, 132 00:06:42,370 --> 00:06:43,500 and then there was a newline. 133 00:06:43,510 --> 00:06:44,410 "Hello", newline, 134 00:06:44,420 --> 00:06:45,910 "this", newline, "is", newline, 135 00:06:45,910 --> 00:06:48,000 "a", newline, "docstring", newline. 136 00:06:48,600 --> 00:06:52,910 But because we just wanted this as a regular string, it said, 137 00:06:52,910 --> 00:06:56,000 "Hey, just so you know, there are supposed to be newlines in here". 138 00:06:56,000 --> 00:07:00,600 Now, what would happen if we were to print the 'docstring'? 139 00:07:00,900 --> 00:07:04,140 Instead of just displaying what the value is, let's actually 140 00:07:04,150 --> 00:07:06,010 print the value of 'docstring'. 141 00:07:07,600 --> 00:07:10,500 Look at that, 'print', the function 142 00:07:10,500 --> 00:07:12,400 'print' looks for '\n', 143 00:07:12,420 --> 00:07:13,710 and says, "Oh, there's a newline". 144 00:07:14,510 --> 00:07:16,650 "Hello", there's a newline. 145 00:07:16,660 --> 00:07:17,910 'this', there's a newline. 146 00:07:17,910 --> 00:07:20,700 And it continues to go on and actually creates newlines for us. 147 00:07:20,940 --> 00:07:23,520 Now, if you're just jumping into this video, and you haven't 148 00:07:23,530 --> 00:07:26,580 watched the previous videos, a fun little fact about strings 149 00:07:26,610 --> 00:07:28,470 is they are immutable, 150 00:07:28,480 --> 00:07:30,990 that means they cannot be changed. 151 00:07:31,000 --> 00:07:35,400 All we can do is overwrite the variable name with a new value. 152 00:07:35,460 --> 00:07:38,460 Now, I mentioned that this is a sequence, and a sequence 153 00:07:38,460 --> 00:07:44,200 is basically a generic term for, like, an ordered set, like a grocery list. 154 00:07:44,220 --> 00:07:47,240 And so what we can do here, and this is getting a little 155 00:07:47,250 --> 00:07:49,040 advanced, so you don't have to know this part, 156 00:07:49,050 --> 00:07:50,570 but I'm going to demonstrate this. 157 00:07:51,200 --> 00:07:53,780 Let's do 'word = "Python"'. 158 00:07:54,800 --> 00:07:59,600 And let's create a for loop, 'for letter in word', 159 00:07:59,690 --> 00:08:02,870 and this demonstrates that this is a sequence of letters. 160 00:08:02,880 --> 00:08:06,090 This isn't just one word called "Python". 161 00:08:06,100 --> 00:08:09,990 It's actually six letters, individual letters, according 162 00:08:10,000 --> 00:08:11,600 to Python's internal workings. 163 00:08:11,640 --> 00:08:14,280 So we can do 'print(letter)'. 164 00:08:14,280 --> 00:08:17,400 And what this is going to do is print 'P-y-t-h-o-n. 165 00:08:17,430 --> 00:08:18,990 Let's go ahead and run that. 166 00:08:19,000 --> 00:08:22,600 Sure enough, it took every single letter and printed it out 167 00:08:22,600 --> 00:08:23,600 one by one. 168 00:08:23,600 --> 00:08:27,700 And that is what makes a string a sequence.