What’s the Meaning of the Exclamation Mark in a Jupyter Notebook?

5/5 - (1 vote)

What many coders using Jupyter notebooks do not know is that Jupyter notebooks provide you the exclamation mark operator that allows you to execute commands on the underlying operating system.

In Jupyter notebooks, the exclamation mark ! executes commands from the underlying operating system. For example, to run the list directory command ls in your Jupyter notebook, call !ls in any cell.

Here are three examples of using three operating system commands in your Jupyter notebook. You can try them yourself in your browser by clicking on the image:

You can even install dependencies from inside your Python script using the exclamation mark/point/bang ! at the beginning of your command:

!pip install numpy

In my Jupyter notebook, this requirement is already satisfied:

Requirement already satisfied: numpy in /usr/local/lib/python3.7/dist-packages (1.19.5)

Example Exclamation Mark Operator to Check Python Version in Jupyter Notebook

To check the Python version, run

  • !python -V or
  • !python ‐‐version

in your Jupyter notebook cell.

This is the operating system command you’d use to check your Python version in your terminal or command line—prefixed with an exclamation mark. This only works in Jupyter notebooks but not in normal Python scripts.

Here’s how this looks like in our interactive Jupyter notebook:

And here’s for copy&paste:

!python -V

💡 Recommended Tutorial: How to Check Python Version in Colab?