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

Discussion in 'Python' started by Oltmans, Jan 21, 2011.

  1. Oltmans

    Oltmans Guest

    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, Jan 21, 2011
    #1
    1. Advertising

  2. Oltmans

    Peter Otten Guest

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

    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 |
    Peter Otten, Jan 21, 2011
    #2
    1. Advertising

  3. Oltmans

    MRAB Guest

    Re: Line breaks in list causing a small formatting problem whilejoining the list

    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")
    MRAB, Jan 21, 2011
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. PJ
    Replies:
    1
    Views:
    516
    Sasha Shevelev
    Aug 4, 2003
  2. ATR2000

    Problem with line breaks

    ATR2000, Sep 19, 2008, in forum: ASP .Net
    Replies:
    1
    Views:
    322
    Alexey Smirnov
    Sep 20, 2008
  3. bernadou

    Force multi line field value to output with line breaks?

    bernadou, Jan 22, 2006, in forum: ASP .Net Web Controls
    Replies:
    2
    Views:
    355
    bernadou
    Jan 23, 2006
  4. Joe90

    Client side script causing formatting problems

    Joe90, Jul 12, 2006, in forum: ASP .Net Web Controls
    Replies:
    0
    Views:
    120
    Joe90
    Jul 12, 2006
  5. Marek Stepanek
    Replies:
    12
    Views:
    410
    Peter J. Holzer
    Sep 2, 2006
Loading...

Share This Page