In this short tutorial, we will show you how to exit the Python prompt while in the terminal.
There are a couple of different methods that you can use to exit the Python interpreter while in the terminal, which I will go into detail below. The instruction for Linux or macOS is slightly different from Windows, so choose carefully.
Python Interactive Mode
The Python interpreter has an interactive mode that allows you to issue commands to Python. Using this mode, you can write code into the terminal and have immediate feedback from Python. The interactive mode may be the preferred method over using a Python script for some use cases.
The example below demonstrates using Python to print the solutions to some simple math problems.
>>> print(2*3) 6 >>> print(21+5) 26 >>> print(6/3) 2.0
Entering interactive mode for Python is very simple as you type “
python3 into the terminal. However, exiting interactive mode is a little different depending on your operating system.
To confirm that you are in interactive mode, you should see three arrows
Below is an example of entering Python interactive mode on Ubuntu.
dev@pimylifeup:~$ python3 Python 3.8.10 (default, Jun 22 2022, 20:18:18) [GCC 9.4.0] on linux Type "help", "copyright", "credits" or "license" for more information. >>>
Exit Python Terminal on Linux or macOS
You can type in
exit() to exit out of Python while using the terminal on a Linux or macOS computer.
Ensure you include the paratheses; otherwise, Python will present you with a message with the correct ways to exit Python. Below is an example of not using paratheses.
>>> exit Use exit() or Ctrl-D (i.e. EOF) to exit
Below is the correct way to exit Python in the terminal on a macOS or Linux computer.
On Linux and macOS, you should be able to use the CTRL + D shortcut to exit the Python prompt.
Below is an example of using CTRL + D to exit.
dev@pimylifeup:~$ python3 Python 3.8.10 (default, Jun 22 2022, 20:18:18) [GCC 9.4.0] on linux Type "help", "copyright", "credits" or "license" for more information. >>> + Stopped python3
Exit Python Terminal on Windows
Like Linux and macOS, the
exit() function will exit out of the Python prompt on Windows.
Below is an example of how you can exit Python in the command line on a Windows computer.
Unlike Linux and macOS, CTRL + D may not work on Windows. Instead, you will need to use CTRL + Z, then Enter to exit the Python prompt.
Below is an example of using CTRL + Z, then Enter to exit.
C:\Users\gus>py Python 3.10.1 (tags/v3.10.1:2cd268a, Dec 6 2021, 19:10:37) [MSC v.1929 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> ^Z C:\Users\gus>
I hope you can now exit the Python prompt in the terminal without any issues. Knowing the correct method to exit the interpreter will come in handy if you use the terminal a lot in your workflow.
If you want to learn more about the Python programming language, we have plenty more tutorials you should check out. If you are starting with Python, we recommend following our getting started with Python tutorial.
Please let us know if you notice a mistake or an important topic is missing from this guide.