Benschilibowl
Benschilibowl

Reputation: 3

Im trying to print a series of strings on the same line

I'm trying to print three strings next to each other like so:

       StringA   StringB   StringC

However, whenever I run my code no matter what I've tried it always prints them on different lines. How do I fix this? My code is as follows:

def DisplayCard(row, column, array):
   x=0
   t=""
   while x < column:
      s = array[x]
      t = ''.join(s)
      x=x+1
      print(t),

Where array is the data passed into the function in the form of a list. Also forgot to mention im running 2.7

Upvotes: 0

Views: 669

Answers (3)

Martin Thoma
Martin Thoma

Reputation: 136187

You habe 3 possibilities:

Upvotes: 3

rassa45
rassa45

Reputation: 3550

Try using print(thing, end = ''). That should work fine

def DisplayCard(row, column, array):
   x=0
   t=""
   while x < column:
      s = array[x]
      t = ''.join(s).replace("\n", "")
      x=x+1
      print(t, end = ''),

Either that or append them to one string

def DisplayCard(row, column, array):
   x=0
   t=""
   while x < column:
      s = array[x]
      t += ''.join(s).replace("\n", "")
      x=x+1
   print(t)   

Upvotes: 2

Aaron Digulla
Aaron Digulla

Reputation: 328546

print(t), should work unless t contains a newline. Use print repr(t), to find out. If you now see \n, then the array contains newline characters which you need to remove first.

If the newlines are at the end of the string, you can remove them with t.strip(). If they are in the middle, use t.replace('\n', '')

Upvotes: 2

Related Questions