Three methods of python string connection, their efficiency, and applicable scenarios are described in detail.
There are generally three methods to connect python strings: Method 1: directly connect to website = & 39; python & 39; + & 39; tab & 39; + & 39; com & 39; method 2
There are three methods to connect python strings:
Method 1: connect directly with the plus sign (+) Operator
| 1 |
website = 'python' + 'tab' + '.com' |
Method 2: join
| 12 |
listStr = ['python', 'tab', '.com'] website = ''.join(listStr) |
Method 3: replace
| 1 |
website = '%s%s%s' % ('python', 'tab', '.com') |
Next, let's talk about the differences between the three methods.
Method 1 is simple and straightforward, but many people on the Internet say this method is inefficient.
In python, the efficiency of using + to connect strings is low because strings in python are unchangeable. When using + to connect two strings, a new string is generated, to generate a new string, you need to re-apply for memory. When many strings are added consecutively (a + B + c + d + e + f + ...), inefficiency is inevitable.
Method 2: The usage is slightly complicated, but the connection validity of multiple characters is high, and only one memory application will be performed. In addition, this method must be the first choice when the list character is connected.
Method 3: String formatting. This method is very common and is recommended.
The following uses experiments to illustrate the efficiency of string connection.
| 123 |
Comparison object: plus sign connectionjoinConnectionPython version: python2.7System Environment: CentOS |
Lab 1:
| 1234567891011121314 |
# -*- 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','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() -tmethod1()method2() |
Result:
| 12 |
0.6416959762570.341440916061 |
Lab 2:
| 1234567891011121314 |
# -*- 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() |
Result:
| 12 |
0.02656912803650.0522091388702 |
The above two experiments show completely different results. The only difference between the two experiments is the number of string connections.
Conclusion: the low efficiency of the plus sign connection occurs when multiple strings are connected consecutively. If the number of connections is small, the efficiency of the plus sign connection is higher than that of the join connection.