0

I need to print to stdout (but next step is print to file) an array of strings, containing the info to build-up a shape. Below, the for loop:

for i in range(0, xMax):
    for j in range(0, yMax):
        print arena_shape[ j + i*yMax]
    print('\r')

The array arena_shape[] contains the chars to built-up a 2D figure, figure with i = [0, xMax] rows and j = [0, yMax] columns. I expect the output to be :

i=0    ----------    ------
i=1    |                  |
       |                  |
       |                  |
       |                  |
       |                  |
       |                  |
i=xMax --------------------

instead to get the first line (i=0, j=[0,yMax]) in "horizontal" as I would expect, I get it displayed in vertical, even if I tell python to \r only when changing row i and not at each column j

i=0 -
    -
    -
    -
    -
    -
    -
    -
    -
    -




    -
    -
    -
    -
    -
    -

I don't understand why this is not changing line after the print("\r") instruction. Thank you in advance for the help.

user123892
  • 1,243
  • 3
  • 21
  • 38
  • Possible duplicate of [How to print in Python without newline or space?](http://stackoverflow.com/questions/493386/how-to-print-in-python-without-newline-or-space) – Chiel Apr 12 '16 at 16:03
  • What exactly is the issue? To print in python without a newline, just tack a comma (`,`) to the end of the line. – kirkpatt Apr 12 '16 at 16:19
  • sorry, I was not clear, I edited the question. – user123892 Apr 12 '16 at 17:36

1 Answers1

2

The print function in Python always prints in a new line. If you want to print without a new line i suggest storing what you need to print in a temporary variable and printing it only when you exit the inner for loop

for i in range(0, xMax):
    temp_variable = ''
    for j in range(0, yMax):
        temp_variable += arena_shape[ j + i*yMax]
    print temp_variable
LucasP
  • 293
  • 2
  • 15