python string, best way to concat

Chris Angelico rosuav at gmail.com
Wed Aug 27 18:55:31 EDT 2014


On Thu, Aug 28, 2014 at 8:44 AM, Tim Chase
<python.list at 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



More information about the Python-list mailing list