Get startedGet started for free

Positional formatting

1. Positional formatting

The next step of our journey is string formatting. In this video, we'll talk about positional formatting.

2. What is string formatting?

String formatting is also called string interpolation. It is the process of inserting a custom string in a predefined text. Here, we see an example. Don't panic. We are going to explain this later. As data scientist, you will use it to insert a title in a graph, show an error message or pass a statement into a function.

3. Methods for formatting

The modern versions of Python have three main approaches to string formatting: positional formatting, covered in this video, formatted strings literals, and template methods, covered in following videos.

4. Positional formatting

Positional formatting works in the following way. We put placeholders defined by a pair of curly braces in a text. We call the string dot format method. Then, we pass the desired value into the method. The method replaces the placeholders using the values in order of appearance. Let's examine the example. We define a string and insert two placeholders. We pass two strings to the method which will be passed to get the following output.

5. Positional formatting

We can use variables for both the string and the values passed to the method. In the example code, we defined a string with placeholders along with two other variables. We apply the format method to the string using the two defined variables. The method reads the string and replaces the placeholders with the given values. And we get the output seen in the slide.

6. Reordering values

We can add index numbers into the curly braces. This affects the order in which the method replaces placeholders. In the example, we left the placeholders empty. The method replaces them with the values in the given order. And we get the output shown here. If we add the index numbers, the replacement order changes accordingly. Now, the output changes as you can observe.

7. Named placeholders

We can also introduce keyword arguments that are called by their keyword name. In the example code, we inserted keywords in the placeholders. Then, we call these keywords in the format method. We then assign which variable will be passed for each of them resulting in the following output.

8. Named placeholders

Let's examine this code. We have defined a dictionary with keys: tool and goal. We want to insert their values in a string. Inside the placeholders, we can specify the value associated with the key tool of the variable data using bracket notation. Pay attention to the code. Data is the dictionary specified in the method and tool is the key present in that dictionary. So, we get the desired output shown in the slide. Be careful! You need to specify the index without using quotes.

9. Format specifier

We can also use format specifiers inside the curly braces. This defines how individual values are presented. We’ll use the syntax index colon specifier. One of the most common format specifiers is float represented by the letter f. In the code, we specified that the value passed with index 0 will be a float, getting the displayed output. We could also add dot two f indicating that we want the float to have two decimals as seen in the resulting output.

10. Formatting datetime

Python has a module called datetime that allows us to, for example, get the date and time for today. You can see that the format returned is very particular. We can use format specifiers such as percentage y, m, d, h and capital m to adjust the format to something more familiar to us as you can observe in the slide.

11. Let's practice!

Now, it's your time to practice string formatting!