better way for ' '.join(args) + '\n'?

Tycho Andersen tycho at tycho.ws
Fri Oct 26 17:26:02 EDT 2012


On Fri, Oct 26, 2012 at 09:49:50AM +0200, Ulrich Eckhardt wrote:
> Hi!
> 
> 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
> alternative.
> 
> 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)

\t



More information about the Python-list mailing list