How to make a Python Program Pause for X seconds


You can pause a Python Program for X seconds using the sleep() function from he time module.

Example 1:
import time

print("Gonna pause for 5 seconds...")
time.sleep(5)
print("Program completed.")

Let's see the execution of this program in action using a gif demo.

Pause Python Program for 5 seconds Example

As you can see the program waited/paused for the execution of the next line to time.sleep(5) for 5 seconds.


Example 2:

Not let's see an example with a for a loop.

import time

print("Countdown of 10 seconds:")
for x in range(10, 0, -1):
  time.sleep(1)
  print(x)
print("Countdown complete!") 
Countdown of 10 seconds:
10
9
8
7
6
5
4
3
2
1
Countdown complete!

Facing issues? Have Questions? Post them here! I am happy to answer!

Author Info:

Rakesh (He/Him) has over 14+ years of experience in Web and Application development. He is the author of insightful How-To articles for Code2care.

Follow him on: X

You can also reach out to him via e-mail: rakesh@code2care.org

Copyright © Code2care 2024 | Privacy Policy | About Us | Contact Us | Sitemap