Signup/Sign In
Ask Question
Not satisfied by the Answer? Still looking for a better solution?

How can I make a time delay in Python?

I want to know the process to how to put a time delay in a Python script.
by

2 Answers

Bharatgxwzm
import time
time.sleep(5) # Delays for 5 seconds. You can also use a float value.

Here is a different case where something is run about once a minute:
import time
while True:
print("This prints once a minute.")
time.sleep(60) # Delay for 1 minute (60 seconds).
sandhya6gczb
Sometimes we need a delay between the execution of two instructions. Python has inbuild support for putting the program to sleep. The time module has a function sleep(). Here is an example to use sleep().

import time
print("hello")
time.sleep(1.2) # sleep for 10=.2 sec
print("world)


For adding time delay in the loop

import time
text="hello world"
for i in text:
print(i)
time.sleep(5)

Using asyncio.sleep() function

import asyncio
print("Hello world")

async def display():
await asyncio.sleep(5)
print("Time sleep added")
asyncio.run(display)

Using Timer

from threading import Timer

print('Hello world')

def display():
print('Timer used')

t = Timer(5, display)
t.start()

Login / Signup to Answer the Question.