F-Strings formatting is not only the most modern approach, it's also the most performant
This post was written a long time ago and its contents and code might be outdated or not aligned to current industry standards.
Please proceed with caution :-)
Intro
There are many ways to format strings in python, concatenation, modulus, ordered, named, and f-strings.
Making sure that all python3+ applications in your ecosystem use f-strings is not only
a matter of standardizing the code base and enforcing best practices among all teammates.
It’s also a matter of performance, different string formatting methods can swing performance in up to 55%.
This is a high number, especially in applications with heavy dependency on string formatting.
Some may argue that saving a 1-2 of seconds per millions of actions is not that important,
but :
- Faster is always better than slower.
- Think about scaling, eventually it’s a numbers game. String formatting is a CPU bound task, higher CPU utilization means spending more money, or hogging resources from other services and applications.
A little test
And the results
As you can see, f-strings formatting is faster by 37.5% - 55% than other methods. This is insignificant number in small deployments, but will definitely reduce execution time and budget when scaled.