1 00:00:00,779 --> 00:00:04,905 I like to imagine string just like a banner you might see at a party with each 2 00:00:04,905 --> 00:00:06,380 letter strung together. 3 00:00:06,380 --> 00:00:08,280 That's really what a string is, right? 4 00:00:08,280 --> 00:00:09,360 It's a series of characters. 5 00:00:10,530 --> 00:00:13,570 The string data type provides some very handy methods that I'd like you to get 6 00:00:13,570 --> 00:00:14,235 familiar with. 7 00:00:14,235 --> 00:00:19,310 After you create a string, it cannot be changed. 8 00:00:19,310 --> 00:00:23,620 This is what is known as immutable or impossible to modify. 9 00:00:23,620 --> 00:00:26,510 I'll remind you of this as we look at some of these examples. 10 00:00:26,510 --> 00:00:30,226 Why don't we pop open a shell and I can show you what I'm talking about. 11 00:00:30,226 --> 00:00:35,850 So, string literals, they can be made with either single or double quotes. 12 00:00:35,850 --> 00:00:38,550 And I can imagine that you might say something like this. 13 00:00:38,550 --> 00:00:47,110 You could say, 'I cannot understand why you need two options for quotes'. 14 00:00:47,110 --> 00:00:50,340 Totally understand why you might say something like that. 15 00:00:50,340 --> 00:00:52,890 But that's probably a little bit formal, isn't it? 16 00:00:52,890 --> 00:00:54,215 We should probably use a conjunction. 17 00:00:54,215 --> 00:00:59,728 How about we say something like, 'I can't understand'. 18 00:00:59,728 --> 00:01:02,890 Oops, that's a problem, right? 19 00:01:04,045 --> 00:01:06,695 We've got a quote inside of the quote. 20 00:01:06,695 --> 00:01:09,620 We got a single quote, we are trying to use a single quote, uh-oh. 21 00:01:09,620 --> 00:01:13,140 Now, option is that you can actually make it so 22 00:01:13,140 --> 00:01:16,570 the quote is ignored by doing something called escaping. 23 00:01:16,570 --> 00:01:21,010 So the backslash starts what is known us an escape sequence. 24 00:01:21,010 --> 00:01:22,430 So the backslash, and 25 00:01:22,430 --> 00:01:26,980 then a quote, basically tells the interpreter to treat that quote 26 00:01:26,980 --> 00:01:30,310 like a character instead of treating it like part of the syntax, right? 27 00:01:30,310 --> 00:01:36,590 So if I do, can\ 't understand, now it will see. 28 00:01:36,590 --> 00:01:38,980 But, I mean this is kinda ugly, isn't it? 29 00:01:38,980 --> 00:01:40,680 This backslash t. 30 00:01:40,680 --> 00:01:42,545 Are you glad there's two types of quotes yet? 31 00:01:42,545 --> 00:01:44,520 And actually, look, the REPL fixed it. 32 00:01:44,520 --> 00:01:45,960 So you can use quotes like that. 33 00:01:45,960 --> 00:01:48,090 So if you use double quotes, 34 00:01:48,090 --> 00:01:52,480 then you could very easily use the single quote inside of it, like so. 35 00:01:52,480 --> 00:01:55,910 Now we can say, "I can't", right? 36 00:01:55,910 --> 00:01:56,660 There's a nice string. 37 00:01:58,440 --> 00:02:00,830 And actually, escape sequences are great for 38 00:02:00,830 --> 00:02:03,310 adding blank lines inside of your string. 39 00:02:03,310 --> 00:02:05,450 So there's a special one that you'll see. 40 00:02:05,450 --> 00:02:10,533 If we come over here and we go up, let's get this I can't back here. 41 00:02:10,533 --> 00:02:14,690 Let's say "I can't..., and then we wanna add a new line. 42 00:02:14,690 --> 00:02:20,350 And so that escape sequence to add a new blank line is \n for new line. 43 00:02:20,350 --> 00:02:23,975 So we'd add two, two blank lines and we'll say, I can't even. 44 00:02:25,408 --> 00:02:30,140 And the REPL here is playing tricks on us. 45 00:02:30,140 --> 00:02:32,570 It's trying really hard to keep things on one line for us. 46 00:02:32,570 --> 00:02:35,510 But actually if you print that string; so we'll go ahead and say we'll print. 47 00:02:35,510 --> 00:02:38,112 And then we'll get back the result that was just there, right? 48 00:02:38,112 --> 00:02:39,890 So we use the underscore. 49 00:02:39,890 --> 00:02:43,350 So you'll see that it is actually I can't new line new line even. 50 00:02:43,350 --> 00:02:44,770 There's our new lines. 51 00:02:44,770 --> 00:02:47,870 My wife says that to me all the time while shaking her head about my dad jokes. 52 00:02:47,870 --> 00:02:50,010 You're not the only one. 53 00:02:50,010 --> 00:02:52,980 I suppose, I should quote her though, right? 54 00:02:52,980 --> 00:02:57,220 So I would say, "She said,. 55 00:02:58,418 --> 00:03:00,800 [LAUGH] I can't, so now what? 56 00:03:00,800 --> 00:03:04,411 Now we've got a single quote and a double quote in here. 57 00:03:04,411 --> 00:03:11,700 So actually, triple quotes allow you to start the string. 58 00:03:11,700 --> 00:03:17,280 So we could say, """She said, "I can't... 59 00:03:17,280 --> 00:03:20,030 And the nice thing about triple quotes is it allows you to have 60 00:03:20,030 --> 00:03:21,280 spaces in your string. 61 00:03:21,280 --> 00:03:23,880 So we can press like this and see those triple dots over here. 62 00:03:23,880 --> 00:03:28,010 That means, it's waiting for me to, it's waiting for those final three quotes. 63 00:03:28,010 --> 00:03:30,010 So we can say, "I can't... 64 00:03:30,010 --> 00:03:31,975 even.", so she said that. 65 00:03:31,975 --> 00:03:34,010 And still, there's another quote there, right? 66 00:03:34,010 --> 00:03:39,040 So I'm ending that quote and then it's waiting for three quotes to end it. 67 00:03:40,900 --> 00:03:46,250 So there we go, and if I say, go up a couple times here, we say, print (_). 68 00:03:46,250 --> 00:03:47,340 She said "I can't... 69 00:03:47,340 --> 00:03:49,620 even.", that's what she said. 70 00:03:49,620 --> 00:03:52,790 All right, so now that we got creation out of the way, 71 00:03:52,790 --> 00:03:55,050 let's look at combining some strings together. 72 00:03:56,440 --> 00:04:00,746 What if I had a string like this word, here, chocolate? 73 00:04:00,746 --> 00:04:06,390 Now we can actually combine it together with another string using a plus sign. 74 00:04:06,390 --> 00:04:11,775 So I can say, "chocolate" + "marshmallow". 75 00:04:11,775 --> 00:04:15,519 And you'll see what it returned was a brand new 76 00:04:15,519 --> 00:04:19,640 string with the two words pushed together. 77 00:04:19,640 --> 00:04:22,100 This is called concatenation. 78 00:04:22,100 --> 00:04:26,200 Now, it's important to remember when you're concatenating strings to include 79 00:04:26,200 --> 00:04:27,270 the proper spacing. 80 00:04:27,270 --> 00:04:29,500 Otherwise, it will be slammed together like this. 81 00:04:29,500 --> 00:04:33,421 So what we want is probably more like this, 82 00:04:33,421 --> 00:04:38,817 "chocolate' + " and 83 00:04:38,817 --> 00:04:43,430 marshmallows". 84 00:04:43,430 --> 00:04:45,340 There we go, that feels good. 85 00:04:45,340 --> 00:04:49,998 And of course, we can store that new string that 86 00:04:49,998 --> 00:04:53,500 was created in a variable, right? 87 00:04:53,500 --> 00:04:58,980 So, we could say desert = "chocolate" + " and marshmallows". 88 00:05:01,055 --> 00:05:06,370 So that was a new string created and then we labeled it with dessert. 89 00:05:06,370 --> 00:05:09,250 And we can use that variable to create a brand new one. 90 00:05:09,250 --> 00:05:10,800 This is called reassigning. 91 00:05:10,800 --> 00:05:16,617 So as I dessert = dessert + " and graham crackers". 92 00:05:23,060 --> 00:05:27,530 And now, that might look like we changed the variable dessert. 93 00:05:27,530 --> 00:05:30,365 But what happened was that this statement, dessert + " and 94 00:05:30,365 --> 00:05:32,430 graham crackers", created a new string. 95 00:05:32,430 --> 00:05:38,210 And we removed our already existing label and put it on the new string. 96 00:05:38,210 --> 00:05:39,720 We reassigned it. 97 00:05:39,720 --> 00:05:42,170 The old string, since it didn't have a label, 98 00:05:42,170 --> 00:05:46,200 is essentially thrown away, like those leftovers in my office fridge. 99 00:05:46,200 --> 00:05:51,770 Remember, we didn't actually change the original string because we can't, right? 100 00:05:51,770 --> 00:05:54,930 And that's because strings are immutable. 101 00:05:54,930 --> 00:05:58,610 This appending of more text to the end of a string is pretty common. 102 00:05:58,610 --> 00:06:02,050 So there is a shortcut called in-place addition. 103 00:06:02,050 --> 00:06:05,610 So if we say dessert, we can say +=. 104 00:06:05,610 --> 00:06:07,940 And basically, that's just like the line above. 105 00:06:07,940 --> 00:06:11,609 It's saying dessert = dessert + and then whatever we do. 106 00:06:11,609 --> 00:06:13,750 So we say += ", yum. 107 00:06:15,680 --> 00:06:21,530 And if we take a look, 'chocolate and marshmallows and graham crackers, yum'. 108 00:06:21,530 --> 00:06:25,610 So [LAUGH] that needs some exclamation points, am I right? 109 00:06:25,610 --> 00:06:27,500 And I want some more than just a couple. 110 00:06:27,500 --> 00:06:29,650 I don't wanna just add a whole bunch of them myself. 111 00:06:29,650 --> 00:06:31,400 I'd rather do that in code. 112 00:06:31,400 --> 00:06:34,220 So that's where the asterisk comes into play. 113 00:06:34,220 --> 00:06:35,380 So check this out. 114 00:06:35,380 --> 00:06:38,400 If I want to repeat the string, well, 115 00:06:38,400 --> 00:06:43,220 here's the string, I can then do a * for multiplication. 116 00:06:43,220 --> 00:06:44,480 I say do that 20 times. 117 00:06:45,940 --> 00:06:47,200 Awesome. 118 00:06:47,200 --> 00:06:49,700 In addition to exclamations, this is really handy for 119 00:06:49,700 --> 00:06:52,270 trying to draw layouts in text. 120 00:06:52,270 --> 00:06:53,835 So let's append those. 121 00:06:53,835 --> 00:06:59,521 So we'll say dessert, and we'll do an in-place addition, so dessert, 122 00:06:59,521 --> 00:07:04,593 basically, that's desert +=, and we'll say "!" * 20. 123 00:07:04,593 --> 00:07:06,070 And there we go. 124 00:07:06,070 --> 00:07:09,640 Now before I append some more string information in your brain, 125 00:07:09,640 --> 00:07:13,880 let's take a quick break and return to talk some more about strings. 126 00:07:13,880 --> 00:07:16,650 We'll talk about various handy methods that a string provides.