Afzal Sayyed
Afzal Sayyed

Reputation: 33

how to not print newline at end of statement in for loop

r, c = input().split()
r=int(r)
c=int(c)
list1=[]
v=1
for i in range(r):
    list2=[]
    for j in range(c):
        list2.append(v)
        v=v+1
    list1.append(list2)


for i in range(r):
    for j in range(c):
        print(list1[i][j],end=" ")
    print()        

Here is an image showing the actual output and the output I am getting:

Upvotes: 2

Views: 5690

Answers (3)

Warren Benson
Warren Benson

Reputation: 11

I had the same issue, this is what I did: >>> 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.

I am very new to python, but this is my code to eliminate a new line at the end of a print statement:

for ch in message:
    print (ord(ch), end=' ')

If I wanted to eliminate the ' ' at the end of each line of my statements, since this comes from the default (sep=" "), then I would use the following:

for ch in message:
        print (ord(ch), ch, sep = '' if ch==message[-1] else ' ', end=' ', )

#Please note that message is a string.

Upvotes: 1

Patrick Artner
Patrick Artner

Reputation: 51643

You can create sublist that partition the data you need to print. Before printing the eacht part, test if you need to print a '\n' for the previous line and print partitiones without '\n':

r, c = map(int, input().split())

# create the parts that go into each line as sublist inside partitioned
partitioned = [ list(range(i+1,i+c+1)) for i in range(0,r*c,c)]
#                       ^^^^ 1 ^^^^              ^^^^ 2 ^^^^

for i,data in enumerate(partitioned):
    if i>0: # we need to print a newline after what we printed last
        print("")

    print(*data, sep = " ", end = "") # print sublist with spaces between numbers and no \n
  • ^^^^ 1 ^^^^ creates a range of all the numbers you need to print for each partition
  • ^^^^ 2 ^^^^ creates the starting numbers of each "row" thats used in ^^^^ 1 ^^^^ (reduced by 1 but thats fixed in 1's range)
  • enumerate(partitioned) returns position inside sequence and the data at that position - you only want to print '\n' after the first output was done.

After the last partitioned - output the for ... is finished and wont enter again - hence no \n after it.


Output for '6 3' (\n added for claritiy reasons):

1 2 3\n
4 5 6\n
7 8 9\n
10 11 12\n
13 14 15\n
16 17 18

with partitioned being:

[[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12], [13, 14, 15], [16, 17, 18]]

Upvotes: 0

Mad Physicist
Mad Physicist

Reputation: 114310

The issue is that you need to skip the newline at the end of the outermost loop and the spaces at the end of each line. For a general iterator, this requires a bit of extra work, but for your simple case, just checking iand j will suffice:

for i in range(r):
    for j in range(c):
        print(list1[i][j], end=" " if j < c - 1 else "")
    if i < r - 1:
        print()

Upvotes: 2

Related Questions