1
00:00:01,030 --> 00:00:06,400
Welcome back in this video, I just wanted to quickly talk about a concept that is called string template

2
00:00:06,400 --> 00:00:08,260
or string interpolation.

3
00:00:08,500 --> 00:00:15,490
So when you hover on the values inside our print statement here, you can see that there is a suggestion.

4
00:00:15,670 --> 00:00:22,420
So it says I should convert a concatenation to a template because what we're doing here is we're concatenating,

5
00:00:22,420 --> 00:00:22,650
right?

6
00:00:22,660 --> 00:00:24,160
We're adding strings together.

7
00:00:24,640 --> 00:00:28,210
But the alternative is to convert it into a template.

8
00:00:28,420 --> 00:00:34,570
And that is something that Scotland prefers and is a very magical feature because it just adds this

9
00:00:34,630 --> 00:00:35,950
with a button click.

10
00:00:35,950 --> 00:00:36,670
So it's pretty cool.

11
00:00:37,150 --> 00:00:43,750
So here, instead of adding two strings together, it's one string, so it's just as one statement.

12
00:00:43,990 --> 00:00:48,070
But then it uses this dollar sign and then the variable name.

13
00:00:48,550 --> 00:00:55,570
So it just says, OK, at this position, please replace the entry in the string with whatever is inside

14
00:00:55,570 --> 00:00:57,790
of the first chart in string at that point.

15
00:00:58,180 --> 00:01:00,600
So in this case, we will receive the same result.

16
00:01:00,610 --> 00:01:07,210
We will still get the character h, but we don't have to add this plus and the empty space and so forth,

17
00:01:07,660 --> 00:01:12,820
which can be very tedious when you have a very long statement where you use multiple different variables.

18
00:01:14,540 --> 00:01:19,040
So this dollar sign here, it starts a string template expression.

19
00:01:19,760 --> 00:01:22,790
So let's look at it with a little more depth.

20
00:01:23,510 --> 00:01:26,480
So here we can actually make a whole sentence.

21
00:01:26,690 --> 00:01:30,380
And at the same time, get values that we need from any variable.

22
00:01:31,280 --> 00:01:43,280
So we have this first character is and then first chart in string and the length of my more, my SDR,

23
00:01:43,550 --> 00:01:44,930
my string is.

24
00:01:45,260 --> 00:01:49,760
And then let's use the length of my SDR.

25
00:01:50,330 --> 00:01:52,650
So how do I get the length of my SDR?

26
00:01:52,670 --> 00:01:54,620
Well, we saw that with this here.

27
00:01:55,370 --> 00:01:59,540
OK, so my SDR length gives me the length of the entire string.

28
00:01:59,540 --> 00:02:05,450
In this case, it's five plus another five and another empty space, so it should be 11, right?

29
00:02:06,320 --> 00:02:08,330
So how do I get the length?

30
00:02:08,660 --> 00:02:13,340
Now, of course, I could go ahead and create an extra variable that stores the length.

31
00:02:13,520 --> 00:02:22,130
So let's call this my length, and this would be my SDR dot length that could be away.

32
00:02:22,640 --> 00:02:23,630
But there is another way.

33
00:02:24,650 --> 00:02:28,910
How about we just copy this year and put it in there with the dollar sign?

34
00:02:30,150 --> 00:02:35,910
And you see that this doesn't seem to work properly because there's this dot, so let's just run this

35
00:02:35,910 --> 00:02:36,420
real quick.

36
00:02:37,350 --> 00:02:43,230
And it says the first character age and the length of my series Helloworld dot length.

37
00:02:43,890 --> 00:02:45,960
So that's not exactly what I wanted to achieve.

38
00:02:46,830 --> 00:02:49,620
So how can I make this work properly?

39
00:02:50,070 --> 00:02:54,240
Well, the thing is, you can just surround it with curly brackets here.

40
00:02:54,630 --> 00:02:59,580
So let's surround Maya's TR dot length with curly brackets.

41
00:02:59,970 --> 00:03:02,790
And then it will treat it as an entire statement.

42
00:03:03,270 --> 00:03:09,210
So now we're using the color and then the curly brackets with the statement inside that we want to be

43
00:03:09,210 --> 00:03:09,570
used.

44
00:03:10,110 --> 00:03:15,930
So let's run it again and we will see first character h and the length of my star is 11.

45
00:03:17,780 --> 00:03:23,240
So this concept is called string template expression or string interpolation.

