which is better, string concatentation or substitution?

Peter Otten __peter__ at web.de
Mon May 8 10:44:52 EDT 2006


John Salerno wrote:

> Duncan Booth wrote:
> 
>> If you build a
>> list of lines to join then you don't have to repeat '\n' on the end of
>> each component line.
> 
> How would that work? Wouldn't the last line in the list still need the
> newlines?

>>> chunks = ["alpha", "beta", "gamma"]
>>> "\n".join(chunks)
'alpha\nbeta\ngamma'

You mean a '\n' after 'gamma'?

>>> chunks.append("")
>>> "\n".join(chunks)
'alpha\nbeta\ngamma\n'

Peter



More information about the Python-list mailing list