Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:00,240 --> 00:00:05,580
In the previous exercise, we have seen how to print multiple variables to consult, but sometimes we
2
00:00:05,580 --> 00:00:11,010
don't want to print things line by line and instead we want to combine variables together into a single
3
00:00:11,010 --> 00:00:11,440
string.
4
00:00:11,850 --> 00:00:18,300
For example, it would be nice if we could print a sentence that says, My name is Andrea, I'm 36 years
5
00:00:18,300 --> 00:00:20,910
old and I'm one point eighty four meters tall.
6
00:00:21,240 --> 00:00:23,250
So let's see how we can do this.
7
00:00:23,250 --> 00:00:29,490
Using the variables that we have already defined by the way we can use single or double quotes to represent
8
00:00:29,490 --> 00:00:30,480
a string literal.
9
00:00:30,660 --> 00:00:33,150
So in this case, we will use double quotes.
10
00:00:33,160 --> 00:00:39,430
So let's remove all these statements and then we can add print and then we then double quotes.
11
00:00:39,750 --> 00:00:47,160
My name is and then we need to find a way to upend the first name variable to this string.
12
00:00:47,550 --> 00:00:53,690
Now there are two ways of doing this, which are known as string concatenation and string interpolation.
13
00:00:53,910 --> 00:00:57,450
So let's see how they work, starting with string concatenation.
14
00:00:57,900 --> 00:00:59,420
Two concatenate two strings.
15
00:00:59,430 --> 00:01:04,650
We can use the plus sign like this plus first name.
16
00:01:05,610 --> 00:01:10,020
And if we run this code, we can see that it works just fine.
17
00:01:10,320 --> 00:01:15,950
And because we can concatenate as many strings as we want here, we could add plus and then we then
18
00:01:15,960 --> 00:01:22,530
double quotes a space so that we can separate the first name and the last name and then plus last name
19
00:01:22,770 --> 00:01:23,370
like this.
20
00:01:23,700 --> 00:01:28,250
And if we run this, the log will show both my name and surname.
21
00:01:28,500 --> 00:01:34,080
But as you can see, this syntax is quite verbose and it quickly becomes hard to read if you have to
22
00:01:34,080 --> 00:01:39,670
concatenate many strings to solve this problem that support string interpolation.
23
00:01:39,870 --> 00:01:41,750
So let me show you how this works.
24
00:01:42,030 --> 00:01:51,270
So here I can copy and paste this line and then I can replace all the concatenation code with the first
25
00:01:51,270 --> 00:01:58,010
name and then space and then dollar last name and quotation mark at the end.
26
00:01:58,440 --> 00:02:05,430
So when we insert a dollar followed by a variable inside a string, we are telling that to take the
27
00:02:05,440 --> 00:02:10,130
value that that variable holds and insert it at this position in the string.
28
00:02:10,440 --> 00:02:17,040
And if we run this code, we can see that the console shows my name is on the top two times.
29
00:02:17,250 --> 00:02:23,310
And while both these print statements produce the same result, we can all agree that this syntax is
30
00:02:23,310 --> 00:02:25,160
more concise and easier to read.
31
00:02:25,590 --> 00:02:31,290
And when we write code, we should always aim to make it not just correct, but easy to understand.
32
00:02:31,560 --> 00:02:36,750
And when creating strings from multiple variables, string interpolation gives us just a convenient
33
00:02:36,750 --> 00:02:43,240
way of doing this so we can delete this line and here we can continue with our sentence and add.
34
00:02:43,580 --> 00:02:53,480
I'm eight years old and I'm Dolarhyde metres tall.
35
00:02:53,520 --> 00:02:58,050
And if we run this program now, we can see that we get the result that we want.
36
00:02:58,350 --> 00:03:02,870
Now let's suppose that I want to print what my age will be next year.
37
00:03:03,180 --> 00:03:04,230
To do that, I cannot.
38
00:03:04,230 --> 00:03:15,530
A new print statement and this could say next year I will be the age plus one years old.
39
00:03:16,800 --> 00:03:23,910
Now, if we run this code, the console will say that next year I will be thirty six plus one years
40
00:03:23,910 --> 00:03:24,240
old.
41
00:03:24,240 --> 00:03:29,060
And while this is technically correct, it is not exactly what we wanted to print.
42
00:03:29,310 --> 00:03:36,750
So our goal here is to first take my age and then add one to it and then interpolate the result inside
43
00:03:36,750 --> 00:03:37,490
this string.
44
00:03:37,800 --> 00:03:44,130
But if we write code like this, that will only interpolate the variable that immediately follows the
45
00:03:44,130 --> 00:03:44,850
dollar sign.
46
00:03:45,120 --> 00:03:51,060
To get the result that we want, we can use a pair of curly braces to surround the expression that we
47
00:03:51,060 --> 00:03:52,290
want to interpolate.
48
00:03:52,440 --> 00:03:58,680
And as you can see, the syntax highlighter now shows age plus one with a different color to indicate
49
00:03:58,680 --> 00:04:00,090
that it is an expression.
50
00:04:00,420 --> 00:04:04,150
And now we can run this code and the result looks much better.
51
00:04:04,320 --> 00:04:10,800
So the key takeaway here is that if you want to evaluate an expression inside a string, you can use
52
00:04:10,830 --> 00:04:13,920
this syntax and put it inside curly braces.
53
00:04:13,950 --> 00:04:19,530
After that, the last line, by the way, this is the first arithmetic expression that we see in this
54
00:04:19,530 --> 00:04:19,980
course.
55
00:04:19,980 --> 00:04:23,320
And we will talk about expressions more in detail a bit later.
56
00:04:23,530 --> 00:04:27,180
Also, you may need to use curly braces in other cases.
57
00:04:27,510 --> 00:04:33,030
For example, here we could define the current temperature of 30 degrees.
58
00:04:33,180 --> 00:04:38,370
And I could print this by typing the LA Times like this.
59
00:04:38,760 --> 00:04:43,080
And here I could add our space and then capital C for Celsius.
60
00:04:43,260 --> 00:04:50,610
But if I wanted to print this without the space, the enterprise temp C as the name of a variable which
61
00:04:50,610 --> 00:04:51,540
doesn't exist.
62
00:04:51,540 --> 00:04:58,950
So to make this code work here and it is curly braces like this, so these examples should clarify how
63
00:04:58,950 --> 00:04:59,510
you can use.
64
00:04:59,750 --> 00:05:02,130
Interpellation with variables and expressions.
65
00:05:02,330 --> 00:05:06,170
OK, so we can clear all this code and continue on the next lesson.
6837
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.