Get startedGet started for free

Formatted string literal

1. Formatted string literal

Modern versions of Python introduced a new powerful string formatting: formatted string literal method.

2. f-strings

The strings defined by this method are called f-strings. They have a minimal syntax as you can see in the slide. To defined them, you need to add the prefix f before the string. Inside quotes, you put your text along with curly braces, which identify placeholders where you insert the expressions. In the example code, we have two variables, way and method. Then, we define an f-string. Inside curly braces, we pass the two variables. The method replaces the placeholders with the variables. And we obtain the following output.

3. Type conversion

f-strings allow us to convert expressions into different types. We can use exclamation mark s for strings, r for printable representation of strings, or a to escape non-ascii characters. Let's imagine we define the variable name as you can see in the slide. The variable should be surrounded by quotes in the resulting string. We can add after the variable the exclamation r conversion. This will return a printable representation of the string. In the output, we can see that quotes are surrounding the variable.

4. Format specifiers

We can also use format specifiers such as e for scientific notation, d for digit and f for float. In the example code, we define a variable containing a number. Then, we insert it in the f-string. We specify that we want it to have only two decimals. And we get the following string.

5. Format specifiers

We can also format datetime. We only need to insert the variable containing the datetime object. After that, we placed a colon and the specifiers month name, day and year. And we get the string containing the date as we see on the code.

6. Index lookups

Do you remember when we accessed dictionaries from the string format method? To insert the value associated with a specific key, we specify the index without quotes. Let's try the same to access dictionaries in f-strings. As we see in the code, Python raises an error telling us that it cannot find the variable. This is due to the fact we need to surround the index with quotes.

7. Escape sequences

In the first video, we said that a string is anything appearing inside quotes. If the string contains quotes as well, one of the ways to escape the error raised is to add a backslash before the quotes.

8. Escape sequences

What does this have to do with f-strings? We said that indexes required quotes. f-strings follow the same rule as strings. If Python finds a second quote, it understands that this is a closing mark. So we could escape the quotes by adding backslash. But, we can observe in the code that backslashes are not allowed in the f-string expression. So our only solution is to use single quotes to get the desire output as we can observe in the slide.

9. Inline operations

One of the biggest advantages of f-strings is that they allow us to perform inline operations. In the example, we define two numeric variables. We then insert them into the f-string. We can also multiply them inside the expression. And we get the result of that operation in the output.

10. Calling functions

We can also call functions inside the expression of f-strings. In the example, we define a function. Then, we call this function and pass two numbers inside the expression in the f-string. This will return the value in the final string as we can observe in the slide.

11. Let's practice!

Now, it's time for you to start using f-strings!