The Python string connection method, generally has the following three kinds:
Method 1: Connect directly via the plus (+) operator
Website = ' python ' + ' tab ' + '. com '
Method 2:join Method
Liststr = [' Python ', ' tab ', '. com '] website = '. Join (LISTSTR)
Method 3: Replace
Website = '%s%s%s '% (' python ', ' tab ', '. com ')
Let's take a look at the difference between the three ways.
Method 1, simple and straightforward to use, but many online people say this method is inefficient
The reason why Python uses + for string connections is inefficient because the strings in Python are immutable types, a new string is generated when using + to concatenate two strings, and a new string is required to re-request the memory, when the string of successive additions is many (a+b+c+d+ e+f+ ...) , inefficiency is inevitable.
Method 2 uses a slightly more complex, but high-efficiency connection to multiple characters, with only one memory request. And if the character of the list is connected, this method must be the preferred
Method 3: String formatting, this method is very common, I also recommend the use of this method
The following experiment is used to illustrate the efficiency of string connections.
Compare objects: Plus connection VS join connection Python version: python2.7 system environment: CentOS
Experiment One:
#-*-Coding:utf-8-*-from time import timedef method1 (): T = time () for I in Xrange (100000): s = ' Pythontab ' + ' pythontab ' + ' pythontab ' + ' pythontab ' + ' pythontab ' + ' pythontab ' + ' pythontab ' + ' pythontab ' + ' pythontab ' + ' pythontab ' + ' Pythontab ' + ' pythontab ' + ' pythontab ' + ' pythontab ' + ' pythontab ' + ' pythontab ' + ' pythontab ' + ' pythontab ' + ' + ' pythontab ' + ' Pythontab ' + ' pythontab ' + ' pythontab ' + ' pythontab ' + ' pythontab ' + ' pythontab ' + ' pythontab ' + ' pythontab ' + ' pythontab ' + ' Pythontab ' + ' pythontab ' + ' pythontab ' + ' pythontab ' + ' pythontab ' + ' pythontab ' + ' pythontab ' + ' pythontab ' + ' pythontab ' + ' Pythontab ' + ' pythontab ' + ' pythontab ' + ' pythontab ' + ' pythontab ' + ' pythontab ' + ' pythontab ' + ' pythontab ' + ' pythontab ' Print time ()-TdeF method2 (): T = time () for I in Xrange (100000): s = ". Join ([' Pythontab ', ' pythontab ', ' Pyth Ontab ', ' pythontab ', ' pythontab ', ' pythontab ', ' pythontab ', ' pythontab ', ' pythontab ', ' pythontab ', ' pythontab ', ' Pythontab ', ' pythontab ', ' pythontab ', ' pythontab ', ' pythontab ', ' pythontab ', ' pythontab ', ' pythontab ', ' Pythontab ',' Pythontab ', ' pythontab ', ' pythontab ', ' pythontab ', ' pythontab ', ' pythontab ', ' pythontab ', ' pythontab ', ' pythontab ', ' Pythontab ', ' pythontab ', ' pythontab ', ' pythontab ', ' pythontab ', ' pythontab ', ' pythontab ', ' pythontab ', ' pythontab ', ' Pythontab ', ' pythontab ', ' pythontab ', ' pythontab ', ' pythontab ', ' pythontab ', ' pythontab ', ' Pythontab ']) print time ()- Tmethod1 () method2 ()
Results:
0.6416959762570.341440916061
Experiment Two:
#-*-Coding:utf-8-*-from time import timedef method1 (): t = time () for I in Xrange (100000): s = ' pythontab ' + ' pythontab ' + ' pythontab ' + ' pythontab ' print Time ()-TdeF method2 (): t = time () for I in Xrange (100000): s = '. Join ([' Pythontab ', ' pythontab ', ' pythontab ', ' Pythontab ']) print time ()-tmethod1 () method2 ()
Results:
0.02656912803650.0522091388702
The above two experiments have completely different results, the only difference between the two experiments is: the number of string connections.
Conclusion: The low-plus connection is inefficient when multiple strings are connected consecutively, and if the number of connections is less, the plus connection efficiency is higher than the join connection efficiency.