Does Python support semicolons?

Are semicolons just for dragons?

Many languages follow the syntax of C, and use semicolons to indicate the end of a statement. For example, in JavaScript:

console.log("Hello World");

JavaScript uses automatic semicolon insertion, so semicolons often aren’t truly necessary:

console.log("Hello World")

So what about Python?

The short answer is that Python supports semicolons, but there is no need to use them, and they are generally considered bad style.

Python accepts semicolons to end statements, such as:

x = 1;
y = 2;

But this is unnecessary, as a new line, outside of brackets, is sufficient to end a statement.

Python only added semicolons to support more than one statement per line, such as:

x = 1; y = 2

Using this capability is considered bad style though, as it reduces both readability and debuggability. The popular code checker Flake8 complains about any use of semicolons:

$ flake8 example.py
example.py:1:6: E702 multiple statements on one line (semicolon)

Additionally, the code formatter Black will reformat code to one statement per line, removing all semicolons:

x = 1
y = 2

Therefore we should avoid using semicolons in production code. But there are occasional uses for them in other places.

Command Line Python

Sometimes it’s useful to run Python in a command line pipe. Here the semicolon comes in useful to write a command in a single line.

Por exemplo, imagine we wanted to call Python’s re.escape() on output from another program. We can do so in a single line like so:

$ echo "21 ^ 2" | python -c 'import re; print(re.escape(input()))'
21\ \^\ 2

Neat.

Thanks to shell string rules, multiple lines are possible without semicolons, but it can look odd:

$ echo "21 ^ 2" | python -c 'import re
print(re.escape(input()))'
21\ \^\ 2

Temporary Snippets

I’ve used semicolons in the past for invoking Python’s debugger, pdb. It’s convenient to import and start pdb in one line, which I inserted with a text editor snippet whenever required:

import pdb; pdb.set_trace()

Since I didn’t want debugging code to get committed, it was also useful that Flake8 flagged warnings on such lines.

Invoking pdb in this manner is slightly outdated though. Since Python 3.7, it’s possible to start pdb with the breakpoint() builtin, no import required:

breakpoint()

Thus I no longer need this snippet. It might still be useful to use semicolons in other snippets though.

IPython

Update (2021-09-27): Thanks to Mike Lee Williams for sending me this tip.

The improved Python shell IPython gives special meaning to semicolons at the end of lines. When we run an expression in IPython, it outputs the result:

In [1]: 2*2
Out[1]: 4

If there are multiple expressions (from semicolons, newlines, etc.), IPython will only output the final result:

In [2]: 2*2; 3*3
Out[2]: 9

This is normally useful, but occasionally we don’t want to see the return value. We can do this by ending the line with a semicolon:

In [3]: 2*2;

This is particularly useful in Jupyter Notebooks when output would distract the reader. For example, calling matplotlib’s title() returns a Title() object, but we don’t care about seeing it in Out [...] when the title is visible on the plot. We can suppress the output from the cell by ending it with a semicolon:

In [1]: import matplotlib.pyplot as plt

In [2]: plt.plot([1, 2, 3])
        plt.title("My Plot");

<plot would be visible here>

Great!

Fin

We’re at the end; I hope you enjoyed this post,

—Adam


Learn how to make your tests run quickly in my book Speed Up Your Django Tests.


Subscribe via RSS, Twitter, Mastodon, or email:

One summary email a week, no spam, I pinky promise.

Related posts:

Tags: