The purpose of the exclamation mark (!) in Jupyter Notebook

avatar
Borislav Hadzhiev

Last updated: Apr 10, 2024
4 min

banner

# Table of Contents

  1. The purpose of the exclamation mark (!) in Jupyter Notebook
  2. Prefix the pip install command with ! to install a module
  3. Assigning the output of a shell command to a Python variable
  4. Using the magic command %cd in in Jupyter notebook

# The purpose of the exclamation mark (!) in Jupyter Notebook

The exclamation mark (!) in Jupyter Notebook is used to issue a shell command.

When a command is prefixed with !, it is run as a shell command instead of a Python script.

  1. Open your terminal and issue the jupyter-notebook command to start your server.
shell
jupyter-notebook
  1. Click on New and select Python 3 (ipykernel).

click new python ipykernel

  1. Type !ls and click on the > Run button to run the command.
shell
!ls

issue ls command in jupyter

The command will list the files and folders in the current directory.

ls is not a valid Python command but prefixing the command with an exclamation mark signals to Jupyter Notebook that the command should be run in the terminal and not using the Python interpreter.

You can run the code in the cell by pressing Ctrl + Enter or clicking on the > Run button.

# Prefix the pip install command with ! to install a module

Something you often have to do is prefix the pip install command with an exclamation mark to install a Python module in Jupyter Notebook.

shell
!pip install requests

installing python module in jupyter with exclamation mark prefix

You can press Ctrl + Enter to run the installation command.

If you need to print the path to the working directory, prefix the pwd command with an exclamation mark.

shell
!pwd

print path to working directory

Note that the Jupyter Notebook shell allows you to run UNIX/Linux commands even if you are on Windows.

The only thing that should be noted is that in order to signal to Jupyter Notebook that you want to run a shell command and not a Python script, you have to prefix the command with an exclamation mark !.

Another thing to keep in mind is that each command is executed in its own subshell.

This means that the output of the previous command is not available.

You can use the !echo command to print messages and environment variables.

shell
!echo "hello world"

use echo command to print messages and environment variables

If you need to print the version of the Python interpreter, use the !python --version command.

shell
python --version

print version of python interpreter

You can also use flags when issuing shell commands.

shell
!ls -s

Output:

shell
total 128 4 another.py 8 example.xlsx 4 test_sample.py 4 employees.csv 4 file-1.txt 16 thumbnail.webp 4 example_1677091720.txt 4 file-2.txt 4 Untitled1.ipynb 4 example.csv 4 main.py 4 Untitled2.ipynb 4 example.json 4 __pycache__ 4 Untitled3.ipynb 4 example.mdx 20 pylintrc 4 Untitled4.ipynb 4 example.pdf 4 readme.md 4 Untitled.ipynb 4 example.txt 4 requirements.txt 4 venv

The -s flag of the ls command displays the size of files and directories next to their names.

If you need to print all options and flags of a given command, use the man (manual) pages.

shell
!man ls

print man pages in jupyter notebook

# Assigning the output of a shell command to a Python variable

You can also assign the output of a shell command to a Python variable.

You just have to prefix the shell command with an exclamation mark in the assignment.

shell
current_dir = !pwd print(current_dir)

assign output of shell command to variable

Notice that the pwd command is prefixed with an exclamation mark !.

The Jupyter Notebook interpreter runs the shell command and stores its output in the current_dir Python variable.

The variable stores a list that contains the path to the current working directory.

Here is an example that uses the same approach to assign the output of the ls command to a Python variable.

shell
contents = !ls print(contents)

store output of ls command into variable

As the screenshot shows, the names of the files in the current directory as stored in the Python list.

# Using the magic command %cd in in Jupyter notebook

As previously noted, the Jupyter Notebook shell runs each command in a subshell.

So you don't have access to the output of the previous commands.

Here is an example that better demonstrates this using the cd (change directory) command.

shell
!pwd !cd .. !pwd

Output:

shell
/home/borislav/Desktop/bobbyhadz_python /home/borislav/Desktop/bobbyhadz_python

cd command has no effect

If you run the command you will see that issuing the cd command didn't work.

You can use the magic %cd command if you need to change to another directory permanently.

shell
!pwd %cd .. !pwd

using magic cd command

Notice that we prefixed the cd command with a percent % sign and not an exclamation mark.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.