Python .join or string concatenation
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.
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.