user11402812
user11402812

Reputation:

Getting Rid Of Extra Characters When Printing

I am printing out this canvas in python that I am using for a board. When I populate it and print with

 for row in canvas:
   print(row)

I prints like this :

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

I need it to print like this

 . . . .
 . . . .
 . . . .

Is there something I can do to strip it by chance?

Thank you

Upvotes: 0

Views: 53

Answers (4)

Daweo
Daweo

Reputation: 36450

As you are using Python 3, where print is function, you might do:

canvas = [['.','.'],['.','.']]
for row in canvas:
    print(*row)

Output:

. .
. .

I used so called unpack operator here (* before row), if you want to know more about that I suggest this short article

Upvotes: 0

Kartikeya Sharma
Kartikeya Sharma

Reputation: 1383

Here you go. Just use the string join method:

canvas=[['.', '.', '.', '.'],
['.', '.', '.', '.'],
['.', '.', '.', '.']]

for row in canvas:
   print(" ".join(row))

Upvotes: 1

Alex Ding
Alex Ding

Reputation: 158

You're using the Python default printing method of a list. What you want is to construct a string from your list that looks the way you want.

This should do the trick

for row in canvas:
  print(" ".join(row))

Upvotes: 2

gregory
gregory

Reputation: 12905

Take the list and convert it to string, then print it:

for row in canvas:
   print("".join(row))

Upvotes: 0

Related Questions