How to concatenate multiple Python strings together efficiently?

WBOY
Release: 2023-08-25 17:37:06
forward
736 people have browsed it

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())
Copy after login

Output

Execution Time =  0.009820308012422174
Copy after login

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())
Copy after login

Output

Execution Time =  0.0876248900021892
Copy after login

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)
Copy after login

Output

0.0022547245025634766
Copy after login

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)
Copy after login

Output

0.000995635986328125
Copy after login

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!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template