SoFunction
Updated on 2025-03-02

Three methods of implementing string concatenation in Python, their efficiency and detailed explanation of applicable scenarios

There are generally three ways to concatenate Python strings:

Method 1: Directly connect via the plus sign (+) 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 talk about the differences between the three methods

Method 1: It is simple and direct to use, but many people on the Internet say that this method is inefficient

The reason why using + in Python for string concatenation is inefficient is that strings in Python are immutable types. When using + to concatenate two strings, a new string will be generated. To generate a new string, you need to re-apply for memory. When there are many strings added continuously (a+b+c+d+e+f+...), inefficiency is inevitable.

Method 2: The use is a bit complicated, but it is efficient when connecting multiple characters, and there will be only one memory application. And if you are connecting the list characters, this method must be the first choice.

Method 3: String formatting, this method is very commonly used, I also recommend using this method

The following is an experiment to illustrate the efficiency of string concatenation.

Comparison object: plus sign connection vs join connection

Python version: python2.7

System environment: CentOS

Experiment 1:

# -*- coding: utf-8 -*-

from time import time

def 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() - t

def 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() -t

method1()

method2()

result:

0.641695976257

0.341440916061

Experiment 2:

# -*- coding: utf-8 -*-

from time import time

def method1():

  t = time()

  for i in xrange(100000):

    s = 'pythontab'+'pythontab'+'pythontab'+'pythontab'

  print time() - t

def method2():

  t = time()

  for i in xrange(100000):

    s = ''.join(['pythontab','pythontab','pythontab','pythontab'])

  print time() -t

method1()

method2()

result:

0.0265691280365

0.0522091388702

The above two experiments have completely different results. The only difference between the analysis of these two experiments is: the number of string concatenation.

Conclusion: The low efficiency of plus sign connection occurs when multiple strings are connected continuously. If the number of connections is small, the efficiency of plus sign connection is higher than that of join connection.

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.