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
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("")
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
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("")