-c input

Steven Bethard steven.bethard at gmail.com
Tue Aug 31 22:02:24 CEST 2004


Mike Maxwell <maxwell <at> ldc.upenn.edu> writes:
> python -c "print 'hi'; for i in [1,2]: print i"
> 
> gives me an invalid syntax error, with the carat pointing to the end
> of the word 'for'.

Semicolons are only allowed with simple statements, not compound statements:

http://docs.python.org/ref/compound.html

Note that:

stmt_list  ::=  simple_stmt (";" simple_stmt)* [";"]

So that you can only join simple statements with ";" characters.

If you want to insert newlines, in unix, you can do this like:

> python -c "print 'hi'\
? for i in [1,2]:\
?     print i"
hi
1
2

Note that the '\' characters cause unix to include the newline in the string, 
instead of reading it as the command terminator.  I don't know how to do this 
in Windows.

Steve




More information about the Python-list mailing list