Using for in one-liner
Paul Watson
pwatson at redlinepy.com
Mon Aug 15 13:06:37 EDT 2005
BranoZ wrote:
> Paul Watson wrote:
>
>>Can a for loop be used in a one-liner? What am I missing?
>>
>>$ python -c "import sys;for i in range(5): print i,"
>> File "<string>", line 1
>> import sys;for i in range(5): print i,
>> ^
>>SyntaxError: invalid syntax
>
>
> This was tricky..
>
> python -c $'import sys;\nfor i in range(5): print i,'
>
> Separate statements with <newline> and enclose it in $'string'.
>
> BranoZ
I did try '\n' a few ways. But, I never thought to add a '$' before the
string? What made you think of that? I cannot find any shell
documentation about it either. Can you provide a reference? Thanks.
More information about the Python-list
mailing list