Working with Strings and Quotes in Python
Python is not intelligent and cannot guess your intent. It is strict about syntax and will reject any code that violates its rules, even by a single character.
One Character Matters
Python does not guess what you intended. It accepts instructions only when they follow Python's syntax rules. A missing quote, a missing parenthesis, or a natural-language sentence in place of a Python command can cause Python to reject the input. This strictness may feel frustrating at first, but the error message gives you information you can use to correct the code.
From Text to Decision
When you write Python, the language stands between your instructions and the result you want. You provide instructions in Python syntax. Python either recognizes those instructions and executes them, or rejects them before the program can run. If Python cannot recognize the syntax, the user does not see the intended output because Python has stopped to report an error.
Opening and Closing Quotes
A string is text written inside quotes. In the print syntax described by the source, the word print is followed by parentheses, and the text appears inside those parentheses enclosed in quotes. The opening quote tells Python that the string begins. Python then expects a matching closing quote to show where the string ends. If that closing quote is absent, Python reports an unterminated string literal: it found the beginning of a string but never found the ending it expected.
Hello world!Reading a SyntaxError
A SyntaxError means that Python syntax rules have been violated. The message identifies the error as a SyntaxError, gives the file and line number where the problem occurred, shows the line of code, points approximately to where the problem was detected with a caret, and describes what went wrong.
Finding an Unclosed String
A line begins a string with a single quote but does not close it.
Locate the line: Use the line number in the error message to return to the relevant line of code.
Read the description: The description unterminated string literal means Python found an opening quote but did not find the closing quote it expected.
Inspect the pointer: The caret marks approximately where Python detected the problem. The pointer can help focus your inspection, but the missing character may be elsewhere in the same line.
Repair the string: Add the missing closing quote, then try the code again.
The line becomes syntactically complete when the string has both its opening and closing quotes.
Mistakes That Stop Execution
Leaving a string without its closing quote.
Python finds the opening quote but never finds the closing quote needed to end the string. It reports an unterminated string literal.
Fix:
Check that the string has a matching closing quote.Forgetting the parentheses after print.
The source identifies missing parentheses after print as a common syntax mistake.
Fix:
Use the print syntax with the word print followed by parentheses and quoted text inside them.Writing a natural-language sentence instead of a Python command.
Python does not understand English sentences as Python instructions. It recognizes Python syntax, not the programmer's intended meaning.
Fix:
Express the instruction using the appropriate Python command and exact syntax.
These mistakes have different visible symptoms, but the underlying lesson is the same: Python stops when the code does not follow its rules. It does not partially repair the instruction and continue. Read the message, return to the indicated line, and compare every required part of the syntax.
The Repair Loop
Learning Python syntax is an error-correction cycle rather than a one-attempt test. Write code and submit it. If Python accepts the syntax, it executes the instructions and produces output. If Python rejects the syntax, read the error message, identify the problem, correct the code, and try again. Repeating this process teaches you Python's rules through direct practice.
- Write the instruction using Python syntax.
- Run or submit the code.
- If Python reports a SyntaxError, read the error name and description.
- Use the line number to find the relevant code.
- Inspect the pointer and the surrounding syntax.
- Fix the missing or incorrect syntax.
- Try the corrected code again.
Suppose Python reports an unterminated string literal on line 1. Before changing anything, state what you would inspect first and what kind of character you would expect to be missing.
Hints
- Start with the line number in the message.
- Look for an opening quote that does not have a matching closing quote.
- Read the description as well as the caret position.
What do you think happens?
What should you expect Python to do when a line contains text that is close to a valid Python command but uses natural language instead of Python syntax?
Reveal answer
Answer: Reject the input with a SyntaxError
Python cannot guess the programmer's intent or understand an English sentence as a Python instruction. It accepts valid Python syntax and reports an error when the input does not follow the rules.
Key Takeaways
- Python follows strict syntax rules and can reject code because of one missing character.
- A quoted string needs an opening quote and a matching closing quote.
- A SyntaxError message provides an error name, line information, the code line, an approximate pointer, and a description.
- Python cannot infer what you meant from natural language or nearly correct syntax.
- The normal learning process is to write code, read errors, fix mistakes, and try again.
Key Takeaways
- Python accepts instructions only when they follow exact syntax rules.
- Opening and closing quotes mark the boundaries of a string; leaving a string unclosed causes a SyntaxError.
- Use the entire SyntaxError message to locate and understand a problem.
- Python does not guess intent, so natural-language instructions must be rewritten as Python commands.
- Use errors as part of the normal write, read, fix, and retry cycle.