Python time.sleep(): How to Add Delays in Your Code (Example)
What Is Python Sleep?
The time.sleep() function pauses the execution of your Python script for a specified number of seconds. It is part of the standard time module and is useful when you need to wait for an external process, simulate latency, or simply delay output.
In this guide you’ll learn:
- How
time.sleep()works and its syntax - Practical examples of using
time.sleep() - Ways to delay function execution
- Alternative delay methods:
asyncio.sleep(),Event().wait(), andTimer
Syntax
import time # Pause for seconds seconds # Example: time.sleep(5) # pauses for 5 seconds
Parameters
seconds – A float or integer indicating how many seconds the program should pause. Fractions of a second are supported.
Example: Basic Use of time.sleep()
Follow these steps to see the delay in action:
import time- Add
time.sleep(5)where you want the pause.
import time
print("Welcome to Python Tutorials")
time.sleep(5)
print("This message appears after a 5‑second pause")
Output
Welcome to Python Tutorials This message appears after a 5‑second pause
Delaying a Function Call
Sometimes you need a delay before a function runs. Place time.sleep() before calling the function:
import time
print('Execution started')
def display():
print('Welcome to Python Tutorials')
time.sleep(5)
display()
print('Function executed with delay')
Output
Execution started Welcome to Python Tutorials Function executed with delay
Other Ways to Introduce Delays
1. Using time.sleep() in a Loop
Print each character of a string with a 1‑second pause:
import time
message = "Guru99"
for char in message:
print(char)
time.sleep(1)
2. Asynchronous Delay with asyncio.sleep()
From Python 3.4 onward, use asyncio.sleep() for non‑blocking delays:
import asyncio
print('Async execution started')
async def display():
await asyncio.sleep(5)
print('Welcome to Python Tutorials')
asyncio.run(display())
3. Threading Event Wait
The Event().wait() method halts execution for a given number of seconds:
from threading import Event
print('Event wait started')
def display():
print('Welcome to Python Tutorials')
Event().wait(5)
display()
4. Threading Timer
A Timer schedules a function to run after a delay:
from threading import Timer
print('Timer started')
def display():
print('Welcome to Python Tutorials')
Timer(5, display).start()
Key Takeaways
- Use
time.sleep()to pause execution for a precise duration. - For non‑blocking delays in async code, use
asyncio.sleep(). - Threading tools like
Event().wait()andTimeroffer alternative delay mechanisms. - Choose the method that best fits your application’s concurrency model.
Python
- Master Python Functions: Syntax, Types, and Practical Examples
- Mastering Python Function Arguments: Positional, Keyword, and Default Parameters
- Python Closures Explained: How Nested Functions Capture Variables
- Mastering Python's time.sleep(): Best Practices, Multithreading, and Real‑World Examples
- Mastering Python’s strip() Method: Comprehensive Guide & Practical Examples
- Understanding Python's Main Function: A Practical Guide to def main()
- Mastering Python’s Yield: Generator vs Return – A Practical Guide
- Mastering Python’s enumerate(): Loop with Indices for Lists, Tuples, Strings, and Dictionaries
- Master Python Attrs: Build Advanced Data Classes with Practical Examples
- Mastering Dates & Times in Python: A Practical Guide