time.sleep() in Python

To make a Python program delay its execution for a given time, you can use the the time.sleep() method.

The time module has a number of methods to deal with time and sleep() is one of them.

The sleep() method takes an argument in seconds.

In this example, I’m importing tin time module and then printing the current time with the ctime() method.

After that, I call sleep(10), meaning the program is going to suspend for 10 seconds.

import time

print(time.ctime())

time.sleep(10)

print(time.ctime())
Wed Apr 22 17:11:05 2020
Wed Apr 22 17:11:15 2020

Notice the 10 seconds jump between the outputs.

Milliseconds

You can also use a floating point number to be more precise.

To set a delay of 50 milliseconds, just make:

time.sleep(0.05)