2

Considering we have an array:

a = {2/3, 4/5, 9/7, 3/7, 1/7, 1/9};

How can I get this to be:

"2/3 4/5 9/7 3/7 1/7 1/9"

Thanks!

Mr.Wizard
  • 271,378
  • 34
  • 587
  • 1,371
SuTron
  • 1,708
  • 1
  • 11
  • 21

5 Answers5

6

I would love this to be uniform for both integer and rational numbers

a2 = {2/3, 4/5, 9/7, 3/7, 1.5, 3, 1/9};


StringTrim@StringJoin[" " <> ToString[#, InputForm] & /@ a2]
(* 2/3 4/5 9/7 3/7 1.5 3 1/9 *)

Row[ToString[#, InputForm] & /@ a2, "  "]
(* 2/3  4/5  9/7  3/7  1.5  3  1/9 *)

StringReplace[ToString[a, InputForm], {"{" | "}" -> "", "," -> " "}]
(* 2/3  4/5  9/7  3/7  1.5  3  1/9 *)

StringJoin[Riffle[ToString[#, InputForm] & /@ a2, " "]]
(* 2/3 4/5 9/7 3/7 1.5 3 1/9 *)
kglr
  • 394,356
  • 18
  • 477
  • 896
4

Or

StringTake[ToString[a, FormatType -> InputForm], {2, -2}]

The inelegant use of StringTake strips off the leading and trailing brackets.

High Performance Mark
  • 1,534
  • 1
  • 10
  • 17
2

Yet another possibility:

a = {2/3, 4/5, 9/7, 3/7, 1/7, 1/9, 5, 6, 99/10};
ExportString[{a}, "Table", "FieldSeparators" -> " "]
   "2/3 4/5 9/7 3/7 1/7 1/9 5 6 99/10"
J. M.'s missing motivation
  • 124,525
  • 11
  • 401
  • 574
2
a = {2/3, 4/5, 9/7, 3/7, 1/7, 1/9};
StringJoin@Cases[a, Rational[x_, y_] :> 
   " "<>ToString[x] <> "/" <> ToString[y]]

Mathematica graphics


Reply to comment "I would love this to be uniform for both integer and rational numbers":

a = {2/3, 4/5, 9/7, 3/7, 1/7, 1/9, 5, 6, 99/10};
f[Rational[x_, y_]] := " " <> ToString[x] <> "/" <> ToString[y];
f[x_] := " " <> ToString[x];
StringJoin[f[#] & /@ a]

Mathematica graphics

J. M.'s missing motivation
  • 124,525
  • 11
  • 401
  • 574
Nasser
  • 143,286
  • 11
  • 154
  • 359
2

Terse:

a = {1, 17, 2/3, 4/5, 9/7, 3/7, 1/7, 1/9};

ToString @ Row[InputForm /@ a, " "]
"1 17 2/3 4/5 9/7 3/7 1/7 1/9"
Mr.Wizard
  • 271,378
  • 34
  • 587
  • 1,371