Welcome back in this video, I just wanted to quickly talk about a concept that is called string template
or string interpolation.
So when you hover on the values inside our print statement here, you can see that there is a suggestion.
So it says I should convert a concatenation to a template because what we're doing here is we're concatenating,
right?
We're adding strings together.
But the alternative is to convert it into a template.
And that is something that Scotland prefers and is a very magical feature because it just adds this
with a button click.
So it's pretty cool.
So here, instead of adding two strings together, it's one string, so it's just as one statement.
But then it uses this dollar sign and then the variable name.
So it just says, OK, at this position, please replace the entry in the string with whatever is inside
of the first chart in string at that point.
So in this case, we will receive the same result.
We will still get the character h, but we don't have to add this plus and the empty space and so forth,
which can be very tedious when you have a very long statement where you use multiple different variables.
So this dollar sign here, it starts a string template expression.
So let's look at it with a little more depth.
So here we can actually make a whole sentence.
And at the same time, get values that we need from any variable.
So we have this first character is and then first chart in string and the length of my more, my SDR,
my string is.
And then let's use the length of my SDR.
So how do I get the length of my SDR?
Well, we saw that with this here.
OK, so my SDR length gives me the length of the entire string.
In this case, it's five plus another five and another empty space, so it should be 11, right?
So how do I get the length?
Now, of course, I could go ahead and create an extra variable that stores the length.
So let's call this my length, and this would be my SDR dot length that could be away.
But there is another way.
How about we just copy this year and put it in there with the dollar sign?
And you see that this doesn't seem to work properly because there's this dot, so let's just run this
real quick.
And it says the first character age and the length of my series Helloworld dot length.
So that's not exactly what I wanted to achieve.
So how can I make this work properly?
Well, the thing is, you can just surround it with curly brackets here.
So let's surround Maya's TR dot length with curly brackets.
And then it will treat it as an entire statement.
So now we're using the color and then the curly brackets with the statement inside that we want to be
used.
So let's run it again and we will see first character h and the length of my star is 11.
So this concept is called string template expression or string interpolation.