This is a text field

A new way to format the string in Python 3.6: fstring

Python fstring
Audio format of fstring

Introduction

String formatting is a great way to format a statement.

Hence, the python community has created 3 methods for it.

Out of 3, 1 is new and most easy to use.

Yes, I’m talking about python fstring.

The fstring method is easy to format. That means it has easy to use syntax.

To go into deep, you’ve to understand the old methods of string formatting.

So, I’m going to show you the old and new string formatting methods in python.

Traditional way to format string

Traditional is old patterns in the world and computer science.

These traditions are hard and confusing. so, you’ve to pay close attention to it.

Python fstring isn’t 1st method of string formatting in python.

There are two other methods of string formatting.

They are format() and % methods. Yet, both have some limitations Comparision to fstring.

First of all, let’s see the format() method.

The syntax of format method is string.format(value1,…).

Values in statement stored inside the placeholders({}).

Inside placeholders, you can use 3 different types of formatting.

For example,

str.format() method

In the above example, you can see the string formatting using three different conditions.

The first type is using numbered indexes.

The second type, the index is an empty placeholder.

And the last one is the variable inside the placeholder({}).

Second, the old method for python string formatting is the % operator.

In this method, you can format string using the %s operator.

Integer using %d and float can be inserted using the %, f operator.

For example,

% operator for string formatting

In the above example, you can see that I’ve formatted the string using the % operator.

All of the above, two are great methods for string formatting.

But when variables increases. so, it becomes hard to find the value for variables.

For example,

% and format()

In the above example, you can see that when variables increase then it becomes hard to string formatting.

The reason is that because the number of placeholders increases. And we have to find the right value for the right variables.

Hence, In 2015 python community proposes a new method for string formatting that is fstring.

Here, the Python fstring came

The python fstring method format the string in an easy way.

It has introduced in PEP 498 by Eric V. Smith.

Python fstring method is available for python 3.6 and after it.

First, you’ll provide f’ then statement Inside single quote {variable}.

For example,

the fstring method

You can provide multiple placeholders inside the single quote.

For example,

Passing multiple fstrings

In the above example, you can see that I’ve passed multiple variables in fstring.

Hence, you can format strings easily with the fstring method.

The benfits of fstring

In the previous section, I’ve shown you some examples of it.

From that examples, you can see that it has easy to use syntax.

The second thing, It’s taking less execution time than the other two methods.

Also, you can pass multiple values to it.

Hence, you’ll remain consistent in string formatting.

But in another two methods, you can get confused.

The fstring in python dictionary

So, you’ve seen that how easily you can use python fstring.

Also, we can use fstring in the python dictionary.

In the dictionary, you can place variables and keys inside [].

This entity will present inside the placeholders({}).

For example,

fstring in the dictionary

Here, you can see that I’ve placed the key and variable inside the placeholders({}).

Hence, you can place the value in a statement using a key.

Format the loop with python fstring

Loops are important to get a sequence of values.

Likewise, you can get a loop through fstring.

For example,

fstring in loop

In the above example, you can see that I’ve used fstring to format the loop.

So, you can format the loop with fstring.

Fstring is fast

I’ve told you that fstring is fast.

fstring in the terms of the execution speed of the program.

First., we will check the execution speed of the str. format() method.

For example,

Speed of the str.format()

This is the execution speed of the str.format() method.

Second, you’re going to see the execution speed of the % formatting.

For example,

the speed of the %formatting

In the above example, you can see that the % string formatting method is slightly faster than str.format() method.

Also, you can find the execution speed of the fstring method.

For example,

the execution speed of the fstring

This is the!

The execution speed of the fstring is faster than the format() and % method.

Also, the f means in fstring is fast.

So, you’ve seen how the fstring is faster than another two methods.

Here, I’ve used timeit() method for execution speed.

It’s not an enemy of format() and %

Although, it is better than another two methods.

But the python community doesn’t propose to remove these two.

Because the fstring method introduces in python 3.6. And most people using prior to 3.6.

So, for them, fstring isn’t available.

Also, these two are useful in small formatting tasks.

Hence, you can say that they are friends working in different python versions.

So, what’s your favourite string formatting method? I’m eager to know!!

Conclusion

Finally, you’ve seen the fstring method.

fstring method use in string formatting.

In string formatting, you place the values in a statement.

The fstring method was Introduce in python 3.6.

Before python 3.6 there are two methods of string formatting. Those are format() and % method.

As you’ve seen, these two methods have slow execution speeds.

In fstring f means fast.

So, which version of python you’re using?

People also reading:

How-to guide?

Avatar for Vipul kunwar
Follow me

    Leave a Reply

    Your email address will not be published. Required fields are marked *

    Back to top