Home  >  Article  >  Backend Development  >  How to concatenate multiple Python strings together efficiently?

How to concatenate multiple Python strings together efficiently?

WBOY
WBOYforward
2023-08-25 17:37:06735browse

How to concatenate multiple Python strings together efficiently?

The most efficient way to concatenate many Python Strings together depends on what task you want to fulfil. We will see two ways with four examples and compare the execution time −

  • When there are fewer strings, use the operator to connect
  • Concatenate using the Joins when the strings are less
  • When there are many strings, use the operator to connect
  • When there are many strings, use the connection operator to connect

Let’s start with the example

Concatenate Strings using the Operator

The Chinese translation of

Example

is:

Example

Let us concatenate using the operator. The timeit() is used to measure the execution time taken by the given code −

import timeit as t

# Concatenating 5 strings
s = t.Timer(stmt="'Amit' + 'Jacob' + 'Tim' +'Tom' + 'Mark'")

# Displaying the execution time
print("Execution Time = ",s.timeit())

Output

Execution Time =  0.009820308012422174

Use Join to connect strings

The Chinese translation of

Example

is:

Example

Let us use the .join() method to join. The timeit() function is used to measure the execution time of a given code.

import timeit as t

# Concatenating 5 strings
s = t.Timer(stmt="''.join(['Amit' + 'Jacob' + 'Tim' +'Tom' + 'Mark'])")

# Displaying the execution time
print("Execution Time = ",s.timeit())

Output

Execution Time =  0.0876248900021892

As shown above, using the operator is more efficient. It takes less time for execution.

Use operator to connect multiple strings

The Chinese translation of

Example

is:

Example

We will now concatenate many strings and check the execution time using the time module −

from time import time

myStr =''
a='gjhbxjshbxlasijxkashxvxkahsgxvashxvasxhbasxjhbsxjsabxkjasjbxajshxbsajhxbsajxhbasjxhbsaxjash'
l=[]

# Using the + operator
t=time()
for i in range(1000):
   myStr = myStr+a+repr(i)
print(time()-t)

Output

0.0022547245025634766

Concatenate Many Strings using the Join

The Chinese translation of

Example

is:

Example

We will now use Join to concatenate many strings and check the execution time. Concatenation is better and faster option when we have many strings −

from time import time

myStr =''
a='gjhbxjshbxlasijxkashxvxkahsgxvashxvasxhbasxjhbsxjsabxkjasjbxajshxbsajhxbsajxhbasjxhbsaxjash'
l=[]

# Using the + operator
t=time()
for i in range(1000):
   l.append(a + repr(i))
z = ''.join(l)
print(time()-t)

Output

0.000995635986328125

As shown above, when there are many strings, it is more efficient to use the join() method. It takes less time to execute.

The above is the detailed content of How to concatenate multiple Python strings together efficiently?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete