DEV Community

Tommi k Vincent
Tommi k Vincent

Posted on

The time.sleep() Function in python.

If you need to pause your program for a while, call the time.sleep() function and pass it the number of seconds you want your program to stay paused. Launch interactive shell and Enter the following:

import time
for i in range(3):
    print('chelsea lost')
    time.sleep(1)
    print('Manchester lost')
    time.sleep(1)

time.sleep(10)
Enter fullscreen mode Exit fullscreen mode

The for loop will print chelsea lost, pause for one second , print Manchester lost ,pause for one second, print chelsea lost, pause, and so on until chelsea and Manchester have each been printed three times.

The time.sleep() function will block—that is, it will not return and release your program to execute other code—until after the number of seconds you passed to time.sleep() has elapsed. For example, if you enter time.sleep(5) ,you’ll see that the next prompt (>>>) doesn’t appear until five seconds have passed.

Top comments (0)