# How to get string representation (like repr in Python)

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

"\"a\\nb\""


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.

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

-
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