How do I make a long list print in segments?

I'm trying to learn python by writing code I'm interested in, currently I want to list all of the primes up to a number of my choice at runtime. However, when I print the primes they just all get blurted out on one line. If you enter a big number this gets really really long. How can I make it so that my print always does like 10 indices then starts a new line?

Note: this is extremely verbose just for debugging purposes.

My code:

primes = []
numberToGoTo = int(input("Find primes up to what number?\n"))

for i in range(2, numberToGoTo):
    for j in range(2, i):
        print("Checking to see if {} is a prime.  Current divisor: {}".format(i, j))
        if i % j == 0 and j > 1 and j < i:
            print("{} is not prime".format(i))
            break
    else:
        print("{} is a prime! Adding to list.".format(i))
        primes.append(i)
else:
    #number of primes
    print(len(primes))
    #list all primes
    print(primes)

1 answer

  • answered 2018-05-16 05:52 Ashwani Shakya

    You can use create custom print function to do this.

    def custom_print(primes, index):
        index /= 2
        lim = index
        while(lim <= len(primes)):
            print(primes[lim-index:lim+index])
            lim += index
    
    custom_print(primes, 10)
    

    I hope this helps.