time.sleep() Function Explained
time.sleep() pauses a Python program for a set amount of time.
It is a simple and useful function for:
- adding a delay in a script
- building a basic countdown
- spacing out repeated output
- practicing program flow
This function is part of Python’s time module, so you must import that module before using it.
Quick example
import time
print("Start")
time.sleep(2)
print("After 2 seconds")
Use time.sleep(seconds) to pause the program. The value can be an integer like 2 or a decimal like 0.5.
What time.sleep() does
time.sleep() stops your program for a number of seconds.
Key points:
- It pauses program execution.
- It belongs to the built-in
timemodule. - You must write
import timebefore using it. - It accepts whole numbers like
2and decimal numbers like0.5.
If you are new to modules, see Python modules explained first.
Basic syntax
The basic syntax is:
time.sleep(seconds)
Here, seconds is the length of the pause.
Examples:
time.sleep(1)pauses for about one secondtime.sleep(0.25)pauses for about a quarter of a second
A full example:
import time
time.sleep(1)
print("Done waiting")
Simple example
This example prints one message, waits, and then prints another message:
import time
print("Before pause")
time.sleep(3)
print("After pause")
Expected output order:
Before pause
After pause
You will see Before pause first. Then the program waits about 3 seconds. After that, it prints After pause.
Using time.sleep() in a loop
time.sleep() is often used inside loops.
This is useful for:
- countdowns
- repeated messages
- making output easier to follow
Example:
import time
for i in range(3):
print("Loop number:", i)
time.sleep(1)
print("Finished")
What happens:
- The loop prints a line.
- The program pauses for 1 second.
- The loop continues.
- After the loop ends,
Finishedis printed.
A beginner-friendly real use case is a countdown timer. For a complete example, see Python countdown timer example.
What value to pass
You can pass different kinds of numbers to time.sleep().
Full seconds
Use an integer when you want a whole number of seconds:
import time
time.sleep(2)
print("Waited 2 seconds")
Fractional seconds
Use a float when you want a shorter pause:
import time
time.sleep(0.5)
print("Waited half a second")
Zero
0 is valid, but it usually creates no visible delay:
import time
time.sleep(0)
print("No visible pause")
Negative numbers
Negative numbers are not valid.
This will cause an error:
import time
time.sleep(-1)
Common errors and confusion
Beginners often run into a few common problems with time.sleep().
Forgetting to import time
This causes a NameError:
time.sleep(2)
Python does not know what time means unless you import it first.
Correct version:
import time
time.sleep(2)
If you want help with this error, see NameError: name is not defined.
Passing a string instead of a number
This causes a TypeError:
import time
time.sleep("2")
"2" is a string, not a number.
Correct version:
import time
time.sleep(2)
You can also convert user input if needed:
import time
seconds = float(input("Enter seconds: "))
time.sleep(seconds)
print("Done")
Using sleep() without the module name
This does not work if you only wrote import time:
import time
sleep(2)
With import time, you must write:
import time
time.sleep(2)
Another valid option is:
from time import sleep
sleep(2)
print("Done")
For beginners, import time and time.sleep(...) is usually clearer.
Expecting exact timing
time.sleep() usually waits about the amount of time you requested, but it is not perfectly exact.
Small timing differences can happen because of:
- your operating system
- other programs running
- normal scheduling delays
So time.sleep(1) means “wait about 1 second,” not “wait with perfect precision.”
When to use it
time.sleep() is a good choice for simple beginner programs.
Common uses:
- simple delays in scripts
- basic countdown timers
- spacing out repeated actions
- learning how program flow works
It is especially helpful when you want to see your program run step by step.
When not to use it
time.sleep() is not the best tool in every situation.
It is not ideal when:
- you need precise timing
- your program must stay responsive during the delay
- you want other work to continue at the same time
To beginners, a sleeping program can look frozen. That is normal. While the program is sleeping, that part of the code is not doing anything else.
FAQ
Does time.sleep() use seconds or milliseconds?
It uses seconds. You can use decimals for part of a second, such as 0.5.
Can I use time.sleep(0.1)?
Yes. A float value is valid and pauses for a fraction of a second.
Why does my program seem frozen?
time.sleep() pauses execution, so nothing else in that part of the program runs during the delay.
Why do I get an error when I use sleep(2)?
You probably forgot to import it correctly.
Use:
import time
time.sleep(2)
Or:
from time import sleep
sleep(2)
Is the delay always exact?
No. It is usually close, but the exact timing can vary slightly.