Luna Gjoka
last year
Lunacountry asked

In Python, why is a space sometimes considered an error?

Abhay Jajodia
Expert
last year
Abhay Jajodia answered

Hi Luna,

In Python, spaces at the start of a line matter because Python uses indentation to understand code blocks.

So if you add extra spaces before a line like print("Hello") when it is not inside an if, for, while, or a function, Python thinks you are starting a block and throws an indentation error.

Example of correct indentation:

if True:
    print("Hello")

If you have more questions, I am here to help 😊

Python
This question was asked as part of the Getting started with Python course.