Three commonly used and most efficient Python string concatenation methods

There are generally three ways to concatenate strings in python:

Method 1:
Concatenate website=&39;python&39;+&39;tab&39;+&39;com&39; directly through the plus (+) operator

Method 2:
There are generally three ways to concatenate python strings:

  • 1: Connect directly via the plus (+) operator
website = 'python' + 'baidu' + '.com'
  • 2: join method
listStr = ['python', 'baidu', '.com'] 
website = ''.join(listStr)
  • 3: replace
 website = '%s%s%s' % ('python', 'baidu', '.com')

Next, let’s talk about the difference between the three methods

  • Method 1
    is simple and straightforward to use, but many people on the Internet say that this method is inefficient. The reason why it is said that the operation of using + for string concatenation in python is inefficient is because strings in python are immutable types. Use +
    to connect two When a string is generated, a new string will be generated. To generate a new string, you need to re-apply for memory. When there are many strings that are added continuously (a+b+c+d+e+f+…)
    , low efficiency is inevitable yes
  • Method 2
    is slightly more complicated to use, but it is efficient when connecting multiple characters, and there is only one memory application. And if it is to connect the characters of the list, this method must be the first choice
  • Method 3
    String formatting, this method is very commonly used, and I also recommend using this method

The following experiments are used to illustrate the efficiency of string concatenation.

Case number one:

from time import time
def method1():
    t = time()
    for i in range(100000):
        s = 'python'+'python'+'python'+'python'+'python'+'python'+'python'+'python'+'python'+'python'+'python'+'python'+'python'+'python'+'python'+'python'+'python'+'python'+'python'+'python'+'python'+'python'+'python'+'python'+'python'+'python'+'python'+'python'+'python'+'python'+'python'+'python'+'python'+'python'+'python'+'python'+'python'+'python'+'python'+'python'+'python'+'python'+'python'+'python'+'python'+'python'+'python'+'python'+'python'+'python'+'python'+'python'+'python'+'python'+'python'+'python'+'python'+'python'+'python'+'python'+'python'+'python'+'python'+'python'+'python'+'python'+'python'+'python'+'python'+'python'
    print (time() - t)
def method2():
    t = time()
    for i in range(100000):
        s = ''.join(['python','python','python','python','python','python','python','python','python','python','python','python','python','python','python','python','python','python','python','python','python','python','python','python','python','python','python','python','python','python','python','python','python','python','python','python','python','python','python','python','python','python','python','python','python','python','python','python','python','python','python','python','python','python','python','python','python','python','python','python','python','python','python','python','python','python','python','python','python','python'])
    print (time() -t)
method1()
method2()

Case 2:

from time import time
def method1():
    t = time()
    for i in range(100000):
        s = 'python'+'python'+'python'+'python'
    print (time() - t)
def method2():
    t = time()
    for i in range(100000):
        s = ''.join(['python','python','python','python'])
    print (time() -t)
method1()
method2()

result:

0.002992391586303711
0.01495981216430664

The above two experiments have 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 continuously. If the number of connections is small, the efficiency of the plus sign connection is higher than that of the join connection.

Guess you like

Origin blog.csdn.net/m0_67575344/article/details/123908179