import locale and print range on same line

Terry Reedy tjreedy at udel.edu
Sat Jan 23 21:45:29 EST 2016


On 1/23/2016 8:58 AM, Chris Angelico wrote:
> On Sun, Jan 24, 2016 at 12:45 AM, Steven D'Aprano <steve at pearwood.info> wrote:
>> On Sun, 24 Jan 2016 12:19 am, Chris Angelico wrote:
>>
>>> On Sun, Jan 24, 2016 at 12:07 AM, Steven D'Aprano <steve at pearwood.info>
>>> wrote:
>>>> On Sat, 23 Jan 2016 09:02 pm, raiwil at gmail.com wrote:
>>>>
>>>>> However I need to put the code on one single line.
>>>>
>>>> Why? Is the Enter key on your keyboard broken?
>>>
>>> Maybe it's for a python -c invocation.
>>
>>
>> [steve at ando ~]$ python -c "for i in range(5):
>>>      print 'hello world'
>>> "
>> hello world
>> hello world
>> hello world
>> hello world
>> hello world
>> [steve at ando ~]$
>
> Well, not everyone's shells are as awesome as bash...

Like Windows command prompt is not.  I tried:

C:\Users\Terry>python -c "for i in range(5):\n\tprint('hello world')"
   File "<string>", line 1
     for i in range(5):\n  print('hello world')
                                              ^
SyntaxError: unexpected character after line continuation character

-c does not preprocess the code string before executing.  I may propose 
that it do so.  However, Python is still pretty awesome.

C:\Users\Terry>python -c "exec('''for i in range(5):\n  print('hello 
world')''')"
hello world
hello world
hello world
hello world
hello world

> and not everyone knows you can do that.

One can even combine -i (interactive) with -c (code).

C:\Users\Terry> python -i -c "exec('''a=[]\nfor i in 
(1,2,3):\n\ta.append(i)''')"
 >>> a
[1, 2, 3]
 >>>

-- 
Terry Jan Reedy




More information about the Python-list mailing list