8) Text and Strings Lesson

Python Errors and Messages

11 min to complete · By Martin Breuss
Illustration of a lighthouse

PageError: You've encountered a page you didn't expect.

Don't worry, everything is fine. In fact, this page is here to convince you that error messages are your friends when learning to program :)

Error messages seem intimidating to many people when they are getting started with programming. You might only have seen error messages when one of your devices breaks. Or maybe you still remember the blue screen of death that used to be part of too many computer sessions a couple of years ago:

Frown face from blue screen of death - Photo by https://unsplash.com/@mrthetrain Joshua Hoehne

Here to Help

In programming, error messages are here to help you out. Python is sending you a message to tell you that there was a miscommunication, and it couldn't quite understand what you were asking it to do. It gives back all that it knows and hopes---with all its digital fingers crossed---that the two of you will be able to figure it out together.

In order for Python to get back on the same page without frustrations, it helps if you train yourself to read error messages---and feel positive about encountering them!

Get to Know Python Errors

Familiarity with reading error messages is one of the most important skills when working in software development. Training this skill is crucial because you will encounter error messages all the time. Let me repeat that and put it here for you as a quote:

You will encounter error messages all. the. time.

To train your error-reading skills, keep exploring when you work on an exercise, the labs, and the projects in this course. Play around with them, break the code, and encounter as many error messages as possible. Then just keep breathing and read over the message that Python sent.

Learn More About Python

Purposefully running into error messages gives you another angle to explore the inner workings of Python. You can figure out how something works by first exploring when it doesn't work. Once you see that something doesn't function as expected, you figure out what needs to be done in order to make it work.

While working on this course, you'll run into lots and lots of error messages. Keep in mind that they are just Python's way of chatting with you about a potential misunderstanding, with the honest-to-good intention of figuring it out together. Like in any misunderstanding, as long as both parties are cooperative, this gives you a great opportunity to better understand how the other party works and thinks.

How to Read a Python Error Message

At this point, you already did some programming and you had the chance to encounter some error messages on the way. Now you'll run into some of them on purpose so you can train your error message reading skills.

Try the following code snippet - an error is expected.

s = "hello'

As you can see, you have managed to confuse Python. But Python is motivated to figure out what went wrong in your conversation and gives you all the info that it has at this point:

>>> s  = "hello'
  File "<input>", line 1
    s  = "hello'
               ^
SyntaxError: EOL while scanning string literal

In this error message, Python tells you the type of error it encountered. In this case, that's a SyntaxError. This gives you a chance to narrow down what you should look for in the code you wrote. Looks like there's some syntactical issue. Great! These errors are often the easiest to spot.

Error Message Read Order

But Python gives you even more information than that. Generally, it's a good approach to read an error message from the bottom up:

  • SyntaxError: Something is wrong with the syntax of your code. Your code doesn't comply with the rules of the Python language.
  • EOL while scanning string literal: Python expected the statement to continue, but it was suddenly over.
  • ^: This small arrow points you to the location where Python got lost. Sometimes this will point exactly to the issue as it does here, but sometimes you might have to look a little earlier in the code. Python can only tell you where it got lost and sometimes the reason why it did happen a bit before Python noticed.
  • line 1: Similar to the arrow, this line number tells you where to look for the error in your code. Python again tells you where it got lost, so the line number it mentions is a good way to start looking.

In this case, the SyntaxError was caused by mixing two different kinds of quotation marks when you created the string. That's not a valid syntactical way of creating a string in Python, which is why Python got confused.

If you're working on Python 3.10 or higher, then you'll see an even more helpful error message when you mix two different types of quotes in a string:

Python 3.11.0 (main, Oct 27 2022, 16:43:43) [Clang 12.0.5 (clang-1205.0.22.9)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> "hello'
  File "<stdin>", line 1
    "hello'
    ^
SyntaxError: unterminated string literal (detected at line 1)

Python's been working on its communication skills! It's really invested in making your friendship work out, so there have been some improvements in error messages that make them a little more clear. If you're curious, you can read more about these updates in the official "what's new?" pages for Python 3.10, Python 3.11 and Python 3.12.

Create Python Errors

Purposefully run into some more errors and deconstruct them as shown above:

  • Type 3 + "2" into your REPL. What error message do you receive?
  • Now turn it around and type "2" + 3 into your REPL. What's the difference in the error message you receive? Can you figure out why?

Carefully read over the error messages that you receive. This gives you a chance to better understand what might have caused the misunderstanding. Once you know what went wrong, you're well on your way to fixing it!

Summary: Python Errors and Messages

  • Python errors are your friends
  • Errors help you understand Python and problems in your application
  • Error messages are read in order from bottom to top

How to Learn with Python Errors

  • See error messages without worrying
  • Read error messages and gathering useful tips from them
  • Resolve error messages by understanding what they mean and where to look to fix the underlying issue
  • Explore the programming language that you are learning about
Edited happy face on blue screen of death - Photo by https://unsplash.com/@mrthetrain Joshua Hoehne (edited)
Illustration of a lighthouse

EndOfPageError: End of current page reached. Please click on the next page to access more content ;)