escape sequences in list comprehensions

K

Kent Johnson

kartik said:
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
 
P

Peter Hansen

kartik said:
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
 
B

Bengt Richter

kartik said:
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 ;-):
... 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
] [1
, 2
, 3
]

Regards,
Bengt Richter
 
J

Josiah Carlson

kartik said:
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 ;-):
... 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
][1
, 2
, 3
]


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

- Josiah
 

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,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top