Get startedGet started for free

Template method

1. Template method

In this video, we'll take a closer look at the last string formatting method, template strings.

2. Template strings

Template strings have a simpler syntax. They are slow. And they don't allow the use of format specifiers. Yet, they are very suitable in some specific situations. Specially, when working with externally formatted strings that you don't have control over.

3. Basic syntax

Template strings do not belong to the Python core features. You need to import the Template class from the string module. First, you need to create the template string. For that, you use the Template constructor that takes only the string, as you can observe in the slide. Template strings use dollar signs to identify placeholders or identifiers. Then, you need to call the method that substitutes the identifier by the string values. For that, you use the identifier name equal the replacement string. And we'll get the output shown in the slide.

4. Substitution

We can place many identifiers as well as variables when using Template strings. In the example code, we define two variables containing strings. We can create a template having two identifiers with a designated name. Afterward, we call the method substitute to assign the identifiers to the different variables. And we get the following output.

5. Substitution

Sometimes we need to add extra curly braces after the dollar sign to enclose the identifier. This is required when valid characters follow the identifier but are not part of it. Let's clarify this. In the example, we need to add the ending -ing immediately after the first identifier. We need to include curly braces. If we don't do it, Python believes that -ing belong to the identifier name. We replace it by the variable noun obtaining the shown output.

6. Substitution

Let's imagine now that you are working with numbers and you want to include the dollar sign as part of a string. Because they are use for identifiers, you will need to escape this character by adding an extra dollar sign. And get the correct output as seen in the code.

7. Substitution

In the example code, we have defined a dictionary with only one key. However, when we define our template string, we include two identifiers. What would happen if we pass this dictionary to the method substitute? Python will raise an error. It tries to replace every placeholder and some of them are missing.

8. Substitution

We could try using the try except block again. In the slide, you can observe again the syntax. The try part will test the given code. If any error appears the except part will be executed obtaining the following output as a result.

9. Safe substitution

A better way to handle this situation is using the safe substitute method. This method will always try to return a usable string. How? It will place missing placeholders in the resulting string. Let's say we have the same situation as before. Now, if we pass the dictionary to the safe substitute, we will not get an error. Instead, we'll get the identifier dollar sign cake in our resulting string, as you can observe in the output.

10. Which should I use?

In summary, how do you decide which string formatting you should use? String format is easy to use. You could start mastering this method and then apply the concepts to f-strings. Moreover, the f-strings are always advisable above all methods. But only if you are working with modern versions of Python. Use template strings only when working with user-provided strings.

11. Let's practice!

Now that you know a lot about template strings, let's practice some of these concepts.