Line breaks in list causing a small formatting problem while joiningthe list

O

Oltmans

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.
 
P

Peter Otten

Oltmans said:
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.
| oltmans | abramhovic |
| sal | lee |
 
M

MRAB

Oltmans said:
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")
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top