 In this video, we are going to briefly look into so-called string interpolation, which is really basically like strings acting as templates that events filled in during runtime. So let's create a new file and let's call it string interpolation. So to make an example, what we are going to do is I'm going to set a variable, let's call it name to my name, which is Alexander. And let's also go ahead and create a second variable and let's say it now it's evening, time of the day is evening. And also let's import the math module and we are in particular working with the math.py variable. So the number pi with the maximum precision that the floating point data type allows. And if you don't know, if you can't remember how the floating point numbers are not precise, how they are inherently imprecise, I would suggest you go back a couple of videos and receive that. So what is string interpolation? So string interpolation is really designing strings that are then filled in with some values at runtime when being evaluated. So there are two concepts. One is a recent addition to Python, so basically starting two, three years ago. Another one is a little bit more commonly used in code bases, but is hopefully fading out because the newer one is also kind of nice to look at. So the newer one is our so-called F strings, which is short for format strings. And then there's going to be the second category, which is going to be the so-called format method. So in the last video, we looked into string methods and the format method is a method that we have not looked into before. So let's also look into that briefly in this video. So let's do an example of an F string. So how does an F string work? Well, simply you're going to write an F and then you start the quotation mark and end the quotation mark. And now in between here, you're going to write whatever you want to write. So let's say I made this example up, hello, and then we are going to say in curly braces, name, which is going to refer to this variable here, name, and hello, whatever I say, hello, name, and then I say good evening probably. So let's do it like this, curly braces and time of day. So if I'm going to execute this cell, what is going to happen is Python is going to read this as a so-called literal because the F string is also a so-called literal notation. So in other words, Python knows that this notation means that it should create a string object with the value being derived from whatever is between the double quotes here. And now the thing is this, the F that is used as a prefix here simply indicates to Python that whatever is inside curly braces should be interpolated from variables in the environment. In this case, we are simply in the global environment, but what do I mean with environments? We have learned about function scopes and non-locals and local scopes and so on. So you should always have that in mind. So in other words, this name and this time of day variable is also looked up from the inside out. So to say if it's used inside a function, for example, but for now we are using it in the global scope and if I go ahead and evaluate that, what I get back is a string object where the value, the semantic value of the string object is simply filled in at runtime. So that's basically all there is to an F string. And this allows you to write nice looking strings in your source code and you can basically read the string code as a template. That is why I say template that is being filled in at runtime. So that is the F string example. And let's also do that for the number pi because the number pi of course is a number and let's say we want to do that. So if I say pi is and let's say we are going to use the curly braces. Inside the curly braces, you cannot only use simple variables, but you can also use a reference, for example, to a module and use the dot notation and get access to the pi variable inside the math module. And if I go ahead like this, then I get back a string that pi is and this number. But let's say I want to round this a little bit. So there is a so-called formatting language that I'm not going into too much detail in this course because it's really just like a little detail that you can look up whenever you need it. But inside the curly braces here, what you can do is you can end the math of pi by simply saying colon dot and let's say I want to have two decimals. So I would say 2f and this is the same notation that we also used in one of the videos when we talked about the float data type. And when we wanted to see how precise or what digits are actually stored inside the memory, if you remember that it's the same syntax. And if I execute it like this, then I get back pi is and the number with only two decimals. And of course, this syntax here that basically tells pi to only use the two first decimals does of course not change the number. So if we look at math dot pi after we executed the F string, it is of course still in full precision. It does not affect the outside world. Okay, so that is F strings. And let's look at how we can do the same thing using the old fashioned format method that the string comes with. So how this works is as follows. We're going to go ahead and we're going to say hello and we write simply curly braces here. Let's write the exclamation mark and let's say good. And also curly braces period. So that is now also a template. So to say if I evaluate that the curly braces just stay in there and the Python simply reads it as a string. So it simply creates a normal common string object where the curly braces are part of the semantic value of the string. However, if I go ahead and I call the format method on it and I pass to it two arguments, the first argument being the first position going into the first position here. So let's go ahead and pass to it name and also time of day. I go ahead like this, then they are plugged in just by position. So it works just like position arguments to functions. The arguments from the format method go into the same order into the curly braces. If you forget curly braces, then they are simply ignored as you can see. So you have to be a bit careful here, but this should not be too hard of a problem. And let's do some variations of that. There are two variations I want to look into with you. So let's say for whatever reasons the two arguments come into a different order. So what could you do to exchange the arguments as they will appear inside the final string? Well, you can simply go ahead and say that is my argument with index 1 and this is going to be my argument with index 0 referring to the index of the argument in the method call. And now this is going to be correct. And there is another version. Now this is now here using positional arguments, but we could of course also go ahead and use the keyword arguments. So what we can do here is I can simply go ahead and say name here. And here I'm simply going to say time. And then back here what we are going to do is we are simply going ahead and we will say name. The keyword name will be set to whatever value or whatever object the name variable is going to refer to. And at the same time also the time keyword to the method call will be set to the time of day variable. And if I go and execute this, I also get the same outcome. So these are various ways of how you can, in your source code, write nice looking and also easy to maintainable strings and use it just like templates. And at runtime you can plug in whatever you want to plug in. So this is often quite nice if you run a function many times. And for example, each time the function is called you want to print something out that is specific to this function call, then you could use that. In the notes you will see that there is a third way of doing a string interpolation, which is going to use, maybe I can give you that here as simply a header. It's going to use what we previously saw, the model operator. So this is also in syntax. So sometimes in some older code bases you also see that. So you could do string interpolation using the model operator as well. But this is discouraged in recent years because it's not so easy to read, even though it is the shortest version is definitely shorter than the format thing here. And yeah, but in recent years, people are going to revert to the F string format. So some tests in the Python community showed that the F strings are actually the fastest, but we shouldn't really care about speed in this beginners course. But the F strings are I guess the nicest one to read because they don't involve some method call after it. You can read the variable right in here. You can also put any expression that is valid Python expression inside curly braces and this will work and so on. So this is basically it. And of course also the formatting language here, the double, the colon and the dot f and so on. This could also be used. Let's maybe do one more example before we end this video. So let's go ahead and say this copy paste down this exact same example. Let's get rid of the F and also let's get rid of math dot pi. Let's simply put it in here just like this and simply use the format method. And give it method pi and we also get back pi is 3.14. So you can use the same formatting string formatting also in the F strings and in the format method. So my recommendation is try to use the F strings and if for whatever reason F strings look ugly in the source code, maybe use the format method. Don't be surprised to see the model operator in real world code, but don't use it on your own. You should always use one of the more recent versions here. Okay, so that's the concept of string interpolation, very easy concept, very valuable to know. So I'll see you in the next video.