# Printing Array elements in the same line

Although I searched a bit on google but did not find a quick and easy answer.

So suppose we have an array with, say 3 elements

X[1]={a,b};X[2]={w,f,s};X[3]={t,y,u};


Now I want to print them in the same line using Do loop. I use the code

Do[Print[X[i]],{i,3}]


But it will print in different lines. I also want to add text in between two elements. I tried to wrap everything using Row[] but it didn't work.

• Don’t use Do, don’t use Print either. Row or Grid would have been my first choices. Can you show us how exactly you used Row, what output you obtained, and what you wanted to obtain instead? You May also be interested in Riffle to interleave elements into an array. – MarcoB Jun 17 '20 at 2:47
• I tried these two code Row[Do[Print[X[i]], {i, 3}]] and Do[Row[Print[X[i]]], {i, 3}]. The output I want is: "The set is : {a,b} && {w,fs} && {t,yu}". All in a single line. The used do loop as the number of elements (which is 3 here) is not fixed. – Epsilon Jun 17 '20 at 3:24

The output I want is: "The set is : {a,b} && {w,fs} && {t,yu}". All in a single line

 X[1]={a,b};X[2]={w,f,s};X[3]={t,y,u};

And @@ (X[#] & /@ Range[3])


 Print["the set is : ", And @@ (X[#] & /@ Range[3])]


if someone wants to use a different in between symbol, like "--" instead of "&&" what changes will be needed?

If "--" is meant to be just normal text of 2 dashes and not some Mathematical operator, then only way I could think of is to make the whole thing string. Something like

str = StringJoin@Riffle[(ToString[X[#]] & /@ Range[3]), "--"];
Print["the set is : ", str]


• thanks a lot. One small question if someone wants to use a different in between symbol, like "--" instead of "&&" what changes will be needed? – Epsilon Jun 17 '20 at 4:12

I would use:

X[1] = {a, b}; X[2] = {w, f, s}; X[3] = {t, y, u};

Row[Array[X, 3], "&&"]
Row[Array[X, 3], "--"]

{a, b} && {w, f, s} && {t, y, u}
{a, b} -- {w, f, s} -- {t, y, u}