Need Help With Python Development?

Work with our skilled Python developers to accelerate your project and boost its performance.

Hire Python Developers

Support On Demand!

Adding a progress bar in Python is a great way to enhance user experience for long-running tasks. Whether you’re iterating over a loop, downloading files, or waiting for a time-consuming process, progress indicators can be easily implemented using libraries or manual approaches.

Method 1: Using the tqdm Library (Recommended)

The tqdm library is the most popular and simple way to display a progress bar.

Example:

from tqdm import tqdm
import time

# Simulating a long-running loop
for i in tqdm(range(100)):
   time.sleep(0.05)  # Simulated workload

Output:

A real-time progress bar with estimated time and percentage completes in the terminal.

Method 2: Manual Progress Bar with sys.stdout

For more control or if you don’t want external dependencies:

import sys
import time

def print_progress_bar(iteration, total, length=50):
   percent = ("{0:.1f}").format(100 * (iteration / float(total)))
   filled_length = int(length * iteration // total)
   bar = '█' * filled_length + '-' * (length - filled_length)
   sys.stdout.write(f'\rProgress: |{bar}| {percent}% Complete')
   sys.stdout.flush()
# Simulate a task
total = 100
for i in range(total):
   time.sleep(0.05)
   print_progress_bar(i + 1, total)

Related Q&A