Dporth Dporth - 5 months ago 7
Python Question

Interpretation of .join()

Original code:

board = []

for x in range(5):
board.append("O" * 5)
print(board)


Why does the code print each index in its own row when you add this to the code above?

board = []

for x in range(5):
board.append("O" * 5)

def print_board(board):
for row in board:
print(" " .join(row))

print_board(board)

Answer

Since you're doing print in a for loop, everytime the loop is iterating through the row, it's doing a new print statement on the next line.

Think of it this way.

for i in range(5):
    print (i)

makes

0
1
2
3
4