F-String Formatting In Python

Introduction

What Is String Formatting?

String formatting is attractively designing our string using formatting techniques provided by the particular programming language. We have different string formatting techniques in Python. We are now going to explore the new f-string formatting technique.

f-string evaluates at runtime of the program. It’s swift compared to the previous methods.

f-string having an easy syntax compared to previous string formatting techniques of Python. We will look into every bit of this formatting using different examples.

Syntax

Every f-string statement consists of two parts, one is character f or F, & the next one is a string which we want to format. The string will be enclosed in single, double, or triple quotes.

Let’s see the syntax.

In place of string, we have to place our sentence which is going to format.

Displaying Variables

We previously use the str.format() method mostly to format the strings. But, the time has changed we have a new method to make our efforts twice as fast.

The variables in the curly { } braces are displayed in the output as a normal print statement. Let’s see an example.

We have got the values of the variables in the output. See, it’s very simple. We can also replace the f with Fas well.

f is either lower or upper it’ll work the same.

Expressions

What if we can evaluate expressions like arithmetic, function calls, etc., in a string? f-string allows us to evaluate expressions inside the string.

Just put the expression inside { } to evaluate. f-string evaluates it at runtime of the program. It’s an excellent feature which saves us time & code.

Let’s see examples of the different types of expressions.

We can also call functions inside the { }. Let’s define a function greet() & call it in the f-string

In the same way, we can also call the predefined methods. Let’s see.

What else can we do with the f-string? Are we able to display the object? Yes, we can. It’s the same as when we show the output of the variables.

Special Characters

What if we want to display special characters like {}, \’, \”?, which are internally used by Python for special meaning. Can we use escape character inside the f-string? Let’s see the answers to these questions.

Quotations

We can use any quotation marks {single or double or triple} in the f-string. We have to use the escape character to print quotation marks. The f-string expression doesn’t allow us to use the backslash. We have to place it outside the { }.

Let’s see it by examples.

Let’s see other types of quotations in f-strings.

We can also include { } inside the three quotes as well.

We can’t use the backslashes inside the { } in f-string. We’ll get an error if they are included inside { }.

Braces

We have to use a double set of braces to print the braces using f-string. Let’s see an example.

If we want to display two sets of braces using f-string, then we have to use four sets of braces.

Dictionaries

We have to be a bit careful while dealing with dictionary keys inside the f-string. We have to use a different quotation to the dictionary key & f-string. We are not permitted to use the same quotations for a dictionary key as if it was an f-string.

We are not allowed to use it as follows in the case of dictionaries.

RELATED LINKS:

Data Scientist & Machine Learning Engineer

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store