'How to delete the 0's before the number if the number does not require them?

I'm making a program that prints all possible mixtures of numbers from 0 to 9 and I want to delete the numbers that my code puts before the actual number if the output is less than 6 places.

I've tried putting "" before 0 in every array, but it also puts random spaces in the output.

Class1 = [0,1,2,3,4,5,6,7,8,9]
Class2 = [0,1,2,3,4,5,6,7,8,9]
Class3 = [0,1,2,3,4,5,6,7,8,9]
Class4 = [0,1,2,3,4,5,6,7,8,9]
Class5 = [0,1,2,3,4,5,6,7,8,9]
Class6 = [0,1,2,3,4,5,6,7,8,9]

for i in Class1:
    for j in Class2:
        for k in Class3:
            for l in Class4:
                for m in Class5:
                    for n in Class6:
                        print (i,j,k,l,m,n)

So I want it to output lets say 895 and not 000895. It does that until it reaches 100000, but that's logical.



Solution 1:[1]

Replace:

print (i,j,k,l,m,n)

By:

print (100000*i+10000*j+1000*k+100*l+10*m+n)

Solution 2:[2]

Although the following code helps you with a similar approach you took in your question I would suggest another way of achieving what you want:

Class1 = [0,1,2,3,4,5,6,7,8,9]
Class2 = [0,1,2,3,4,5,6,7,8,9]
Class3 = [0,1,2,3,4,5,6,7,8,9]
Class4 = [0,1,2,3,4,5,6,7,8,9]
Class5 = [0,1,2,3,4,5,6,7,8,9]
Class6 = [0,1,2,3,4,5,6,7,8,9]

for i in Class1:
    for j in Class2:
        for k in Class3:
            for l in Class4:
                for m in Class5:
                    for n in Class6:
                        print(int(''.join([str(i),str(j),str(k),str(l),str(m),str(n)])))

The above method is using the trick of converting individual numbers to strings, concatenating them with join() string method and re-converting them to int. With this,a string like '0000001' will be converted to 1.

But a better way is to utilise itertools.product() with the same trick:

import itertools
for seq in itertools.product([0,1,2,3,4,5,6,7,8,9], repeat=6):
    print(int(''.join([str(s) for s in seq])))

Here inside join() I used list comprehension for brevity.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Dominique
Solution 2 Farzad Vertigo