To end a Python program, press one of the following key combos depending on your system:
-
CTRL + C
on Windows. -
Control + C
on 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 + Z
on Windows. -
Control + Z
on Mac.
Or run the exit()
function in your session:
>>> exit()
How to Terminate a Python Script
If you need to end a Python program from the code, use the sys.exit()
function.
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!
Further Reading
50 Python Interview Questions with Answers
50+ Buzzwords of Web Development
About the Author
- I'm an entrepreneur and a blogger from Finland. My goal is to make coding and tech easier for you with comprehensive guides and reviews.
Recent Posts
Artificial Intelligence2023.05.16Humata AI Review (2023): Best PDF Analyzer (to Understand Files)
Python2023.05.139 Tips to Use ChatGPT to Write Code (in 2023)
Artificial Intelligence2023.04.1114 Best AI Website Builders of 2023 (Free & Paid Solutions)
JavaScript2023.02.16How to Pass a Variable from HTML Page to Another (w/ JavaScript)