Reputation: 3
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
Reputation: 136187
You habe 3 possibilities:
print(text, end="")
, see https://stackoverflow.com/a/2456292/562769sys.out.write
, see
How to print without newline or space?Upvotes: 3
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
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