better way for ' '.join(args) + '\n'?
tycho at tycho.ws
Fri Oct 26 23:26:02 CEST 2012
On Fri, Oct 26, 2012 at 09:49:50AM +0200, Ulrich Eckhardt wrote:
> General advise when assembling strings is to not concatenate them
> repeatedly but instead use string's join() function, because it
> avoids repeated reallocations and is at least as expressive as any
> What I have now is a case where I'm assembling lines of text for
> driving a program with a commandline interface. In this scenario,
> I'm currently doing this:
> args = ['foo', 'bar', 'baz']
> line = ' '.join(args) + '\n'
Assuming it's the length of the list that's the problem, not the
length of the strings in the list...
args = ['foo', 'bar', 'baz']
args[-1] = args[-1] + '\n'
line = ' '.join(args)
More information about the Python-list