escape sequences in list comprehensions

Discussion in 'Python' started by kartik, Nov 12, 2004.

  1. kartik

    kartik Guest

    Escape sequences don't seem to work in strings within list comprehensions:
    >>> print ['%s\n' %i for i in [1,2,3]]

    ['1\n', '2\n', '3\n']

    What am I missing?

    Thank you.
     
    kartik, Nov 12, 2004
    #1
    1. Advertising

  2. kartik

    Kent Johnson Guest

    kartik wrote:
    > Escape sequences don't seem to work in strings within list comprehensions:
    >
    >>>>print ['%s\n' %i for i in [1,2,3]]

    >
    > ['1\n', '2\n', '3\n']
    >
    > What am I missing?


    This is correct; what were you expecting?

    When you print a list, it prints the repr() of the list elements. In the
    case of strings, it shows you the escaped form of the string. Maybe this
    is what you want:
    >>> a=['%s\n' %i for i in [1,2,3]]
    >>> a

    ['1\n', '2\n', '3\n']
    >>> for x in a:

    .... print x
    ....
    1

    2

    3

    >>>


    Note the extra lines when the strings are actually printed.

    Kent

    >
    > Thank you.
     
    Kent Johnson, Nov 12, 2004
    #2
    1. Advertising

  3. kartik

    Russell Blau Guest

    "kartik" <> wrote in message
    news:...
    > Escape sequences don't seem to work in strings within list comprehensions:
    >>>> print ['%s\n' %i for i in [1,2,3]]

    > ['1\n', '2\n', '3\n']
    >
    > What am I missing?


    You're missing what happens when you print *any* list containing strings:

    >>> print ['\n', '\n']

    ['\n', '\n']

    --
    I don't actually check my hotmail account, but you can replace hotmail with
    excite if you really want to contact me.
     
    Russell Blau, Nov 12, 2004
    #3
  4. kartik wrote:
    > Escape sequences don't seem to work in strings within list comprehensions:
    >
    >>>>print ['%s\n' %i for i in [1,2,3]]

    >
    > ['1\n', '2\n', '3\n']
    >
    > What am I missing?


    Other have answered to this question.

    Just try this instead :
    print "\n".join(["%d" % i for i in [1,2,3]])

    > Thank you.

    HTH
     
    Bruno Desthuilliers, Nov 12, 2004
    #4
  5. kartik

    Peter Hansen Guest

    kartik wrote:
    > Escape sequences don't seem to work in strings within list comprehensions:
    >
    >>>>print ['%s\n' %i for i in [1,2,3]]

    >
    > ['1\n', '2\n', '3\n']
    >
    > What am I missing?


    Others have answered, but not explained. When you print
    a list (in other words, when you display the results of
    calling str() on a list), the list chooses how to display
    itself. It chooses to display the brackets at the start
    and end, the commas separating the items, and the results
    of calling repr() on each individual item. That means
    that strings are shown with quotation marks (which are not
    part of the string normally) and with special characters
    represented as escape sequences.

    The following appears to be what you believed you wanted,
    though it's not really what you wanted <wink>:

    lst = ['%s\n' % i for i in [1,2,3]]
    print '[' + ', '.join([str(x) for x in lst]) + ']'

    -Peter
     
    Peter Hansen, Nov 13, 2004
    #5
  6. On Fri, 12 Nov 2004 22:46:17 -0500, Peter Hansen <> wrote:

    >kartik wrote:
    >> Escape sequences don't seem to work in strings within list comprehensions:
    >>
    >>>>>print ['%s\n' %i for i in [1,2,3]]

    >>
    >> ['1\n', '2\n', '3\n']
    >>
    >> What am I missing?

    >
    >Others have answered, but not explained. When you print
    >a list (in other words, when you display the results of
    >calling str() on a list), the list chooses how to display
    >itself. It chooses to display the brackets at the start
    >and end, the commas separating the items, and the results
    >of calling repr() on each individual item. That means
    >that strings are shown with quotation marks (which are not
    >part of the string normally) and with special characters
    >represented as escape sequences.
    >
    >The following appears to be what you believed you wanted,
    >though it's not really what you wanted <wink>:
    >
    >lst = ['%s\n' % i for i in [1,2,3]]
    >print '[' + ', '.join([str(x) for x in lst]) + ']'
    >

    Maybe this is what he wanted (not recommended for general use ;-):

    >>> class FunnyStr(str):

    ... def __repr__(self): return str(self)
    ...

    Plain:
    >>> lst = ['%s\n' % i for i in [1,2,3]]
    >>> lst

    ['1\n', '2\n', '3\n']

    Funny:
    >>> lst = [FunnyStr('%s\n' % i) for i in [1,2,3]]
    >>> lst

    [1
    , 2
    , 3
    ]
    >>> print lst

    [1
    , 2
    , 3
    ]

    Regards,
    Bengt Richter
     
    Bengt Richter, Nov 13, 2004
    #6
  7. (Bengt Richter) wrote:
    >
    > On Fri, 12 Nov 2004 22:46:17 -0500, Peter Hansen <> wrote:
    >
    > >kartik wrote:
    > >> Escape sequences don't seem to work in strings within list comprehensions:
    > >>
    > >>>>>print ['%s\n' %i for i in [1,2,3]]
    > >>
    > >> ['1\n', '2\n', '3\n']
    > >>
    > >> What am I missing?

    > >
    > >Others have answered, but not explained. When you print
    > >a list (in other words, when you display the results of
    > >calling str() on a list), the list chooses how to display
    > >itself. It chooses to display the brackets at the start
    > >and end, the commas separating the items, and the results
    > >of calling repr() on each individual item. That means
    > >that strings are shown with quotation marks (which are not
    > >part of the string normally) and with special characters
    > >represented as escape sequences.
    > >
    > >The following appears to be what you believed you wanted,
    > >though it's not really what you wanted <wink>:
    > >
    > >lst = ['%s\n' % i for i in [1,2,3]]
    > >print '[' + ', '.join([str(x) for x in lst]) + ']'
    > >

    > Maybe this is what he wanted (not recommended for general use ;-):
    >
    > >>> class FunnyStr(str):

    > ... def __repr__(self): return str(self)
    > ...
    >
    > Plain:
    > >>> lst = ['%s\n' % i for i in [1,2,3]]
    > >>> lst

    > ['1\n', '2\n', '3\n']
    >
    > Funny:
    > >>> lst = [FunnyStr('%s\n' % i) for i in [1,2,3]]
    > >>> lst

    > [1
    > , 2
    > , 3
    > ]
    > >>> print lst

    > [1
    > , 2
    > , 3
    > ]



    If that's really what he wants, he should take a look at the pprint
    module.

    - Josiah
     
    Josiah Carlson, Nov 13, 2004
    #7
    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. Olaf Petzold

    vhdl textio and escape sequences

    Olaf Petzold, Nov 26, 2005, in forum: VHDL
    Replies:
    1
    Views:
    3,444
    Mike Treseler
    Nov 28, 2005
  2. Harald Kirsch
    Replies:
    0
    Views:
    329
    Harald Kirsch
    Nov 19, 2004
  3. Kit

    Escape sequences and printing

    Kit, Sep 16, 2003, in forum: C Programming
    Replies:
    5
    Views:
    502
    Matt Gregory
    Sep 17, 2003
  4. Steven Bethard
    Replies:
    7
    Views:
    398
    Rocco Moretti
    Jan 20, 2006
  5. slomo
    Replies:
    5
    Views:
    1,546
    Duncan Booth
    Dec 2, 2007
Loading...

Share This Page