How to use zfill to make length equivalent to 2 digit length of n [duplicate]
You can use format string syntax 02
.
change the line : print(*range(1,n+1))
to print(*(f"{i:02}" for i in range(1, n + 1)))
Full code:
n = 15
for i in range(1, n + 1):
print(*(f"{i:02}" for i in range(1, n + 1)))
n = n - 1
output:
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15
01 02 03 04 05 06 07 08 09 10 11 12 13 14
01 02 03 04 05 06 07 08 09 10 11 12 13
01 02 03 04 05 06 07 08 09 10 11 12
01 02 03 04 05 06 07 08 09 10 11
01 02 03 04 05 06 07 08 09 10
01 02 03 04 05 06 07 08 09
01 02 03 04 05 06 07 08
01 02 03 04 05 06 07
01 02 03 04 05 06
01 02 03 04 05
01 02 03 04
01 02 03
01 02
01
Another option is to use zfill
like :
print(*(str(i).zfill(2) for i in range(1, n + 1)))
you need to convert the i
into str because zfill
is a method of string objects.