Which is the preferred way to concatenate a string in Python?

前端 未结 12 850
眼角桃花
眼角桃花 2020-11-22 06:45

Since Python\'s string can\'t be changed, I was wondering how to concatenate a string more efficiently?

I can write like it:

s += string         


        
相关标签:
12条回答
  • 2020-11-22 06:56

    The best way of appending a string to a string variable is to use + or +=. This is because it's readable and fast. They are also just as fast, which one you choose is a matter of taste, the latter one is the most common. Here are timings with the timeit module:

    a = a + b:
    0.11338996887207031
    a += b:
    0.11040496826171875
    

    However, those who recommend having lists and appending to them and then joining those lists, do so because appending a string to a list is presumably very fast compared to extending a string. And this can be true, in some cases. Here, for example, is one million appends of a one-character string, first to a string, then to a list:

    a += b:
    0.10780501365661621
    a.append(b):
    0.1123361587524414
    

    OK, turns out that even when the resulting string is a million characters long, appending was still faster.

    Now let's try with appending a thousand character long string a hundred thousand times:

    a += b:
    0.41823482513427734
    a.append(b):
    0.010656118392944336
    

    The end string, therefore, ends up being about 100MB long. That was pretty slow, appending to a list was much faster. That that timing doesn't include the final a.join(). So how long would that take?

    a.join(a):
    0.43739795684814453
    

    Oups. Turns out even in this case, append/join is slower.

    So where does this recommendation come from? Python 2?

    a += b:
    0.165287017822
    a.append(b):
    0.0132720470428
    a.join(a):
    0.114929914474
    

    Well, append/join is marginally faster there if you are using extremely long strings (which you usually aren't, what would you have a string that's 100MB in memory?)

    But the real clincher is Python 2.3. Where I won't even show you the timings, because it's so slow that it hasn't finished yet. These tests suddenly take minutes. Except for the append/join, which is just as fast as under later Pythons.

    Yup. String concatenation was very slow in Python back in the stone age. But on 2.4 it isn't anymore (or at least Python 2.4.7), so the recommendation to use append/join became outdated in 2008, when Python 2.3 stopped being updated, and you should have stopped using it. :-)

    (Update: Turns out when I did the testing more carefully that using + and += is faster for two strings on Python 2.3 as well. The recommendation to use ''.join() must be a misunderstanding)

    However, this is CPython. Other implementations may have other concerns. And this is just yet another reason why premature optimization is the root of all evil. Don't use a technique that's supposed "faster" unless you first measure it.

    Therefore the "best" version to do string concatenation is to use + or +=. And if that turns out to be slow for you, which is pretty unlikely, then do something else.

    So why do I use a lot of append/join in my code? Because sometimes it's actually clearer. Especially when whatever you should concatenate together should be separated by spaces or commas or newlines.

    0 讨论(0)
  • 2020-11-22 06:59

    You can do in different ways.

    str1 = "Hello"
    str2 = "World"
    str_list = ['Hello', 'World']
    str_dict = {'str1': 'Hello', 'str2': 'World'}
    
    # Concatenating With the + Operator
    print(str1 + ' ' + str2)  # Hello World
    
    # String Formatting with the % Operator
    print("%s %s" % (str1, str2))  # Hello World
    
    # String Formatting with the { } Operators with str.format()
    print("{}{}".format(str1, str2))  # Hello World
    print("{0}{1}".format(str1, str2))  # Hello World
    print("{str1} {str2}".format(str1=str_dict['str1'], str2=str_dict['str2']))  # Hello World
    print("{str1} {str2}".format(**str_dict))  # Hello World
    
    # Going From a List to a String in Python With .join()
    print(' '.join(str_list))  # Hello World
    
    # Python f'strings --> 3.6 onwards
    print(f"{str1} {str2}")  # Hello World
    

    I created this little summary through following articles.

    • Python 3's f-Strings: An Improved String Formatting Syntax (Guide) (Speed test also included)
    • Formatted string literals
    • String Concatenation and Formatting
    • Splitting, Concatenating, and Joining Strings in Python
    0 讨论(0)
  • 2020-11-22 07:02

    While somewhat dated, Code Like a Pythonista: Idiomatic Python recommends join() over + in this section. As does PythonSpeedPerformanceTips in its section on string concatenation, with the following disclaimer:

    The accuracy of this section is disputed with respect to later versions of Python. In CPython 2.5, string concatenation is fairly fast, although this may not apply likewise to other Python implementations. See ConcatenationTestCode for a discussion.

    0 讨论(0)
  • You write this function

    def str_join(*args):
        return ''.join(map(str, args))
    

    Then you can call simply wherever you want

    str_join('Pine')  # Returns : Pine
    str_join('Pine', 'apple')  # Returns : Pineapple
    str_join('Pine', 'apple', 3)  # Returns : Pineapple3
    
    0 讨论(0)
  • 2020-11-22 07:08

    Using in place string concatenation by '+' is THE WORST method of concatenation in terms of stability and cross implementation as it does not support all values. PEP8 standard discourages this and encourages the use of format(), join() and append() for long term use.

    As quoted from the linked "Programming Recommendations" section:

    For example, do not rely on CPython's efficient implementation of in-place string concatenation for statements in the form a += b or a = a + b. This optimization is fragile even in CPython (it only works for some types) and isn't present at all in implementations that don't use refcounting. In performance sensitive parts of the library, the ''.join() form should be used instead. This will ensure that concatenation occurs in linear time across various implementations.

    0 讨论(0)
  • 2020-11-22 07:11

    The recommended method is still to use append and join.

    0 讨论(0)
提交回复
热议问题