In general, Python does not use semicolons as Python is a “whitespace delimited” language.

While other programming languages require the use of semicolons to end code statements, you do not need to put a semicolon at the end of your lines of code in Python.

string_variable = "no semicolon at the end of this statement"

When learning Python for the first time, it’s possible you’ve already learned another language that required semicolons after each line of code.

In Python, you don’t need to end your lines of code with semicolons.

Python is a “whitespace delimited” language and therefore spaces and tabs are what separates blocks of code. You can read more in pep 8.

string_variable = "no semicolon at the end of this statement"

if len(string_variable) > 0:
    print("look at the whitespace")

#Output:
look at the whitespace

There are some cases where you can use a semicolon, but in general, semicolons are not used in Python code.

The Use of Semicolons in Python

There are cases where you can use semicolons in Python.

One example is if you want to create an inline if statement with multiple operations.

x = 1

if x > 0: print(x); print("hello"); print(x + x);

#Output:
1
hello
2

Another example is if you want to initialize multiple variables with variables in a single line.

x = 1; y = 2; z = 3;

In general, you shouldn’t use these as they are non-pythonic. However, if you want to use these, you won’t get an error in your code.

Hopefully this article has been useful for you to learn that Python doesn’t use semicolons.

Categorized in:

Python,

Last Update: March 12, 2024