Easy(?) newbie question

Erik Johnson ejohnso9 at earthlink.net
Sun Nov 11 17:16:38 EST 2001


Hi,

    I am just getting savvy to Python - rather excited about the
possibilities, but I have come across something that seems rather simple
but I'm stumped...

The following loop does what I would expect:

>>> for x in range(10): print x,
...
0 1 2 3 4 5 6 7 8 9

but when I try to put this code into a string and evaluate it, I get a
syntax error:

>>> s = "for x in range(10): print x,"
>>> s
'for x in range(10): print x,'
>>> eval(s)
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
  File "<string>", line 1
    for x in range(10): print x,
      ^
SyntaxError: invalid syntax
>>>

None of these forms are any better...

s = """for x in range(10):
  print x,"""

s = """
for x in range(10):
  print x,"""

s = """
for x in range(10):
  print x,
"""

s = """
for x in range(10):
  print x,

"""

    I don't get it - this syntax seems to be fine when executed directly
as statements, but if I put the same statements in a string and
eval()'uate them, it's no longer valid syntax? What am I missing here?

Thanks in advance,
-ej
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20011111/4cb48955/attachment.html>


More information about the Python-list mailing list