How to end a Python Program Explained

Posted in Python on January 17, 2023 by Jessica Rose ‐ 1 min read

How to end a Python Program Explained Ad Sponsored

ⓘ Sponsored by

To end a Python program, press one of the following keys depending on the type of operating system :

For Windows :


For Mac Os

Control + C

How to End the Interactive Python Command-Line Session

To leave the interactive Python command-line session, press: For Windows :


For Mac OS :

Control + Z

Or run the exit() function in your session.

>>> exit()


How to Terminate a Python Script

To terminate a Python program from within the code, utilize the sys.exit() function.

  1. Always import the sys library
  2. Use sys.exit()

For example:

import sys
sys.exit() Ad Sponsored

ⓘ Sponsored by