Skip to content

Add Time delay in Python | MS (milliseconds), 1 second

  • by

You can use a sleep function to add a delay in the execution of a program. You might be thinking about why needed to make your Python program wait for something?

Here is the answer, You needed a time delay in the program if there is a file to upload or download, or for a graphic to load or user interacting.

Python has built-in time the module has a function sleep() that use to suspend execution of the calling thread for however many seconds you specify.

Syntax

time.sleep(t)

Add Time delay in Python Example

Here is an example of a 5-second delay execution of the next line. You have to import time modules to use the sleep function.

import time
 
print("Start : Before sleep")
time.sleep(5)
print("End : After sleep")

Output:

Time delay in Python Example

Sleep Time milliseconds – ms

it’s easy, you may know 1 second = 1000 milliseconds. So you have to pass the value in sleep function like that – 1/1000 = .001. You can put a float number in the sleep() function.

If you want to make a delay for 100 milliseconds which is 0.1 seconds.

import time
 
print("Start: Before sleep")
time.sleep(.001)
print("End: After sleep")

Output: Start: Before sleep
End: After sleep

Python Time Delay in for loop 

import time

for i in [1, 2, 3, 4]:
    print("After for %s seconds" % i, end='' + '\n')
    print("Current Time:", time.asctime(time.localtime(time.time())))
    time.sleep(i)

Output:

Python time delay

Python delay 1 second

it’s easy, put 1 second = 1 value in function.

import time

time.sleep(1)

Read more about – Python sleep function

Do comment if you have any doubts or suggestions on this tutorial.

Note: This example (Project) is developed in PyCharm 2018.2 (Community Edition)
JRE: 1.8.0
JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o
macOS 10.13.6
Python 3.7
All Python Programs are in Python 3, so it may change its different from python 2 or upgraded versions.

Leave a Reply

Your email address will not be published. Required fields are marked *