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


Groups > comp.lang.python > #77173

Re: python string, best way to concat

References <55bab2a0-e0bc-4398-90b4-c9937498f5d8@googlegroups.com> <53FE5EDD.9020502@mrabarnett.plus.com> <20140827174446.11a1cd01@bigbox.christie.dr>
Date 2014-08-28 08:55 +1000
Subject Re: python string, best way to concat
From Chris Angelico <rosuav@gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.13533.1409180134.18130.python-list@python.org> (permalink)

Show all headers | View raw


On Thu, Aug 28, 2014 at 8:44 AM, Tim Chase
<python.list@tim.thechases.com> wrote:
> On 2014-08-27 23:42, MRAB wrote:
>> How many parameters are there? len(self.param)
>>
>> Make that many placeholders and then join them together with commas:
>>
>> ', '.join(['?'] * len(self.param))
>
> I prefer the clarity of Peter Otten's suggestion of
>
>   ', '.join('?' * len(self.param))
>
> over the mild obscurity of putting it in a list before multiplying.

Actually, I'd go the other way. This code has a possibly-surprising
limitation: it works only if the replicated string is one character
long, and in Python 3, that really does mean character, not byte. Of
course, Python experts know that it works on string iterability, and
know exactly why b'?' wouldn't work there, but putting it in a list
solves both problems.

So, I'd say the ['?'] code is less fragile.

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