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()


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.

  • $\begingroup$ Interesting. I did not know that ToString acts like this for InputForm. +1. $\endgroup$ – Leonid Shifrin Mar 22 '12 at 18:39
  • $\begingroup$ @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. $\endgroup$ – Szabolcs Mar 22 '12 at 19:14
  • $\begingroup$ 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. $\endgroup$ – Leonid Shifrin Mar 22 '12 at 19:26
  • 1
    $\begingroup$ @Szabolcs I added the //InputForm to show the full structure of the string, and to make it clear that something really did change. $\endgroup$ – Brett Champion Mar 22 '12 at 20:22
  • $\begingroup$ @Brett Thanks for the edit! $\endgroup$ – 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.

  • $\begingroup$ 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 ... $\endgroup$ – Szabolcs Mar 22 '12 at 19:13
  • $\begingroup$ 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. $\endgroup$ – Jens Mar 22 '12 at 19:17
  • $\begingroup$ 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. $\endgroup$ – Szabolcs Mar 22 '12 at 19:27
  • $\begingroup$ @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. $\endgroup$ – Jens Mar 23 '12 at 0:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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