views:

143

answers:

2

I have this Python code:

import re
s = "aa67bc54c9"
for t, n in re.findall(r"([a-z]+)([0-9]+)", s)

And I get this error message when I try to run it:

  File "<stdin>", line 1
    for t, n in re.findall(r"([a-z]+)([0-9]+)", s)
                                                 ^
SyntaxError: invalid syntax

How can I solve this? I am new to Python.

+4  A: 

You need a colon (:) on the end of the line.

And after that line, you will need an indented statement(s) of what to actually do in the loop. If you don't want to do anything in the loop (perhaps until you get more code written) you can use the statement pass to indicate basically a no-op.

In Python, you need a colon at the end of

  • for statements
  • while statements
  • if/elif/else statements
  • try/except statements
  • class statements
  • def (function) statements
Mark Rushakoff
Thanks for being a great teacher
edwards
Don't forget Class and def statements.
Tofystedeth
+7  A: 

for starts a loop, so you need to end the line with a :, and put the loop body, indented, on the following lines.

EDIT:

For further information I suggest you go to the main documentation.

Douglas Leeder
An improvement to this answer might include "read about the for statement syntax in the Python language reference": http://docs.python.org/reference/compound_stmts.html#the-for-statement
S.Lott