Industrial manufacturing
Industrial Internet of Things | Industrial materials | Equipment Maintenance and Repair | Industrial programming |
home  MfgRobots >> Industrial manufacturing >  >> Industrial programming >> Python

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:

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:

  1. import time
  2. 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

Python

  1. Master Python Functions: Syntax, Types, and Practical Examples
  2. Mastering Python Function Arguments: Positional, Keyword, and Default Parameters
  3. Python Closures Explained: How Nested Functions Capture Variables
  4. Mastering Python's time.sleep(): Best Practices, Multithreading, and Real‑World Examples
  5. Mastering Python’s strip() Method: Comprehensive Guide & Practical Examples
  6. Understanding Python's Main Function: A Practical Guide to def main()
  7. Mastering Python’s Yield: Generator vs Return – A Practical Guide
  8. Mastering Python’s enumerate(): Loop with Indices for Lists, Tuples, Strings, and Dictionaries
  9. Master Python Attrs: Build Advanced Data Classes with Practical Examples
  10. Mastering Dates & Times in Python: A Practical Guide