Pros and cons of Python’s f-strings literal

In this post, I would like to give my opinion about Python’s f-strings literal. On August 1, 2015, PEP 498 was released. It was about “Literal String Inteprolation”. PEP 498 implemented first in Python 3.6.

This PEP is driven by the desire to have a simpler way to format strings in Python. The existing ways of formatting are either error prone, inflexible, or cumbersome.

Python PEP 498

My perspective of using f-strings literal

Good side

In my opinion, having a simpler syntax is what makes Python more Pythonic. As we all know, Python simply has 3 methods of string formatting. Those 3 methods are using the traditional %-formatting, using str.format(), and using the f-strings literal. Lets compare them!

a, b, c = 1, 2, 3
# traditional %-formatting
print("alice: %d, bob: %d, cat: %d" % (a, b, c))
# str.format()
print("alice: {}, bob: {}, cat: {}".format(a, b, c))
# f-strings literal
print(f"alice: {a}, bob: {b}, cat: {c}"

As you can see from the above example, using f-strings literal makes everything shorter. Also, f-strings literal are more readable compared to the others. This is very helpful when formatting a long string.

From the performance perspective.

F-strings are fast! Much faster than %-formatting and str.format() — the two most commonly used string formatting mechanisms.

Syed Komail Abbs, Hackernoon

According to that statement, it is true that the f-strings literal are faster compared to the others. Syed Komail Abbs said in the article that the concept of f-strings literal is what makes it faster than the others.

Bad side

Using f-strings literal is not always a good choice. Having good impact doesn’t mean that it doesn’t have bad impact when used. F-strings also have a negative side. Especially when dealing with old Python versions. I experience this a lot of times. Using f-strings literal means that you must have at least Python 3.6 installed. Installing the latest Python isn’t really a big deal. Having things more compact and simple is better.

Having things more compact and simple is better. Imagine that you have created an automation script (with f-strings implemented), that will be executed by bunch of computers running Python 3.5. It will be better to implement the traditional %-formatting rather than adding another task of installing the latest Python.

Conclusion

Python’s f-strings literal are great. This thing is what makes Python better. F-strings haves a shorter, more readable syntax, and also much faster. But, it is not always better. One of the worst cases is when having to deal with an older Python version. F-strings are better if you have the latest Python version. It is also better when speed and readability matters. If you have an older version of Python or backward compatibility is very important, then you should stick to the traditional %-formatting. At the end of the day, I would like to say that this is my perspective and you have your own perspective. It doesn’t matter which one is better because everything depends on its case. I hope that this article could be a good answer for you.

Leave a Reply

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