To end a Python program, press one of the following key combos depending on your system:
CTRL + Con Windows.
Control + Con Mac.
This stops your program from running.
How to End the Interactive Python Command-Line Session
To leave the interactive Python command-line session, press:
CTRL + Zon Windows.
Control + Zon Mac.
Or run the
exit() function in your session:
How to Terminate a Python Script
If you need to end a Python program from the code, use the
Before you do that make sure to import the
sys library. This is a built-in library that comes with useful functionality, such as the
exit() function with which you can terminate a Python program.
Here’s how you can use it:
import sys sys.exit()
Try running this program and you will see that it terminates the program immediately.
Thanks for reading!