Home > Blockchain >  for loop range - result in columns/horizontally
for loop range - result in columns/horizontally

Time:07-18

How can i get result in columns instead?, i have tried with for [a-c], (a-c), but it will give error message.

for a in range(1,6):
    print(f'{a}')

for b in range(1,6):
    print(f'{b}')

for c in range(1,6):
    print(f'{c}')

Desired result:

1    1    1
2    2    2
3    3    3
4    4    4  
5    5    5

Current result:

1
2
3
4
5
1
2
3
4
5
1
2
3
4
5

CodePudding user response:

You can change the end from \n to space :

for a in range(1,6):
    print(f'{a}', end=' ')
print()

for b in range(1,6):
    print(f'{b}', end=' ')
print()

for c in range(1,6):
    print(f'{c}', end=' ')
print()

** UPDATE **: after updating your question, here is my solution:

a = range(1, 6)
b = range(1, 6)
c = range(1, 6)

for index in range(len(a)):
    print(f'{a[index]}    {b[index]}    {c[index]}')

output:

1    1    1
2    2    2
3    3    3
4    4    4
5    5    5
  • Related