0

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

3 Answers3

3

You habe 3 possibilities:

Community
  • 1
  • 1
Martin Thoma
  • 124,992
  • 159
  • 614
  • 958
2

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)   
rassa45
  • 3,482
  • 1
  • 29
  • 43
2

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', '')

Aaron Digulla
  • 321,842
  • 108
  • 597
  • 820