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

MRAB python at mrabarnett.plus.com
Fri Jan 21 13:54:23 EST 2011


On 21/01/2011 16:25, Peter Otten wrote:
> 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 |
>
Or:

     print ('| ' + ' | '.join(names)).replace("\n ", "\n")



More information about the Python-list mailing list