- [Instructor] It probably comes as no surprise to you that string formatting is one of the most common things that Python programmers have to do. It might, however, come as a surprise to you that there are no fewer than four ways, at least, to perform string formatting in Python. In this video, we will take a look at using template strings, which you might be familiar with from other languages. The documentation for these are at this link in the Python docs, and I'll go ahead and click on the template strings, over here, in the table of contents.
And it's not a long read but I'll cover the main features here. So, let's go back to the code. So, I've opened the file templstr_start.py and you can see that I've got a string, here, in the main function, and I'm using the regular string formatting feature to format it. And when I just go ahead and run this, as it is, you can see that it works pretty much as you'd expect. So, here's the string and these values have been substituted into the string by the format function but you can also use a simpler string formatting method known as template strings.
In order to do this, I'm going to first, import the template class from the string module. So, up here, I'll write, so from string, I will import template, alright. And then, I'm going to define a new template. So, let's close this one, entirely. And what I'm going to do is write templ equals template and then I'll give it a string. And I'll write you're watching. And now, I'm going to use the substitution placeholder variable syntax, which is the dollar sign and braces, and I'm going to use the keyword title by and then dollar sign, author.
So, to insert the values into the placeholders, I need to use the substitute method on the string using keyword arguments for the values. So, to do that, I will write str2 equals templ.substitute and for the title keyword, I'll give it advanced Python, and for author, I'll give it my name. Right, so let's save that and then, of course, I'll print str2.
So, let's go ahead and run this. So, up here, I'll click the debug view and I'll run. And now, you can see that the output is identical, right? Using the regular string formatting and then using the template strings, right? So even better, I can use a dictionary to hold the data values that I want to substitute. So, let's clear this and let's go back to the code. And what I'll do now is insert a dictionary, and the first value will be author, and the second value will be title.
Alright, and now I'll make a new string, str3, and I'll say templ.substitute, and in this case, I'll just simply pass the dictionary, and then I'll just print str3. Alright, so once again, let's go ahead and run that. Let me save it first. And when I run it, you can see that the results are exactly the same. So, you might be wondering why you would use this method of string formatting instead of the regular string format function. And there are a couple of reasons.
First, if all you need to do is simple variable substitution, then the template string method is much easier to use and the code is more readable. The format function is definitely effective and has a lot of power. You can control the output formatting with all kinds of specifiers for spacing, and number formatting, and justification, and so on. But the template method is just about straight forward value substitution. The other reason you might want to use template strings is if the templates are being supplied from a source that you don't control or don't fully trust.
There are some potential security issues with the format function, given how much power it has, so if you need to format strings with some extra security, then the template method might be a better way to go.
- Truth value testing
- Template strings
- Advanced Python functions
- Advanced collections
- Advanced classes and objects
- Python comprehensions: list, dictionary, and set