27
$\begingroup$
Print["first part of the result", DateString[]]   
Print["addition to the result", DateString[]]

will add a newline character at the end of line automatically for each Print[]. But I'd like the two strings to be shown on the same line. How can I do this?

$\endgroup$
3
  • 1
    $\begingroup$ Some users might find PrintTemporary useful: the printed stuff disappears when the computation is over. $\endgroup$
    – anderstood
    Oct 31, 2016 at 22:06
  • $\begingroup$ Not what I asked for. $\endgroup$
    – qazwsx
    Nov 7, 2016 at 1:58
  • $\begingroup$ That's why I did not answer and specified it could be useful to some users... The accepted answer only works with strings, not graphics. PrintTemporary at least works with graphics. $\endgroup$
    – anderstood
    Nov 7, 2016 at 2:41

2 Answers 2

26
$\begingroup$

For Example:

WriteString["stdout", "First part of the result: ", DateString[]];
(*Perform some calc*)
i = 0;
WriteString["stdout", " -- Addition to the result: ", DateString[],  "\n"];

First part of the result: Thu 15 Oct 2015 12:58:29 -- Addition to the result: Thu 15 Oct 2015 12:58:29

$\endgroup$
9
  • $\begingroup$ What's so special about the string "stdout"? I would have thought such thing should be a System` symbol. $\endgroup$
    – qazwsx
    Apr 22, 2012 at 1:41
  • 1
    $\begingroup$ Another question is is there a solution using Print[] instead of WriteString[]? $\endgroup$
    – qazwsx
    Apr 22, 2012 at 1:43
  • $\begingroup$ +1. As I understand, "printing" in such a way is only possible in the plaintext format? $\endgroup$ Apr 22, 2012 at 1:46
  • 1
    $\begingroup$ @AlexeyPopkov Yep. From the help: The special stream "stdout" allows you to give output to the "standard output" provided by the operating system. Note however that you can use this stream only with simple text-based interfaces to Mathematica. $\endgroup$ Apr 22, 2012 at 2:53
  • 1
    $\begingroup$ documentation of this is not very detailed, actually I think (no reference at hand) that "stdout" is just a shortcut for Streams["stdout"] at that position, which will give you those of all open Streams[] which are called "stdout". Print will print to $Output which is a list of OutputStreams, by default having one entry which is once more "stdout". So using $Output would probably most closely reflect the behavior of Print.... $\endgroup$ Feb 24, 2016 at 17:10
6
$\begingroup$

I tried belisarius' answer using WriteString, but it just prints plain text, and you can't print, for example x2. Instead you have to print x^2 which is not cool for reading.

I found a way using Google and Mathematica's help. The main thing I was missing was that under each command found in the help of Mathematica you have "More Information" image which if clicked shows more info about the command. For the Print command, it says that Print uses the Row which is what I use for my solution.

The idea is to create a list. We can do it for example with the command myArray = Range[2];. Then instead of printing what we want to print we will add it in the list myArray[[1]] = "First thing to print";. When we are ready and want to print all we just call the Row[myArray] command which will concatenate the elements of the list and print them with a newline at the end of the concatenated elements.

Here is your solution:

myArray = Range[2];
myArray[[1]] = "first part of the result "  DateString[];
myArray[[2]] = " addition to the result "  DateString[];
Row[myArray] 
$\endgroup$
2
  • 3
    $\begingroup$ Instead of multiplying two strings, you should probably Join them. In infix notation this would be "first part"<>DateString[]. Also, if I understand correctly, the OP wants to use the print statements in two different locations in the code, so this won't help. It might come in handy in other situations though! $\endgroup$
    – sebhofer
    Apr 29, 2012 at 9:38
  • 3
    $\begingroup$ I believe you missed a point here. In the question, the need for two print statements comes from the fact that they are different events in time.You should be able to print something, then calculate whatever you need and then print again in the same line as before $\endgroup$ Feb 24, 2016 at 17:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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