Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Strings and Operators7:17 with Craig Dennis
Let's explore Strings and how to concatenate them together
I like to imagine string just like a banner you might see at a party with each 0:00 letter strung together. 0:04 That's really what a string is, right? 0:06 It's a series of character. 0:08 The string data type provides some very handy methods that I'd like you to get 0:10 familiar with. 0:13 .After you create a string, it cannot be changed. 0:14 This is what is known as immutable or impossible to modify. 0:19 I'll remind you of this as we look at some of these examples. 0:23 Why don't we pop up in a show and I can show you what I'm talking about. 0:26 So, string literals, they can be made with either single or double quotes. 0:30 And I can imagine that you might say something like this. 0:35 You could say, 'I cannot understand why you need two options for quotes'. 0:38 Totally understand why you might say something like that. 0:47 But that's probably a little bit formal, isn't it? 0:50 We should probably use a conjunction. 0:52 How about we say something like, 'I can't understand'. 0:54 Oops, that's a problem, right? 0:59 We've got a quote inside of the quote. 1:04 We got a single quote, we are trying to use a single quote, uh-oh. 1:06 Now, option is that you can actually make it so 1:09 the quote is ignored by doing something called escaping. 1:13 So the backslash starts what is known us an escape sequence. 1:16 So the backslash, and 1:21 then a quote, basically tells the interpreter to treat that quote 1:22 like a character instead of treating it like part of the syntax, right? 1:26 So if I do, can\ 't understand, now it will see. 1:30 But, I mean this is kinda ugly, isn't it? 1:36 This backslash t. 1:38 Are you glad there's two types of quotes yet? 1:40 And actually, look, the REPL fixed it. 1:42 So you can use quotes like that. 1:44 So if you use double quotes, 1:45 then you could very easily use the single quote inside of it, like so. 1:48 Now we can say, "I can't", right? 1:52 There's a nice string. 1:55 And actually, escape sequences are great for 1:58 adding blank lines inside of your string. 2:00 So there's a special one that you'll see. 2:03 If we come over here and we go up, let's get this I can't back here. 2:05 Let's say "I can't..., and then we wanna add a new line. 2:10 And so that escape sequence to add a new blank line is \n for new line. 2:14 So we'd add two, two blank lines and we'll say, I can't even. 2:20 And the REPL here is playing tricks on us. 2:25 It's trying really hard to keep things on one line for us. 2:30 But actually if you print that string; so we'll go ahead and say we'll print. 2:32 And then we'll get back the result that was just there, right? 2:35 So we use the underscore. 2:38 So you'll see that it is actually I can't new line new line even. 2:39 There's our new lines. 2:43 My wife says that to me all the time while shaking her head about my dad jokes. 2:44 You're not the only one. 2:47 I suppose, I should quote her though, right? 2:50 So I would say, "She said,. 2:52 [LAUGH] I can't, so now what? 2:58 Now we've got a single quote and a double quote in here. 3:00 So actually, triple quotes allow you to start the string. 3:04 So we could say, """She said, "I can't... 3:11 And the nice thing about triple quotes is it allows you to have 3:17 spaces in your string. 3:20 So we can press like this and see those triple dots over here. 3:21 That means, it's waiting for me to, it's waiting for those final three quotes. 3:23 So we can say, "I can't... 3:28 even.", so she said that. 3:30 And still, there's another quote there, right? 3:31 So I'm ending that quote and then it's waiting for three quotes to end it. 3:34 So there we go, and if I say, go up a couple times here, we say, print (_). 3:40 She said "I can't... 3:46 even.", that's what she said. 3:47 All right, so now that we got creation out of the way, 3:49 let's look at combining some strings together. 3:52 What if I had a string like this word, here, chocolate? 3:56 Now we can actually combine it together with another string using a plus sign. 4:00 So I can say, "chocolate" + "marshmallow". 4:06 And you'll see what it returned was a brand new 4:11 string with the two words pushed together. 4:15 This is called concatenation. 4:19 Now, it's important to remember when you're concatenating strings to include 4:22 the proper spacing. 4:26 Otherwise, it will be slammed together like this. 4:27 So what we want is probably more like this, 4:29 "chocolate' + " and 4:33 marshmallows". 4:38 There we go, that feels good. 4:43 And of course, we can store that new string that 4:45 was created in a variable, right? 4:49 So, we could say desert = "chocolate" + " and marshmallows". 4:53 So that was a new string created and then we labeled it with dessert. 5:01 And we can use that variable to create a brand new one. 5:06 This is called reassigning. 5:09 So as I dessert = dessert + " and graham crackers". 5:10 And now, that might look like we changed the variable dessert. 5:23 But what happened was that this statement, dessert + " and 5:27 graham crackers", created a new string. 5:30 And we removed our already existing label and put it on the new string. 5:32 We reassigned it. 5:38 The old string, since it didn't have a label, 5:39 is essentially thrown away, like those leftovers in my office fridge. 5:42 Remember, we didn't actually change the original string because we can't, right? 5:46 And that's because strings are immutable. 5:51 This appending of more text to the end of a string is pretty common. 5:54 So there is a shortcut called in-place addition. 5:58 So if we say dessert, we can say +=. 6:02 And basically, that's just like the line above. 6:05 It's saying dessert = dessert + and then whatever we do. 6:07 So we say += ", yum. 6:11 And if we take a look, 'chocolate and marshmallows and graham crackers, yum'. 6:15 So [LAUGH] that needs some exclamation points, am I right? 6:21 And I want some more than just a couple. 6:25 I don't wanna just add a whole bunch of them myself. 6:27 I'd rather do that in code. 6:29 So that's where the asterisk comes into play. 6:31 So check this out. 6:34 If I want to repeat the string, well, 6:35 here's the string, I can then do a * for multiplication. 6:38 I say do that 20 times. 6:43 Awesome. 6:45 In addition to exclamations, this is really handy for 6:47 trying to draw layouts in text. 6:49 So let's append those. 6:52 So we'll say dessert, and we'll do an in-place addition, so dessert, 6:53 basically, that's desert +=, and we'll say "!" * 20. 6:59 And there we go. 7:04 Now before I append some more string information in your brain, 7:06 let's take a quick break and return to talk some more about strings. 7:09 We'll talk about various handy methods that a string provides. 7:13
You need to sign up for Treehouse in order to download course files.Sign up