Mark Domino
Mark Domino

Reputation: 25

Strings in 2D lists in Python

This is a normal 2D list in python containing what supposed to be white spaces:

shape = [
["", "", ""],
["", "", ""],
["", "", ""]]

but when printing this array using for loop that's what happens:

['', '', '']
['', '', '']
['', '', '']

isn't it supposed to show only white space ? How do I remove these single quot marks but leave the commas ?

Upvotes: 1

Views: 945

Answers (3)

JacobViehweg
JacobViehweg

Reputation: 1

You can loop through your lists and replace the quotations with empty spaces

for x in shape:
print(str(x).replace("'", ""))

Which outputs:

[, , ]
[, , ]
[, , ]

Upvotes: 0

U13-Forward
U13-Forward

Reputation: 71570

No, it shows everything there is, to remove it, you need to do some string replacing stuff:

print(str(shape).replace("'", '').replace('], [', '],\n ['))

Which outputs:

[[, , ],
 [, , ],
 [, , ]]

Upvotes: 1

JohnO
JohnO

Reputation: 777

If it's just about printing something to the screen, you don't need to first create a list, just create a string like this:

print("[ , , ]\n[ , , ]\n[ , , ]")

Or even

print("[ | | ]\n[ | | ]\n[ | | ]")

Which I think looks better:

[ , , ]                                                                                                                                                                                                                                         
[ , , ]                                                                                                                                                                                                                                         
[ , , ]                                                                                                                                                                                                                                         

[ | | ]                                                                                                                                                                                                                                         
[ | | ]                                                                                                                                                                                                                                         
[ | | ]

Upvotes: 0

Related Questions