Python Programming (132 Blogs) Become a Certified Professional
AWS Global Infrastructure

Data Science

Topics Covered
  • Business Analytics with R (32 Blogs)
  • Data Science (38 Blogs)
  • Mastering Python (71 Blogs)
  • Decision Tree Modeling Using R (1 Blogs)

How to implement Time Sleep in Python?

Published on Sep 25,2019 194 Views

Today, everywhere you look around you will find applications. While all these applications have been coded in various different programming languages, one of the most popular till date is the Python programming language. In this article, we will know more about the famous module, time sleep in Python in the following sequence:


Introduction to Time Sleep in Python

In day to day programming there often comes the need of pausing a program in between so that other operations can take place. While halting a program in between can serve a specific purpose, it can also simply add to the efficiency of the entire operation. Whatever might be the need, the sleep() module in Python can be used to achieve this.

The use of the sleep() module provides an accurate and flexible way to do this. In the latest versions of Python that is Python 2 and 3, the sleep module has been replaced with the time module, and both of them serve the same function.



In the above syntax, sec is used to define the number of seconds for which the execution will be halted or paused.

In order to understand the use of the sleep function better, take a look at the example below:

# Python code to demonstrate
# working of sleep()

import time

# printing the start time
print("The time of code execution begin is : ", end ="")

# using sleep() to hault the code execution

# printing the end time
print("The time of code execution end is : ", end ="")


Output 1 - time sleep in python - edureka


Let us take another example to understand how the delay function works in Python:

import time
print("Printed immediately.")
print("Printed after 2.4 seconds.")

In the above program, the first string is printed immediately, followed by the second string which is printed after a delay of 2.4 seconds as mentioned in the time.sleep module.


Output 2 - time sleep in python - edureka


Applications for Sleep Module

Similar to all other modules present within the Python interface, the sleep function serves a multitude of applications. One of the most significant uses of the sleep function can be to execute a background thread at regular intervals. Another great use of the sleep function can be to print a string letter by letter, for better user experience.

To understand this application better, take a look at the example below:

# Python code to demonstrate
# application of sleep()

import time

# initializing string
strn = "Edureka says Hello!"

# printing geeksforgeeks after delay
# of each character
for i in range(0, len(strn)):
print(strn[i], end ="")



Let us take a look at another example, where we create a digital clock by using the time.sleep module in Python:

import time
while True:
localtime = time.localtime()
result = time.strftime("%I:%M:%S %p", localtime)

If you see the above program, you will find that we have printed the local time multiple times inside the infinite while loop and this has been achieved through the time.sleep function. After the first iteration, the program waits for 1 second, computes the local time and then prints it, and this repetition is computed infinite times unless prompted to stop.



Mentioned below is a slightly modified version of the above program:

import time
while True:
localtime = time.localtime()
result = time.strftime("%I:%M:%S %p", localtime)
print(result, end="", flush=True)
print("r", end="", flush=True)



Time and sleep modules in Python multithreaded programs

The time and sleep module can be used in multithreaded python programs as well to achieve certain outcomes. The main difference between its uses in single-threaded and multithreaded programs is the fact that in single-threaded programs, the sleep function suspends execution of the thread as well as the process. On the other hand, in multithreaded programs a single thread rather than the whole process is suspended.

To understand this concept better, take a look at the example below:

import threading
import time

def print_Edureka():
for i in range(4):
print("Edureka ")

def print_Python():
for i in range(4):
t1 = threading.Thread(target=print_ Edureka)
t2 = threading.Thread(target=print_ Python)

In the above program, there are two threads each with a delay of 0.5 and 0.75 seconds respectively. These are executed simultaneously when the program is run in the interpreter without halting the entire process.



The time and sleep modules in Python can be used to achieve a lot of different purposes. From the above examples, we hope that you have learned their individual functions, differences and how you can use them in your day to day usage.

Now that you have understood what is Python, check out the Python Programming Certification by Edureka, a trusted online learning company with a network of more than 250,000 satisfied learners spread across the globe.

Got a question for us? Please mention it in the comments section of this “Time Sleep in Python” blog and we will get back to you as soon as possible.

Upcoming Batches For Python Certification Training Course
Course NameDate
Python Certification Training Course

Class Starts on 28th May,2022

28th May

SAT&SUN (Weekend Batch)
View Details
Python Certification Training Course

Class Starts on 18th June,2022

18th June

SAT&SUN (Weekend Batch)
View Details

Join the discussion

Browse Categories

Send OTP
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP

Subscribe to our Newsletter, and get personalized recommendations.

image not found!
image not found!

How to implement Time Sleep in Python?