invalid syntax printing string from file

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • DanTheL
    New Member
    • Nov 2014
    • 3

    invalid syntax printing string from file

    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
  • dwblas
    Recognized Expert Contributor
    • May 2008
    • 626

    #2
    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”
    Code:
    """ #! /usr/bin/env python
     #encoding:latin-1
    """
    s='“foo”'
    print ord(s[0])
    print ord(s[-1])
    print ord('"')
    print ord("'")

    Comment

    • DanTheL
      New Member
      • Nov 2014
      • 3

      #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

      Comment

      • bvdet
        Recognized Expert Specialist
        • Oct 2006
        • 2851

        #4
        See if this makes any difference:

        # coding=latin-1

        OR

        # -*- coding: latin-1 -*-

        Comment

        • DanTheL
          New Member
          • Nov 2014
          • 3

          #5
          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

          Comment

          Working...