Build a Text Progress Bar

Medium ⏱ 12 min 53% acceptance ★★★★★ 4.6
Write a function progress_bar(percent, width=10) that returns a text progress bar string like "[####------] 40%" for percent=40, width=10 — the bar has width characters total, filled proportionally with # and the rest with -, followed by " <percent>%".

Examples

Example 1
Input
progress_bar(40, 10)
Output
'[####------] 40%'
Explanation

40% of 10 = 4 filled chars, 6 empty.

Example 2
Input
progress_bar(100, 5)
Output
'[#####] 100%'
Explanation

Fully filled bar at 100%.

Constraints

  • percent is an int from 0 to 100 inclusive.
  • width defaults to 10 but callers may pass another value.

Topics

Input & Outputstring building

Companies

AtlassianServiceNow

Hints

Hint 1

filled = round(width * percent / 100).

Hint 2

Build the string with "#" * filled + "-" * (width - filled).

Loading the Python runtime… Run executes your code and shows printed output; Submit checks your function against this problem's examples.