Search code examples
pythonsyntax-errorquotesquotequotation-marks

Syntax error on opening quotation mark


I'm trying to run a script for exercise 3.3 in the book Think Python:

Problem: Python provides a built-in function called len that returns the length of a string, so the value of len('allen') is 5. Write a function named right_justify that takes a string named s as a parameter and prints the string with enough leading spaces so that the last letter of the string is in column 70 of the display.

I've worked a few kinks out of script so far and right now I have this:

#!/usr/bin/env python
# -*- coding: utf-8 -*-

def right_justify(s):
    print ‘ ‘ * (70 - len(s)) + s

right_justify(‘allen’)

and when I try to run it I get the following error:

 File "/Users/Jon/Documents/Python/Chapter 3/right justify.py", line 5
    print ‘ ‘ * (70 - len(s)) + s
          ^
SyntaxError: invalid syntax

What mistake did I make and what should I do to fix this script?


Solution

  • The character is unrecognized by the parser. You need to use either apostrophes or quotation marks (' or ") for string literals:

    print ' ' * (70 - len(s)) + s
    

    For more information, see Strings literals in the documentation.