class Board:
def __init__(self):
self.board = [str(i) for i in range(1, 10)] # --> ['1', '2', '3', '4', '5', '6', '7', '8', '9']
def display_board(self):
for i in range(0, 9, 3):
print('|'.join(self.board[i:i+3])) # --> 1|2|3.... "using 'join' method."
if i < 6:
print('-'*5)
class Board:
def __init__(self):
self.board = [i for i in range(1, 10)] # --> [1, 2, 3, 4, 5, 6 7, 8, 9]
def display_board(self):
for i in range(0, 9, 3):
print(*self.board[i:i+3], sep='|') # --> 1|2|3.... "using 'unpacking operator' and 'sep'."
if i < 6:
print('-'*5)
The output:
1|2|3
-----
4|5|6
-----
7|8|9
I need to know when do I use join method not just in this specific case but in general.
The first image is my teacher's implementation he used "join" string method to separate the number with '|' separator.
While I used two of the "print" methods 1. the unpacking operator and 2. sep.
Both of us aimming to the same output shown in the image but I thought to myself 'why would the teacher use this instead of that'.
Note that I'm still learning python, so excuse my ignorance.
Thank you.