Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I would like to Export a text file, and use paragraph / ENTER commands to place different strings on different lines. Say we have three strings - str1, str2, and str3. How can we place them on separate lines of a text file? This command -

Export["test.txt", StringJoin[str1, str2, str3]];

Places str1, str2, and str3 on a single line.

share|improve this question
Try Export["test.txt",{str1, str2, str3}]. – b.gatessucks Sep 15 '13 at 0:04
@b.gatessucks Works. Terrific. But is there a general way to specify some line depth for each string? – DFr Sep 15 '13 at 0:05
How do you define "line depth"? – C. E. Sep 15 '13 at 0:15
@Anon I define line depth as the line # in a text file. For example "a" [ENTER] "b" [ENTER] [ENTER] "c" would place "c" at a so-called line depth of 4 (since it's four lines down from the top of the file). – DFr Sep 15 '13 at 0:18
OK, just making sure. You can put elements "" instead of strings in the list. E.g. {str1,"","",str2} will make the line depth of str2 be 4. If you say how you intend to indicate on what line a string should appear, such a list could be generated programmatically. – C. E. Sep 15 '13 at 0:22
up vote 8 down vote accepted

Perhaps something like the following will work for you.

str[1] = "The quick brown fox jumped over the lazy dog";
str[2] = "Grumpy wizards make a toxic brew for the jovial queen";
str[3] = "The five boxing wizards jumped quickly";

text = Normal[SparseArray[{1 -> str[1], 2 -> str[2], 5 -> str[3]}]] /. {0 -> ""}

Export[FileNameJoin[{HomeDirectory[], "Desktop", "test.txt"}], text]

The contents of test.txt will look like this:

The quick brown fox jumped over the lazy dog  
Grumpy wizards make a toxic brew for the jovial queen  

The five boxing wizards jumped quickly


If the strings are indexed by line number, as Yves Klett suggests, then this very simple code will suffice:

str[1] = "line 1"; str[2] = "line 2"; str[5] = "line 5";
text = Table[str[i], {i, 5}] /. str[_] -> ""

Mr.Wizard has come up with an even more concise variant of the above.

str[_] = ""; text = Array[str, 5]

Another edit

Mr.Wizard has also supplied a neat variant of my original SparseArraysolution.

Normal @ SparseArray[{1 -> str[1], 2 -> str[2], 5 -> str[3]}, Automatic, ""]
share|improve this answer
neat use of sparse arrays +1 – s0rce Sep 15 '13 at 3:36
How about directly using the line number as argument of str? Saves on wear and tear during automation... – Yves Klett Sep 15 '13 at 12:17
@YvesKlett. Good idea. I've edited my answer to incorporate your suggestion. – m_goldberg Sep 15 '13 at 16:34
I think this can be cleaned up even further: str[_] = ""; text = Array[str, 5] – Mr.Wizard Sep 15 '13 at 19:48
@Mr.Wizard. I take it that your comment is intended to apply only to the case considered int the edit. – m_goldberg Sep 16 '13 at 1:11

Alternatively, you can use the newline command "\n" and have the complete file in a single string. For the example above:

str = "The quick brown fox jumped over the lazy dog\nGrumpy wizards make a toxic brew for the jovial queen\n\n\nThe five boxing wizards jumped quickly";
Export["test.txt", str]

gives the text file

enter image description here

share|improve this answer

Your Answer


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.