Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #77191

Re: python string, best way to concat

References <55bab2a0-e0bc-4398-90b4-c9937498f5d8@googlegroups.com> <63bdccb4-9e34-4e40-b07d-14342e21815f@googlegroups.com> <ltmq2t$sao$1@ger.gmane.org>
Date 2014-08-28 18:58 +1000
Subject Re: python string, best way to concat
From Chris Angelico <rosuav@gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.13547.1409216298.18130.python-list@python.org> (permalink)

Show all headers | View raw


On Thu, Aug 28, 2014 at 6:43 PM, Mark Lawrence <breamoreboy@yahoo.co.uk> wrote:
> On 28/08/2014 09:30, 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?
>>
>> Peter
>>
>
> Please quote context, there's some smart people on this list but none of
> them are mind readers :)

Speak for yourself! I play a mind reader on Threshold RPG (in fact,
one of the highest level psions on the game), and several of my
brothers tell me I can pull the same tricks in real life. Or maybe I'm
just really good at knowing what problems they're having, even before
they recognize the problems themselves...

But Peter, no - no reason to avoid it for concatenating two strings.
The advantages of join() are visible when you have huge numbers of
strings; plus it's more readable than repeating a separator lots of
times, especially if you're working with variable numbers of list
elements:

https://github.com/Rosuav/runningtime/blob/master/runningtime.py#L211

The debug info is collected in a list (appending each item, and quite
a few of them are elided if they're some kind of default), and then
they're joined with ', ' as the separator. I could use string
appending for that, but there's no point; it's more readable this way.
Performance doesn't matter at all here, just readability.

ChrisA

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

python string, best way to concat dennisearlevans@gmail.com - 2014-08-27 13:31 -0700
  Re: python string, best way to concat Dan Stromberg <drsalists@gmail.com> - 2014-08-27 15:30 -0700
  Re: python string, best way to concat Peter Otten <__peter__@web.de> - 2014-08-28 00:34 +0200
  Re: python string, best way to concat MRAB <python@mrabarnett.plus.com> - 2014-08-27 23:42 +0100
  Re: python string, best way to concat Tim Chase <python.list@tim.thechases.com> - 2014-08-27 17:44 -0500
  Re: python string, best way to concat Chris Angelico <rosuav@gmail.com> - 2014-08-28 08:55 +1000
  Re: python string, best way to concat Peter Otten <__peter__@web.de> - 2014-08-28 00:59 +0200
  Re: python string, best way to concat MRAB <python@mrabarnett.plus.com> - 2014-08-28 08:12 +0100
  Re: python string, best way to concat peter <peter.mosley@talk21.com> - 2014-08-28 01:30 -0700
    Re: python string, best way to concat Marko Rauhamaa <marko@pacujo.net> - 2014-08-28 11:34 +0300
    Re: python string, best way to concat Mark Lawrence <breamoreboy@yahoo.co.uk> - 2014-08-28 09:43 +0100
    Re: python string, best way to concat Chris Angelico <rosuav@gmail.com> - 2014-08-28 18:58 +1000
    Re: python string, best way to concat Roy Smith <roy@panix.com> - 2014-08-28 08:08 -0400
      Re: python string, best way to concat Mihamina Rakotomandimby <mihamina.rakotomandimby@rktmb.org> - 2014-08-28 15:19 +0300
      Re: python string, best way to concat Mark Lawrence <breamoreboy@yahoo.co.uk> - 2014-08-28 15:48 +0100

csiph-web