How to add time delay in Python?

23 Jun, 2022
In this article, we are going to discuss how to add delay in Python. 
In order to add time delay in our program code, we use the sleep() function from the time module. This is the in-built module in Python we don’t need to install externally.
Time delay means we are adding delay during the execution time in our program code. It should be between two statements or between any part of the program code according to you.
Approach:
Import the time module
For adding time delay during execution we use the sleep() function between the two statements between which we want the delay. In the sleep() function passing the parameter as an integer or float value.
Run the program.
Notice the delay in the execution time.
time.sleep(value)
To understand the topic perfectly. Let’s see the implementation by taking some examples.
Note: As an output, I have shown the GIF, so that you can notice the time delay in the program code during the execution time.
Python3
# importing moduleimport time  # running loop from 0 to 4for i in range(0,5):     # printing numbers  print(i)     # adding 2 seconds time delay  time.sleep(2)
Output:
Python3
# importing time moduleimport time  def message(string):       for i in string:               # printing each character of the message        print(i, end="")                 # adding time delay of half second        time.sleep(0.5)  # main functionif __name__ == '__main__':    msg = "Its looks like auto typing"         # calling the function for printing the    # characters with delay    message(msg)
Output:
Python3
# importing moduleimport time  # function to print the patterndef pattern(n):       for i in range(0, n):        for j in range(0, i+1):                       print('*', end=' ')                         # adding two second of time delay            time.sleep(0.5)        print(' ')  # main functionif __name__ == '__main__':       # taking range from the user    num = 4    print("Printing the pattern")         # calling function to print the pattern    pattern(num)
Output:
Python3
# importingimport timefrom threading import Thread  # making first thread of Geeksclass Geeks(Thread):       def run(self):        for x in range(4):            print("Geeks")                         # adding delay of 2.2 seconds            time.sleep(2.2) # making second thread of Forclass For(Thread):       def run(self):        for x in range(3):            print('For')                         # adding delay of 2.3 seconds            time.sleep(2.3)  print("Hello") # making the object for both the# threads separatelyg1 = Geeks()f1 = For() # starting the first threadg1.start() # starting the second threadf1.start() # waiting for the both thread to join# after completing their jobg1.join()f1.join() # when threads complete their jobs# message will be printedprint("All Done!!")
Output:
The threading.Event.wait procedure, the thread waits until the set() method execution is not complete. Time can be used in it; if a specific time is set, execution will halt until that time has passed; after that, it will resume while the set() of an event is still active.
What is the Syntax of threading.Event.wait function
threading.Event.wait()
Example:
Python3
from time import sleep if __name__ == '__main__':    # delay in seconds    delay = 2     print('Geeks')    sleep(delay)    print('for')    sleep(delay)    print('Geeks')
Output:
Geeks
for
Geeks
Actions that need to be scheduled to begin at a specific time are represented by timer objects. These items are scheduled to execute on a different thread that performs the action.
What is the Syntax of threading.Timer function
threading.Timer(interval, function)
Example:
Python3
# Program to demonstrate# timer objects in python import threading def gfg():    print("Computer Science: GeeksforGeeks\n")  timer = threading.Timer(1.0, gfg)timer.start()print("Timer")
Output:
Timer
Computer Science: GeeksforGeeks
surajkumarguptaintern
Picked
Python time-module
Technical Scripter 2020
Python
Technical Scripter
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to Install PIP on Windows ?
Python Classes and Objects
Python OOPs Concepts
Introduction To PYTHON
How to drop one or multiple columns in Pandas Dataframe
Python | os.path.join() method
Check if element exists in list in Python
How To Convert Python Dictionary To JSON?
Python | Get unique values from a list
Python | datetime.timedelta() function