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