Unable to print a range of results with a space between every 3 results

Question:

How can I print a range of results with a space between every three results? 

When I print something like the following:

for i in range(1, 11):
    print(i)

Output I get:

1
2
3
4
5
6
7
8
9
10

Expected output:

1
2
3

4
5
6

7
8
9

10
Asked By: SMTH

||

Answers:

for i in range(1, 11):
    print(i)
    if i % 3 == 0:
        print('')

Should do it!

Basically, it just checks that i is modulo 3. If it is, it will just print an empty line.

Example with a more complex loop

In the case that you have a loop that goes from a to b and that you want to print an empty line every c print, you can do:

for i in range(a, b):
    print(i)
    if (i - a + 1) % c == 0:
        print("")
Answered By: TKirishima
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.