Group Abstract Group Abstract

Message Boards Message Boards

0
|
58 Views
|
2 Replies
|
4 Total Likes
View groups...
Share
Share this post:

How to make the results generated by multiple print statements display on a single line?

Posted 3 days ago
 a = 2
 b = 3
 c = 5
 Print["a+b=", a + b]
 Print["ab=", a b]
 Print["a-c=", a - c]

The above code invokes the Print[] function line by line, yielding three distinct lines of output.

enter image description here

My desired output format is a single line display, as shown below:

{a + b = 5, ab = 6, a - c = -3}

I've tried implementing the following code on my own, but it doesn't produce the desired result. What's the correct approach to this?

{Print["a+b=", a + b], Print["ab=", a b], Print["a-c=", a - c]}
POSTED BY: Bill Blair
2 Replies

The output is printed without need for an explicit Print:

{"a+b=", a + b, "ab=", a b, "a-c=", a - c}
POSTED BY: Gianluca Gorni

You have to use one Print:

Print[{"a+b=", a + b, "ab=", a b, "a-c=", a - c}]

I'm not sure how you want the combination formatted & punctuated. You might find StringForm[] helpful.

Another format:

Row[{HoldForm[a + b] == a + b,
     HoldForm[a*b] == a*b,
     HoldForm[a - c] == a - c}, ", "] // TraditionalForm
POSTED BY: Michael Rogers
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard