Fstrings
If you are using Python 3.6+ , you can use the new so-called f-strings , which denotes formatted strings, and you can use it by adding the f character at the beginning of the line to identify it as f-string .
price = 123 name = "Jerry" print(f"{name}!!, {price} is much, isn't {price} a lot? {name}!") >Jerry!!, 123 is much, isn't 123 a lot? Jerry!
The main advantages of using f-strings are that they are more readable, can be faster and offer better performance:
Pandas Source for Everyone: Python Data Analysis, Daniel Y. Chen
Benchmarks
Without a doubt, the new f-strings more readable, since you do not need to reassign the lines, but is it faster as indicated in the above quote?
price = 123 name = "Jerry" def new(): x = f"{name}!!, {price} is much, isn't {price} a lot? {name}!" def old(): x = "{1}!!, {0} is much, isn't {0} a lot? {1}!".format(price, name) import timeit print(timeit.timeit('new()', setup='from __main__ import new', number=10**7)) print(timeit.timeit('old()', setup='from __main__ import old', number=10**7)) > 3.8741058271543776
Running the 10 million test, it seems that the new f-strings actually works faster when matching.
user1767754 Dec 19 '17 at 8:51 on 2017-12-19 08:51
source share