Python Forum
What is the significance of end=' ' and print()
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
What is the significance of end=' ' and print()
#1
Hi All,
I am new to Python programming.
I am trying to display nested list like a matrix. I am trying with below code.

x=[[1,2,3],[4,5,6],[7,8,9]]
i=0
for i in range(len(x)):
for j in range(len(x[i])):
print(x[i][j],end=' ')
print()

I am getting data displayed like matrix only.
My doubt here is "what exactly end =' ' and print()" functions are doing.

Can anyone of you clarify me this point.
Thanks,
Subash
Reply
#2
Good point to start is built-in help. In Python interactive interpreter:

>>> help(print)
Help on built-in function print in module builtins:

print(...)
    print(value, ..., sep=' ', end='\n', file=sys.stdout, flush=False)
    
    Prints the values to a stream, or to sys.stdout by default.
    Optional keyword arguments:
    file:  a file-like object (stream); defaults to the current sys.stdout.
    sep:   string inserted between values, default a space.
    end:   string appended after the last value, default a newline.
    flush: whether to forcibly flush the stream.
(END)
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#3
(Sep-06-2019, 10:19 AM)Subash_P Wrote: Hi All,
I am new to Python programming.
I am trying to display nested list like a matrix. I am trying with below code.

x=[[1,2,3],[4,5,6],[7,8,9]]
i=0
for i in range(len(x)):
for j in range(len(x[i])):
print(x[i][j],end=' ')
print()

I am getting data displayed like matrix only.
My doubt here is "what exactly end =' ' and print()" functions are doing.

Can anyone of you clarify me this point.
Thanks,
Subash

Hi!

Although Perfringo has given you an answer, and it's clear that Perfringo has helped many Python users (with more than 800 posts here!!!), I think that maybe, depending on the users and their way of learning (more when they are newbies like myself), they can get lost with the documentation available. I would say that for me, at least, that documentation will be very helpful later on, when I know more about Python, but right now, it gives me more questions than answers... (For instance, what's sys.stdout?, what's flush?...)

If you are like me, a newbie, and one that needs to see what each bit of code does, maybe you just have to do exactly that, by modifying the code and see how that changes the output.

For that, I modified a little program of mine, to show a matrix and its output:

m1 = [['00', '01', '02', '03', '04', '05'], ['10', '11', '12', '13', '14', '15'], ['20', '21', '22', '23', '24', '25'], ['30', '31', '32', '33', '34', '35'], ['40', '41', '42', '43', '44', '45'], ['50', '51', '52', '53', '54', '55'], ['60', '61', '62', '63', '64', '65'], ['70', '71', '72', '73', '74', '75'], ['80', '81', '82', '83', '84', '85']]

print("\n\nBuilding matrix m1 (9x6):\n")
for i in range(len(m1)):
    for j in range(len(m1[i])):
        print(m1[i][j], end=' ')
    print()
That gives the following output:
Output:
Building matrix m1 (9x6): 00 01 02 03 04 05 10 11 12 13 14 15 20 21 22 23 24 25 30 31 32 33 34 35 40 41 42 43 44 45 50 51 52 53 54 55 60 61 62 63 64 65 70 71 72 73 74 75 80 81 82 83 84 85
You asked what end='' and print() do. Let's find out by modifying the code. First, we eliminate the
print()
from the code, so it looks now like this:
m1 = [['00', '01', '02', '03', '04', '05'], ['10', '11', '12', '13', '14', '15'], ['20', '21', '22', '23', '24', '25'], ['30', '31', '32', '33', '34', '35'], ['40', '41', '42', '43', '44', '45'], ['50', '51', '52', '53', '54', '55'], ['60', '61', '62', '63', '64', '65'], ['70', '71', '72', '73', '74', '75'], ['80', '81', '82', '83', '84', '85']]


print("\n\nBuilding matrix m1 (9x6):\n")
for i in range(len(m1)):
    for j in range(len(m1[i])):
        print(m1[i][j], end=' ')
and that changes the output to:

Output:
Building matrix m1 (9x6): 00 01 02 03 04 05 10 11 12 13 14 15 20 21 22 23 24 25 30 31 32 33 34 35 40 41 42 43 44 45 50 51 52 53 54 55 60 61 62 63 64 65 70 71 72 73 74 75 80 81 82 83 84 85
So it seems that print() makes the program to print the next thing in another line (each thing in this case is each row, as
    for j in range(len(m1[i])):
        print(m1[i][j], end=' ')
prints a row of the matrix).
Also notice the indentation, as it is very important in Python. If you change the indentation for print() from the for i loop (the loop starting with
for i in range(len(m1)):
to the for j loop, the loop starting with
for j in range(len(m1[i])):
, the program changes to:
m1 = [['00', '01', '02', '03', '04', '05'], ['10', '11', '12', '13', '14', '15'], ['20', '21', '22', '23', '24', '25'], ['30', '31', '32', '33', '34', '35'], ['40', '41', '42', '43', '44', '45'], ['50', '51', '52', '53', '54', '55'], ['60', '61', '62', '63', '64', '65'], ['70', '71', '72', '73', '74', '75'], ['80', '81', '82', '83', '84', '85']]


print("\n\nBuilding matrix m1 (9x6):\n")
for i in range(len(m1)):
    for j in range(len(m1[i])):
        print(m1[i][j], end=' ')
        print()
producing the following output:
Output:
Building matrix m1 (9x6): 00 01 02 03 04 05 10 11 12 13 14 15 20 21 22 23 24 25 30 31 32 33 34 35 40 41 42 43 44 45 50 51 52 53 54 55 60 61 62 63 64 65 70 71 72 73 74 75 80 81 82 83 84 85
Why is that? Because
print()
makes print the next thing in a new line, and in this case, by being at the same level (indentation) as
print(m1[i][j], end=' ')
, what it does is printing each thing in a new line, but in this case, each thing is each element of the matrix!!!

You asked also what was the meaning of end='', and although it is explained in the documentation provided by Perfringo, I'd try to explain it more visually, by modifying again the original program with the elimination of end='' to see what happens:
m1 = [['00', '01', '02', '03', '04', '05'], ['10', '11', '12', '13', '14', '15'], ['20', '21', '22', '23', '24', '25'], ['30', '31', '32', '33', '34', '35'], ['40', '41', '42', '43', '44', '45'], ['50', '51', '52', '53', '54', '55'], ['60', '61', '62', '63', '64', '65'], ['70', '71', '72', '73', '74', '75'], ['80', '81', '82', '83', '84', '85']]


print("\n\nBuilding matrix m1 (9x6):\n")
for i in range(len(m1)):
    for j in range(len(m1[i])):
        print(m1[i][j],)
    print()
As you can see, I left the comma (,) that was before the end='', but you can also eliminate it:
m1 = [['00', '01', '02', '03', '04', '05'], ['10', '11', '12', '13', '14', '15'], ['20', '21', '22', '23', '24', '25'], ['30', '31', '32', '33', '34', '35'], ['40', '41', '42', '43', '44', '45'], ['50', '51', '52', '53', '54', '55'], ['60', '61', '62', '63', '64', '65'], ['70', '71', '72', '73', '74', '75'], ['80', '81', '82', '83', '84', '85']]


print("\n\nBuilding matrix m1 (9x6):\n")
for i in range(len(m1)):
    for j in range(len(m1[i])):
        print(m1[i][j])
    print()
as both programs, with or without that comma (,) produce the same output (although I guess, doing it without the comma (,) is better code):
Output:
Building matrix m1 (9x6): 00 01 02 03 04 05 10 11 12 13 14 15 20 21 22 23 24 25 30 31 32 33 34 35 40 41 42 43 44 45 50 51 52 53 54 55 60 61 62 63 64 65 70 71 72 73 74 75 80 81 82 83 84 85
So, again, (as with
print()
), it is something related with the display of the result. The end='' makes print the elements of the result without any space between them (in this case, respecting the blank line between the elements of different rows, given by the
print()
line).
And finally, to show that in fact, the blank line between the elements of different rows, is given by the
print()
line in the last output, I'm going to modify, once again, the program to eliminate both the end='' and the print() parts from the program:
m1 = [['00', '01', '02', '03', '04', '05'], ['10', '11', '12', '13', '14', '15'], ['20', '21', '22', '23', '24', '25'], ['30', '31', '32', '33', '34', '35'], ['40', '41', '42', '43', '44', '45'], ['50', '51', '52', '53', '54', '55'], ['60', '61', '62', '63', '64', '65'], ['70', '71', '72', '73', '74', '75'], ['80', '81', '82', '83', '84', '85']]


print("\n\nBuilding matrix m1 (9x6):\n")
for i in range(len(m1)):
    for j in range(len(m1[i])):
        print(m1[i][j])
and now we have a new output:
Output:
Building matrix m1 (9x6): 00 01 02 03 04 05 10 11 12 13 14 15 20 21 22 23 24 25 30 31 32 33 34 35 40 41 42 43 44 45 50 51 52 53 54 55 60 61 62 63 64 65 70 71 72 73 74 75 80 81 82 83 84 85
because end='' (that makes inside the print function to write each new element without any space after the previous one) is not present now, and because print() (that it seems to write a blank line after each row, but that in conjunction with end='', it seems that it writes the following row just in another line) is not present now either.

I hope this clarifies what end='' and print() do for you.

All the best,
newbieAuggie2019

"That's been one of my mantras - focus and simplicity. Simple can be harder than complex: You have to work hard to get your thinking clean to make it simple. But it's worth it in the end because once you get there, you can move mountains."
Steve Jobs
Reply
#4
Another way to display matrix.
x = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
for ele in x:
    print(*ele)
Reply
#5
Two nits to pick. In general you want to avoid non-descriptive variable names like i, j, x. Much easier to debug if you know what the variable is to represent.

Also, when you have the temptation to use range(len(.... dont. You don't have to in Python. The following works fine for the first example and is easy to read and figure out (imo)
m1 = [['00', '01', '02', '03', '04', '05'], ['10', '11', '12', '13', '14', '15'], ['20', '21', '22', '23', '24', '25'], ['30', '31', '32', '33', '34', '35'], ['40', '41', '42', '43', '44', '45'], ['50', '51', '52', '53', '54', '55'], ['60', '61', '62', '63', '64', '65'], ['70', '71', '72', '73', '74', '75'], ['80', '81', '82', '83', '84', '85']]
 
print("\n\nBuilding matrix m1 (9x6):\n")
for row in m1:
    for cell in row:
        print(cell, end=' ')
    print()
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Query regrading plotting 95 % significance level PreetiRajpoot 0 1,526 Mar-09-2021, 04:00 PM
Last Post: PreetiRajpoot

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020