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 “url.com/img0001.png” but the 0s are being removed and just showing img1.png

how would i go around keeping the zeros

current = 0001
while True:
	url = "https://url.com/{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"
2 Likes

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"https://url.com/img{n:04}.png"
    print(url)

Output:

https://url.com/img0001.png
https://url.com/img0002.png
https://url.com/img0003.png
https://url.com/img0004.png
https://url.com/img0005.png
https://url.com/img0006.png
https://url.com/img0007.png
https://url.com/img0008.png
https://url.com/img0009.png
https://url.com/img0010.png
2 Likes