Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

No output for `python.exe -c 'print("hello")'`

I am trying to learn python and stumbled on what seems to be another stupid error on my part.

With version 2.7.3 that I downloaded from python.org I do not get any output for a simple program with -c. I do get output with the 2.6.8 release from cygwin.

What am I missing?

> c:\Python27\python.exe --version
Python 2.7.3

> c:\Python27\python.exe -c 'print("hello")'

> c:\Python27\python.exe
Python 2.7.3 (default, Apr 10 2012, 23:24:47) [MSC v.1500 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> print("hello")
hello
>>> exit()

> c:\cygwin\bin\python2.6.exe --version
Python 2.6.8

> c:\cygwin\bin\python2.6.exe -c 'print("hello")'
hello

> c:\cygwin\bin\python2.6.exe
Python 2.6.8 (unknown, Jun  9 2012, 11:30:32)
[GCC 4.5.3] on cygwin
Type "help", "copyright", "credits" or "license" for more information.
>>> exit()
like image 793
Miserable Variable Avatar asked Feb 13 '13 20:02

Miserable Variable


1 Answers

Try it without the single quotes around the program:

python -c print(\"hello\")

With the single quotes, I guess it interprets the input as a string so doesn't do the print. You also need to escape the double quotes in the program itself.

Edit:

You don't need to escape single quotes, so you can do this instead:

python -c print('hello')

or

python -c "print('hello')"

(which is the original example, just with the quote types swapped)

like image 111
Matthew Strawbridge Avatar answered Sep 22 '22 13:09

Matthew Strawbridge