Python : how to get rid of comma's

#i want the output without comma
#how to do it?
#now it looks like this:
#[5, 4, 1]
#[4, 1, 1]
#[3, 1, 4]
“”“5 4 1
4 1 1
3 1 4
“””

from random import randint
x = int(input("number: "))
board =
for a in range(x):
row =
for b in range(x):
row.append(randint(0,5))
board.append(row)

def boardy(board):
for x in board:
print("".join(str(x)))

boardy(board)

join takes a list, which if we convert it to a string will contain all the list syntax as printable characters.

We cannot really tell what the objective is. Is this a battleship lesson? If so, please post a link to the exercise.

so what should i do i cant even put just x in .join() as its not a string
my output looks like this:
[5, 4, 1]
[4, 1, 1]
[3, 1, 4]
but i want it this way :
5 4 1
4 1 1
3 1 4
plz help me get it!

if i remove str : it throws following error:

Traceback (most recent call last):
File “C:\Users\DELL\AppData\Local\Programs\Python\Python36-32\play.py”, line 14, in
boardy(board)
File “C:\Users\DELL\AppData\Local\Programs\Python\Python36-32\play.py”, line 12, in boardy
print("".join(x))
TypeError: sequence item 0: expected str instance, int found

join can be used to join string characters in a list to form a single string, or it can be used to insert a separator character (or string) between letters in a string. Consequently it can take a list or a string as arguments.

The key is in how you build the list, to begin with. Convert the numbers to string when appending to the list.

row.append(str(randint(0,5)))

thanks a lot it’s working fine now , i have got it now :grinning:

1 Like
>>> "%20".join('words in a string encoded'.split())
'words%20in%20a%20list%20encoded'
>>> 
>>> ':)'.join('Happy coding! ')
'H:)a:)p:)p:)y:) :)c:)o:)d:)i:)n:)g:)!:) '
>>> 
1 Like