Last but not least, let's see yet another type of string formatting using f strings, also called formatted string. literals. f strings have been introduced in Python version three dot six through the pep 458 Python enhancement proposal as a way to embed variables and expressions inside strings using a minimal and elegant syntax. Considering the same string template that we used earlier, let's define some variables first, and then let's use an F string to include these values referenced by these variables in the string. So let me show you what I mean by this. Let's say we have model equals 2600 exam.
Then we have another variable slots equals four. And finally we have iOS equals 12 dot three, let's say and now let's use the new concept of F strings. Let me write this first. And now let me hit enter to see the result. Okay, so what we did here is we prefix the string with the letter F, as you can see, right To define it as an F string, and then in between curly braces, we entered the name of each previously defined variable to obtain the desired result. Pretty straightforward, right?
What if instead of just the variable name, we want to enter an expression? Well, in that case, all we have to do is to simply write the expression itself in between the curly braces. For instance, let's assume that we want the value referenced by the slots variable, which is four to be multiplied by two inside the resulting string. In this case, we have to just embed this expression in the string using the name of the variable and the multiplication operator. So without further ado, let me show you so we would need to use slots times two right here. After hitting Enter, the string was updated.
So we have eight one slots instead of four. I think this is pretty awesome. Now what if we want to use a string specific method to convert the letters in the string referenced by the model variable, so that would be 2600 exam two lowercase when we would need to call the lower method, right? f strings allow us to do that right in between the curly braces. So let me show you what I mean. I'm going to use model dot lower.
And indeed now we have lowercase x m, instead of uppercase XM target achieved using a very elegant and straightforward syntax. I hope you enjoyed this video and I will see you in the next one. Bye.