This article mainly introduces three methods for implementing string connection in python, as well as their efficiency and application scenarios. it has some reference value. if you are interested, you can refer to it. This article mainly introduces three methods for implementing string connection in python, as well as their efficiency and application scenarios. it has some reference value. if you are interested, you can refer to it.
There are three methods to connect python strings:
Method 1: connect directly with the plus sign (+) operator
website = 'python' + 'tab' + '.com'
Method 2: join
listStr = ['python', 'tab', '.com'] website = ''.join(listStr)
Method 3: Replace
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.
Comparison object: Plus sign connection VS join connection
Python version: python2.7
System environment: CentOS
Lab 1:
# -*- 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:
0.641695976257
0.341440916061
Lab 2:
# -*- 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:
0.0265691280365
0.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.
The above is all the content of this article. I hope it will help you learn and support PHP.
For more information about the three methods for implementing string connection in python and their efficiency and application scenarios, see The PHP Chinese network!