Python dumping leading zeros

im trying to make a simple web crawler which runs through a url and downloading the images from it and the url starts with “” but the 0s are being removed and just showing img1.png

how would i go around keeping the zeros

current = 0001
while True:
	url = "{a}.png"
	current = current + 1
	print(url.format(a = current))
1 Like

A few spots here.

First, when you say current = 0001, it’s the same than current = 1. They are the same number. I think you are looking for a strict 0001, so you may have to use strings instead, something like current = "0001". The issue is that you’ll have figure out a way to modify current, since current = current + 1 won’t work now.

1 Like

How you format a number (leading zeros, scientific notation, significant figures) isn’t intrinsic to the number itself, but something you specify when converting the number to a string.

num = 1
assert f"{num:04}" == "0001"

Please consider the following example, and modify or incorporate it into existing code to suit your needs:

upper_limit = 10
for n in range(1, upper_limit + 1):
    url = f"{n:04}.png"