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.
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?

share|improve this question
add comment

2 Answers

up vote 12 down vote accepted

For Example:

WriteString["stdout", "First part of the result: ", DateString[]];
(*Perform some calc*)
i = 0;
WriteString["stdout", " -- Addition to the result: ", DateString[],  "\n"];
share|improve this answer
    
What's so special about the string "stdout"? I would have thought such thing should be a System` symbol. –  Problemaniac Apr 22 '12 at 1:41
    
Another question is is there a solution using Print[] instead of WriteString[]? –  Problemaniac Apr 22 '12 at 1:43
    
+1. As I understand, "printing" in such a way is only possible in the plaintext format? –  Alexey Popkov Apr 22 '12 at 1:46
    
@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. –  belisarius Apr 22 '12 at 2:53
    
@MaThEmAtika Print[] is a special case of CellPrint[], which creates a new cell upon each call. I can't find a way to prevent that happening. –  belisarius Apr 22 '12 at 3:20
show 2 more comments

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] 
share|improve this answer
2  
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! –  sebhofer Apr 29 '12 at 9:38
add comment

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.