For loop starting from 00001? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

For loop starting from 00001?

Let's say I want to make a counter (a loop) that start from the integer 00001 and increase in one on each iteration... the classic for loop. There's another option I've trying to do by passing value (00001) to a variable or list, but when I try to add 1 (00001 + 1), the result is just 2; I mean, the zeros despairs. Can anyone help me with this issue, please?

6th Aug 2016, 12:46 AM
Francisco Gómez García
Francisco Gómez García - avatar
1 Answer
0
Ofc that would happen. The extra zeros before the actual integer is not required in arithmetics. If you want to print out the counter value with the 0s before it, try converting it to string and joining the 0s to it. Ofc the amount of 0s would have to differ depending on the number. Check the length of the number and add the 0s accordingly. e.g s = str(counter) if len(counter) == 1: s = "0000"+s elif len(counter) == 2: s = "000"+s etc... There are others ways you could achieve the same effect like using zfill() but this should suffce.
6th Aug 2016, 9:28 AM
Gershon Fosu
Gershon Fosu - avatar