Conclusion: f-strings are fastest in CPython because it builds the strings straight from stack
to improve performance. A new Opcode BUILD_STRING
was added specifically to solve this problem.
there are many ways to format a string in python
"My name is " + my_name + "."
"My name is %s.".format(my_name)
"".join(["My name is " , my_name , "."])
f"My name is {my_name}."
We can think about some aspects to consider when choosing which method to use.
First,need to decide which implementation are we using? CPython? PyPI? Rust-Python? CPython is most used, so talking about that.
Then,
This blog is about performance part. For Usability you can refer to many good posts, or realpython.org, or google python guide.
Most performance issue in CPython is due to creation of PyObject
structs. While strings look mutable in python source code, under the hood new PyObject
is created everytime a string is updated.
so "My name is " + my_name + "." usually shows worst performance.
% format
str.format()
''.join(list_of_str_to_join)
these 3 joins the string at once, but with additional parsing.
f-string, introduced at python 3.6, called ''.join() as subroutine in its alpha stage. Then in this discussion, a patch was made to join them straight from the stack using a new Opcode BUILD_STRING
.
refrences
https://hg.python.org/cpython/rev/28e280915508#l10.1
https://bugs.python.org/issue27078