Three methods of Python string connection and their efficiency and application scenarios

Source: Internet
Author: User

The Python string connection method, generally has the following three kinds: Method 1: Directly through the plus (+) operator connection website=& 39;python& 39;+& 39;tab& 39;+& 39; com& 39; Method 2

The Python string connection method, generally has the following three kinds:

Method 1: Connect directly via the plus (+) operator
1 website =‘python‘ + ‘tab‘ +‘.com‘
Method 2:join Method
12 listStr =[‘python‘‘tab‘‘.com‘website =‘‘.join(listStr)
Method 3: Replace
1 website =‘%s%s%s‘ %(‘python‘‘tab‘‘.com‘)

Let's take a look at the difference between the three ways.

Method 1, simple and straightforward to use, but many online people say this method is inefficient

The reason why Python uses + for string connections is inefficient because the strings in Python are immutable types, a new string is generated when using + to concatenate two strings, and a new string is required to re-request the memory, when the string of successive additions is many (a+b+c+d+ e+f+ ...) , inefficiency is inevitable.

Method 2 uses a slightly more complex, but high-efficiency connection to multiple characters, with only one memory request. And if the character of the list is connected, this method must be the preferred

Method 3: String formatting, this method is very common, I also recommend the use of this method

The following experiment is used to illustrate the efficiency of string connections.
123 比较对象:加号连接 VS join连接python版本: python2.7系统环境:CentOS

Experiment One:

1234567891011121314 # -*- coding: utf-8 -*-fromtime importtimedefmethod1():    =time()    forinxrange(100000):        =‘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‘    printtime() -tdefmethod2():    =time()    forinxrange(100000):        =‘.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()

Results:

12 0.6416959762570.341440916061

Experiment Two:

1234567891011121314 # -*- coding: utf-8 -*-fromtime import timedefmethod1():    =time()    forinxrange(100000):        =‘pythontab‘+‘pythontab‘+‘pythontab‘+‘pythontab‘    printtime() -tdef method2():    =time()    forinxrange(100000):        =‘.join([‘pythontab‘,‘pythontab‘,‘pythontab‘,‘pythontab‘])    printtime() -tmethod1()method2()

Results:

12 0.02656912803650.0522091388702

The above two experiments have completely different results, the only difference between the two experiments is: the number of string connections.

Conclusion: The low-plus connection is inefficient when multiple strings are connected consecutively, and if the number of connections is less, the plus connection efficiency is higher than the join connection efficiency.

Three methods of Python string connection and their efficiency and application scenarios

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.