Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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!

share|improve this question
    
Related: (7063), (15032), (20412), (56419) –  Mr.Wizard 10 hours ago

4 Answers 4

up vote 4 down vote accepted

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 *)
share|improve this answer

Or

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

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

share|improve this answer
    
Elegant! You can use StringDelete[..., "{" | "}" | ","] instead of StringTake. –  István Zachar 16 hours ago
1  
Nice. Btw, this does not give the same form the OP wanted due the "," being there between the numbers. May be just need small modification? –  Nasser 16 hours ago
    
May be just need small modification? -- Surely, but left as an exercise for the reader :-) –  High Performance Mark 15 hours ago
    
Nice to see you here @HighPerformanceMark ! :-) –  Mr.Wizard 10 hours ago
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:

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

share|improve this answer
    
I would love this to be uniform for both integer and rational numbers. Thanks! –  SuTron 17 hours ago

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"
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.