Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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 have a string variable. I want to obtain another string which contains the representation of the string variable content itself.

s = "a \n b"

I need to get a string containing the exact characters from above, including the escape character \ and the ":

r = "\"a \\n b\""

Is there a function that can do that?

The output should be similar to repr("a \n b") from Python:

This function makes an attempt to return a string that would yield an object with the same value when passed to eval()

share|improve this question
up vote 10 down vote accepted

Just use ToString

ToString["a\nb", InputForm] // InputForm

Adding // InputForm is not strictly necessary. It will influence only the printing of the output, not its structure. By default, Mathematica does not show quotes around strings in the output, and it expands escaped characters (such as \n) to their meaning (an actual newline in this case). InputForm will change this behaviour and will cause Mathematica to show quotes and escaped characters.

share|improve this answer
Interesting. I did not know that ToString acts like this for InputForm. +1. – Leonid Shifrin Mar 22 '12 at 18:39
@Leonid ToString is the function to convert a Mathematica expression to a string prepresentation. Otherwise one could use ExportString[..., "Package"] to achieve a similar effect. – Szabolcs Mar 22 '12 at 19:14
I am aware of the general purpose of ToString. But I rarely use the second argument of ToString, and in particular I did not know that it acts in this way when you set it to InputForm. I did need this functionality a few times as well, particularly here, where I used some half-baked and inferior custom version. – Leonid Shifrin Mar 22 '12 at 19:26
@Szabolcs I added the //InputForm to show the full structure of the string, and to make it clear that something really did change. – Brett Champion Mar 22 '12 at 20:22
@Brett Thanks for the edit! – Szabolcs Mar 22 '12 at 20:28

Simply do this:

r = ToBoxes[s]

and you're done. In case you need additional escape levels, just iterate ToBoxes.

share|improve this answer
ToString["a\nb", InputForm] (my answer) already returns the requested result. Don't be fooled by the quotes not being printed, they're there even if the system doesn't print them. Just start editing the output cell ... – Szabolcs Mar 22 '12 at 19:13
That's what I mean: do I understand what he wants? I added another escape level, as you can see with "Show Expression" or by exporting as text... maybe that's too much escaping, I'm not sure. – Jens Mar 22 '12 at 19:17
My concern about your solution is that applying ToExpression to it does not give the same expression we started with (it throws an error). It's possible to influence the printing of the output like this: ToString["a\nb", InputForm] // InputForm. This will show the quotes and the escapes without changing the meaning of the expression. ToExpression[%] will return the string we started with. – Szabolcs Mar 22 '12 at 19:27
@Szabolc OK, the question was much simpler than how I interpreted it. In this case, my answer can of course be simplified, too. So I'll do that now. – Jens Mar 23 '12 at 0:49

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.