By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,952 Members | 1,725 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,952 IT Pros & Developers. It's quick & easy.

invalid syntax printing string from file

P: 3
running 2.7.5
on Mac
from the command prompt --
>>> Print "foo"
foo

works fine.

from script file
--
#! /usr/bin/env python
#encoding:latin-1
print “foo”
--

get following error:
dans-air:~ dan$ python ./Documents/Python/Samples/foo.py
File "./Documents/Python/Samples/foo.py", line 3
print ?foo?
^
SyntaxError: invalid syntax


since this is 2.x Print "foo" should work but tried Print ("foo") get same error on quote mark.


file is saved as latin-1 encoding added encoding lines #.

can print integer from this file, print 6 results in 6 when file runs.


thanks,
Banging my head against the wall
Nov 9 '14 #1
Share this Question
Share on Google+
4 Replies


Expert 100+
P: 613
Python only recognizes single or double quote marks, ord 34 or 39, for strings. Note the difference in the quote marks between
>>> P(p)rint "foo"
and
print “foo”
Expand|Select|Wrap|Line Numbers
  1. """ #! /usr/bin/env python
  2.  #encoding:latin-1
  3. """
  4. s='“foo”'
  5. print ord(s[0])
  6. print ord(s[-1])
  7. print ord('"')
  8. print ord("'") 
Nov 9 '14 #2

P: 3
used both single quote and double quote (not both at same time) both result in same error:

case 1:
#! /usr/bin/env python
#encoding:latin-1
print ‘foo’

dans-air:~ dan$ python ./Documents/Python/Samples/foo.py
File "./Documents/Python/Samples/foo.py", line 3
print ?foo?
^
SyntaxError: invalid syntax


case 2:
#! /usr/bin/env python
#encoding:latin-1
print “foo”

dans-air:~ dan$ python ./Documents/Python/Samples/foo.py
File "./Documents/Python/Samples/foo.py", line 3
print ?foo?
^
SyntaxError: invalid syntax

the quote kicks out the ? syntax error. as if the character is not recognized. I am encoding so not sure what is missing.

thanks
Nov 10 '14 #3

bvdet
Expert Mod 2.5K+
P: 2,851
See if this makes any difference:

# coding=latin-1

OR

# -*- coding: latin-1 -*-
Nov 10 '14 #4

P: 3
bcdet,
tried both still kicks out same error:

File "./Documents/Python/Samples/foo.py", line 3
print ?foo?
^
SyntaxError: invalid syntax


not recognizing the "

thanks
Nov 11 '14 #5

Post your reply

Sign in to post your reply or Sign up for a free account.