Python Sleep Method To Wait, Sleep, Pause, Stop, Delay

By | January 3, 2018

Let us understand what is Python sleep() method and how we can implement Python wait, pause, stop and delay functionalities using the sleep() function.

What is Python sleep() function?

The sleep() function in Python suspends the execution of a Python script for a given period of time, usually in seconds. The sleep() function is defined in the Python’s time module.

With the sleep() method, it is possible to implement the delay, pause, wait and stop the Python script and all these tasks can be performed by the sleep() method.

Syntax

time.sleep(time_parameter);

It is important to import the time module in the Python script where you want to use the sleep() method.

Uses of Python.sleep() method

  • The Python script can wait for user inputs for a given period of time
  • Create intentional delays in executing certain parts of the script
  • Stop the execution of a script
  • Pause the script execution for a given period of time

Also Read: Python Range() Function

Note: This script to implement Python wait functionality using Python sleep() function is written using Python IDLE on Microsoft Windows operating system and the version is Python 3.6.4.

Method 1: Basic Python wait functionality

The time module in Python provides various functionalities for time access and conversions. It is very much similar to the time library of C programming.

We need to initially import time module using import time statement. Then, use the sleep method with the time module using a dot operator with the following statement time.sleep(10).

import time

time.sleep(10) #Python script waits for 10 seconds before execution

Method 2: Python sleep() function with print statements

#!/usr/bin/python

import time

print("This is the first line without sleep() method")
time.sleep(10) #Python script waits for 10 seconds after interpreting this line
print("This is the second line with sleep() method implemented for 10 seconds")

Method 3: Implementing time.sleep() method with start and end time

#!/usr/bin/python

import time
print("Start Time:\t%s" + time.ctime())
print("This is the first line without time.sleep() method")
time.sleep(10) #Python wait for 10 seconds after interpreting this line
print("End Time:\t%s" + time.ctime())
print("This is the second line with sleep() method implemented for 10 seconds");

Method 4: Python wait function with User Input

If you’re writing this script in Python 2.x versions, then you should use raw_input() method and for Python 3.x versions, you can use input() method. We’re converting the user input into float datatype here since the default input is string. You can also use any other numeric datatype.

 
#!/usr/bin/python

import time
print("This is the first line without time.sleep() method")
waiting_time = float(input('Enter Waiting Time:\t'))
time.sleep(waiting_time) #Python script waits for 10 seconds after interpreting this line
print("End Time:\t%s" + time.ctime())

Output

Python Sleep Method Explained To Implement Python Wait, Sleep, Delay, Pause and Stop functionalities

If you have any issues while writing a script to implement Python sleep() method, let us know about it in the comment section below.

One thought on “Python Sleep Method To Wait, Sleep, Pause, Stop, Delay

Let's Discuss