Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!nntp.club.cc.cmu.edu!micro-heart-of-gold.mit.edu!bloom-beacon.mit.edu!bloom-beacon.mit.edu!panix!roy From: Roy Smith Newsgroups: comp.lang.python Subject: Re: python string, best way to concat Date: Thu, 28 Aug 2014 08:08:22 -0400 Organization: PANIX Public Access Internet and UNIX, NYC Lines: 28 Message-ID: References: <55bab2a0-e0bc-4398-90b4-c9937498f5d8@googlegroups.com> <63bdccb4-9e34-4e40-b07d-14342e21815f@googlegroups.com> NNTP-Posting-Host: localhost X-Trace: reader1.panix.com 1409227703 24582 127.0.0.1 (28 Aug 2014 12:08:23 GMT) X-Complaints-To: abuse@panix.com NNTP-Posting-Date: Thu, 28 Aug 2014 12:08:23 +0000 (UTC) User-Agent: MT-NewsWatcher/3.5.3b3 (Intel Mac OS X) Xref: csiph.com comp.lang.python:77201 In article <63bdccb4-9e34-4e40-b07d-14342e21815f@googlegroups.com>, peter wrote: > I used to struggle with the concept of ''.join(('hello ','world')) - it > seemed so convoluted compared with the intuitive 'hello '+'world', and I > could never remember the syntax. Also, for the strings I was generally using > the performance penalty was infinitesimal, so I was just adding complexity > for the sake of the abstract concept of a more 'pythonic' style. > > Obviously this isn't going to change, but for concatenating short strings a > and b is there any practical reason to avoid a+b? For places where performance doesn't matter, string addition is just fine. The computer works for you. If you're working for the computer, you're doing something wrong. That being said, join is typically used where you have a variable number of strings in some iterable (e.g. a list of strings). For exactly two strings, I would have probably written this as: '%s %s' % (string1, string2) and if I really wanted to use the join syntax, I would have moved the delimiter (in this case, a space), into the first string: ' '.join([string1, string2]) Be aware of the various ways, then pick the one that works for you.