473,511 Members | 15,197 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

escape sequences in list comprehensions

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.
Jul 18 '05 #1
6 1666
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.

Jul 18 '05 #2
"kartik" <ka*************@yahoo.com> wrote in message
news:94**************************@posting.google.c om...
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.
Jul 18 '05 #3
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

Jul 18 '05 #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?


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
Jul 18 '05 #5
On Fri, 12 Nov 2004 22:46:17 -0500, Peter Hansen <pe***@engcorp.com> 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
Jul 18 '05 #6

bo**@oz.net (Bengt Richter) wrote:

On Fri, 12 Nov 2004 22:46:17 -0500, Peter Hansen <pe***@engcorp.com> 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

Jul 18 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

24
3323
by: Mahesh Padmanabhan | last post by:
Hi, When list comprehension was added to the language, I had a lot of trouble understanding it but now that I am familiar with it, I am not sure how I programmed in Python without it. Now I...
3
7289
by: harrelson | last post by:
I have a list of about 2500 html escape sequences (decimal) that I need to convert to utf-8. Stuff like: 비 행 기 로 보 낼 거
23
2220
by: Mike Meyer | last post by:
Ok, we've added list comprehensions to the language, and seen that they were good. We've added generator expressions to the language, and seen that they were good as well. I'm left a bit...
3
5709
by: Ken | last post by:
HI: I'm reading a string that will be displayed in a MessageBox from a resource file. The string in the resource file contains escape sequences so they will be broken up into multiple lines. ...
72
5470
by: Gregory Petrosyan | last post by:
Please visit http://www.python.org/peps/pep-0204.html first. As you can see, PEP 204 was rejected, mostly because of not-so-obvious syntax. But IMO the idea behind this pep is very nice. So,...
7
1596
by: Steven Bethard | last post by:
Tom Anderson <twic@urchin.earth.li> wrote: > Sounds good. More generally, i'd be more than happy to get rid of list > comprehensions, letting people use list(genexp) instead. That would >...
15
18280
by: pkaeowic | last post by:
I am having a problem with the "escape" character \e. This code is in my Windows form KeyPress event. The compiler gives me "unrecognized escape sequence" even though this is documented in MSDN....
5
2355
by: Anton81 | last post by:
Hi all! I used escape sequences to produce colour output, but a construct like print "%8s" % str_with_escape doesn't do the right thing. I suppose the padding counts the escape characters,...
4
4262
by: JJ | last post by:
Is there a way of checking that a line with escape sequences in it, has no strings in it (apart from the escape sequences)? i.e. a line with \n\t\t\t\t\t\t\t\r\n would have no string in it a...
0
7237
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7349
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
7074
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7506
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5659
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4734
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3210
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1572
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
445
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.