Line breaks in list causing a small formatting problem while joining the list

Peter Otten __peter__ at web.de
Fri Jan 21 11:25:35 EST 2011


Oltmans wrote:

> Hi Python gurus, hope you're doing well. I've a small problem.
> 
> When I run the following code
> ___________________________________________________
>>>> names = ['oltmans','abramhovic','\n','sal','lee']
>>>> print '| ' + ' | '.join(names)
> | oltmans | abramhovic |
>  | sal | lee
> ___________________________________________________
> 
> I get the output like above. However, I want it to output like below
> 
> | oltmans | abramhovic |
> | sal | lee
> 
> 
> That is, there shouldn't be a space in the beginning of second line.
> The list can of course contain more than 5 elements. Any ideas? I will
> appreciate any hint. Thanks in advance.

>>> print "|%s|" % "|".join(n if n == "\n" else " %s " % n for n in names)
| oltmans | abramhovic |
| sal | lee |




More information about the Python-list mailing list