Imagine you have four strings: a, b, c and d.
Say you want their concatenation, e = a + b + c + d. However, the + operator is only defined for two operands, so you would apparently need to concatenate the individual strings one by one.
The naive way to calculate this might be the following:
e = ((a + b) + c) + d
But this is inefficient, because it generates two throwaway strings: a + b and a + b + c.
Now, imagine we created a buffer-like object holding all the characters we wanted to combine:
e_buffer = [a, b, c, d]
We could create e from this all at once, avoiding the need to create many intermediate strings.
This requires a function, which, in Python, we call join; it is a method of a str, and intersperses that string between the arguments provided. Therefore, when you execute some_separator.join([a, b, c]), you get, in effect, a + some_separator + b + some_separator + c.
To answer your question: in general, when concatenating many strings, it should be faster to use join, and it will at least be more readable, especially when using a separator. For the case given in the question, I would instead use an f-string:
def greeting(name: str) -> str:
return f'Hello {name}'
Answer from gmds on Stack Overflowcpython - How use ' '.join() in Python? - Stack Overflow
Python .join or string concatenation - Stack Overflow
Why is .join() the way it is?
join() or '+' for string concatenation
If a dictionary has integer type keys and string type values, can we concatenate the values using the join() function?
If a dictionary has integer type keys and string type values, what happens if we attempt concatenating the keys using the join() function?
If you're creating a string like that, you normally want to use string formatting:
>>> user = 'username'
>>> host = 'host'
>>> '%s@%s' % (user, host)
'username@host'
Python 2.6 added another form, which doesn't rely on operator overloading and has some extra features:
>>> '{0}@{1}'.format(user, host)
'username@host'
As a general guideline, most people will use + on strings only if they're adding two strings right there. For more parts or more complex strings, they either use string formatting, like above, or assemble elements in a list and join them together (especially if there's any form of looping involved.) The reason for using str.join() is that adding strings together means creating a new string (and potentially destroying the old ones) for each addition. Python can sometimes optimize this away, but str.join() quickly becomes clearer, more obvious and significantly faster.
I take the question to mean: "Is it ok to do this:"
ret = user + '@' + host
..and the answer is yes. That is perfectly fine.
You should, of course, be aware of the cool formatting stuff you can do in Python, and you should be aware that for long lists, "join" is the way to go, but for a simple situation like this, what you have is exactly right. It's simple and clear, and performance will not be an issue.
<rant>
I've been programming in Python for almost a decade now and I love most things about it. One thing that never felt right to me was how a string's .join() method works. In short, ' + '.join(['a', 'b', 'c']) returns 'a + b + c'.
Wouldn't it make more sense to have the join be a member function of the list/iterable? You could apply the same type constraints. I'd even be comfortable with it floating alone in the namespace and calling something like join(['a', 'b', 'c'], ' + ').
In short, the current way feels backwards in some way that I can't explain an it's grinding my gears rn. Carry on.
</rant>
I know there are several ways to concatenate strings. For the most basic cases, such as joining a and b here..
a = "ABCD" b = "EFGH"
.. which is recommended?
a + b
''.join([a, b])
I find many examples of people using join(), but I don't see how this is an advantage in the simplest of cases. One disadvantage is it consumes 10 additional characters on the line of code, which is significant if you're trying to keep lines at 79 characters or less.
As a follow-up question, can anyone elaborate on this statement from PEP8 ?
...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.
.. is this a blanket statement to avoid using += and + for concatenation in all cases? If so, why even have the feature as part of the language?