How to Use Time Sleep in Python Script?

In Python programming methods you can use sleep() methods available under time stamp big module. This will sleep or suspends(waits) delay source code with script run as well as execution for the determine total number of seconds.The sleep() wait method to delay or suspends (waits) run with execution of the active more thread for a specify total number of seconds. you can also read my prev article for python absolute value.

Python time sleep() Method

You can use simple helpful to syntax of the time.sleep(seconds) as per below sleep milliseconds example. The data value passed to user can sleep() method is in second format.

Syntax

Below is the simple syntax for sleep() until wait function − Get Current Date & Time in Python

time.sleep(t)
python sleep milliseconds,python sleep in minutes,python sleep stack,stackoverflow python sleep,python sleep until,python sleeping hours,python delay without sleep,raspberry pi python sleep,python wait
python sleep milliseconds

Example 1: Python sleep()

import time
 
time.sleep(8)  # delays for 8 second
time.sleep(2)  # delays for 2 second
time.sleep(5)  # delays for 5 second
time.sleep(3)  # delays for 3 seconds
time.sleep(60)  # delays for 1 minute

Example 2: Python Delay Example

Now, Let’s make a tiny script current time source sleeping code to display current date time with Sleep every after 8 seconds.

import time
 
while True:
    print("Good Morning Dear " + time.strftime("%c"))
    time.sleep(8) # delays execution by 8 seconds

and then you can Execute above simple full script on CMD or Terminal command line. To wait and exit your running mode script press simple CTRL + C. You will great time stamp format display outputs something like as a below way

Good Morning Dear Mon Apr 25 15:39:50 2021
Good Morning Dear Mon Apr 25 15:39:58 2021
Good Morning Dear Mon Apr 25 15:40:06 2021
Good Morning Dear Mon Apr 25 15:40:14 2021
Good Morning Dear Mon Apr 25 15:40:22 2021

Example 3: Python create a digital clock with current time

import time

while True:
  mypakatime = time.localtime()
  output = time.strftime("%I:%M:%S %p", mypakatime)
  print(output)
  time.sleep(1)

output

01:11:50 AM
01:11:51 AM
01:11:52 AM
01:11:53 AM
01:11:54 AM
... .. ...
Web Programming Tutorials Example with Demo

Read :

Summary

You can also read about AngularJS, ASP.NET, VueJs, PHP.

I hope you get an idea about how to sleep in python?.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.

Leave a Comment