String formatting and concatenation

I see a lot of people using these format strings:

root = "sample" output = "output" path = "{}/{}".format(root, output) 

Instead of just concatenating such strings:

 path = root + '/' + output 

Do format strings have better performance or is it just for appearance?

+13
source share
7 answers

This is for appearance only. You can immediately see what kind of format it is. Many of us like readability better than microoptimization.

Let's see what IPython %timeit :

 Python 3.7.2 (default, Jan 3 2019, 02:55:40) IPython 5.8.0 Intel(R) Core(TM) i5-4590T CPU @ 2.00GHz In [1]: %timeit root = "sample"; output = "output"; path = "{}/{}".format(root, output) The slowest run took 12.44 times longer than the fastest. This could mean that an intermediate result is being cached. 1000000 loops, best of 5: 223 ns per loop In [2]: %timeit root = "sample"; output = "output"; path = root + '/' + output The slowest run took 13.82 times longer than the fastest. This could mean that an intermediate result is being cached. 10000000 loops, best of 5: 101 ns per loop In [3]: %timeit root = "sample"; output = "output"; path = "%s/%s" % (root, output) The slowest run took 27.97 times longer than the fastest. This could mean that an intermediate result is being cached. 10000000 loops, best of 5: 155 ns per loop In [4]: %timeit root = "sample"; output = "output"; path = f"{root}/{output}" The slowest run took 19.52 times longer than the fastest. This could mean that an intermediate result is being cached. 10000000 loops, best of 5: 77.8 ns per loop 
+14
source

As in most cases, there will be a difference in performance, but ask yourself: "Does it really matter if it's faster?". The root + '/' output method can be quickly and easily printed. But it can be complicated to read quickly when you have several variables to print

 foo = "X = " + myX + " | Y = " + someY + " Z = " + Z.toString() 

vs

 foo = "X = {} | Y= {} | Z = {}".format(myX, someY, Z.toString()) 

What is easier to understand what is happening? If you really don't need to perform performance, choose a method that will be easier for people to read and understand.

+8
source

The row format does not contain data when binding data. While in concatenation we have to type a letter or transform the data accordingly.

For instance:

 a = 10 b = "foo" c = str(a) + " " + b print c > 10 foo 

This can be done using line formatting:

 a = 10 b = "foo" c = "{} {}".format(a, b) print c > 10 foo 

Thus, with-in placeholders {} {} , we assume that two things should come next, that is, in this case a and b .

+7
source

This is not only for โ€œviewsโ€, or for powerful transformations of the lexical type; it is also necessary for internationalization.

You can change the format string depending on the selected language.

With a long string of concatenations of strings baked into the source code, this becomes almost impossible for proper operation.

+7
source

I agree that formatting is mainly used for readability, but since the release of f-rows in 3.6 tables have turned in terms of performance. I also think that f-lines are more readable / supported, since 1) they can be read left-right, as in most ordinary texts, and 2) the concatenation disadvantages associated with the interval are eliminated, since the variables are built-in.

Running this code:

 from timeit import timeit runs = 1000000 def print_results(time, start_string): print(f'{start_string}\n' f'Total: {time:.4f}s\n' f'Avg: {(time/runs)*1000000000:.4f}ns\n') t1 = timeit('"%s, %s" % (greeting, loc)', setup='greeting="hello";loc="world"', number=runs) t2 = timeit('f"{greeting}, {loc}"', setup='greeting="hello";loc="world"', number=runs) t3 = timeit('greeting + ", " + loc', setup='greeting="hello";loc="world"', number=runs) t4 = timeit('"{}, {}".format(greeting, loc)', setup='greeting="hello";loc="world"', number=runs) print_results(t1, '% replacement') print_results(t2, 'f strings') print_results(t3, 'concatenation') print_results(t4, '.format method') 

gives this result on my machine:

 % replacement Total: 0.3044s Avg: 304.3638ns f strings Total: 0.0991s Avg: 99.0777ns concatenation Total: 0.1252s Avg: 125.2442ns .format method Total: 0.3483s Avg: 348.2690ns 

A similar answer to another question is given on this answer .

+5
source

Starting in Python 3.6, you can interpolate literal strings by first typing f into a string:

 root = "sample" output = "output" path = f"{root}/{output}" 
+3
source

This is for viewing and saving code. Editing your code is very convenient if you used the format. Also, when you use +, you can skip details such as spaces. Use the format for your and possible accompaniment.

+2
source

All Articles