I'd like to know how to print multiple lines from Python

Asked 2 years ago, Updated 2 years ago, 168 views

Hello, I'm a beginner at Python.


a=10
b=7
c=5
d=9
e=8
f=2

print(a,b,c,d,e,f)
>>> 10 7 5 9 8 2

How do I print out by breaking it into multiple lines?

10 7 5
9 8 2

3 lines per line, 2 lines in total, or

10 7
5 9
8 2

2 lines per line, 3 lines in total, etc.

Is there any way I can adjust the number of variables that can fit in a line?

I'd appreciate it if you could help me

python print

2022-09-22 19:57

2 Answers

Hello, everyone

I think you can adjust it using print('test', end=') and print().

a = 10
b = 7
c = 5
d = 9
e = 8
f = 2

l = [a, b, c, d, e, f] # I made it into a list

dec_print(row, col): # Gets the number of lines and lines to print
    i = 0
    For_in range(row): # Repeat enough!
        For c in range (col): # Repeat as many times!
            if i+c < len(l):
                print(l[i + c], end=' ')
        print()
        i = i + col

rc_print(3,2)
# 10 7 
# 5 9 
# 8 2 

rc_print(2,3)
# 10 7 5 
# 9 8 2 

rc_print(2,5)
# 10 7 5 9 8 
# 2 

Just in case, an easy way ver.

a=10
b=7
c=5
d=9
e=8
f=2

print(a, b, c)
print(c, e, f)

Have a nice day


2022-09-22 19:57

def myprint(n, result):
    for i in range(len(result)):
        if i%n == 0:print()
        print(result[i],  end = ' ')

>>> rgs = tuple(range(30));myprint(5, rgs)

0 1 2 3 4 
5 6 7 8 9 
10 11 12 13 14 
15 16 17 18 19 
20 21 22 23 24 
25 26 27 28 29 
>>> myprint(7, rgs)

0 1 2 3 4 5 6 
7 8 9 10 11 12 13 
14 15 16 17 18 19 20 
21 22 23 24 25 26 27 
28 29

Alternatively, you can use the enumerate function

def myprint(n, result):
    for k, v in enumerate(result, 1): 
        print('{}'.format(v), end='\n' if k % n == 0 else ' ')


2022-09-22 19:57

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.