Converting between Strings, Boxes, and Expressions

ToString[expr,form]create a string representing the specified textual form of expr
ToBoxes[expr,form]create boxes representing the specified textual form of expr
ToExpression[input,form]create an expression by interpreting a string or boxes as input in the specified textual form
ToString[expr]create a string using OutputForm
ToBoxes[expr]create boxes using StandardForm
ToExpression[input]create an expression using StandardForm

Converting between strings, boxes, and expressions.

Here is a simple expression.
In[1]:=
Click for copyable input
Out[1]=
This gives the InputForm of the expression as a string.
In[2]:=
Click for copyable input
Out[2]=
In FullForm explicit quotes are shown around the string.
In[3]:=
Click for copyable input
Out[3]//FullForm=
This gives a string representation for the StandardForm boxes that correspond to the expression.
In[4]:=
Click for copyable input
Out[4]//FullForm=
ToBoxes yields the boxes themselves.
In[5]:=
Click for copyable input
Out[5]=

In generating data for files and external programs, it is sometimes necessary to produce two-dimensional forms which use only ordinary keyboard characters. You can do this using OutputForm.

This produces a string which gives a two-dimensional rendering of the expression, using only ordinary keyboard characters.
In[6]:=
Click for copyable input
Out[6]=
The string consists of two lines, separated by an explicit newline.
In[7]:=
Click for copyable input
Out[7]//FullForm=
The string looks right only in a monospaced font.
In[8]:=
Click for copyable input
Out[8]=

If you operate only with one-dimensional structures, you can effectively use ToString to do string manipulation with formatting functions.

This generates a string corresponding to the OutputForm of StringForm.
In[9]:=
Click for copyable input
Out[9]//InputForm=
InputFormstrings corresponding to keyboard input
StandardFormstrings or boxes corresponding to standard two-dimensional input (default)
TraditionalFormstrings or boxes mimicking traditional mathematical notation

Some forms handled by ToExpression.

This creates an expression from an InputForm string.
In[10]:=
Click for copyable input
Out[10]=
This creates the same expression from StandardForm boxes.
In[11]:=
Click for copyable input
Out[11]=
In TraditionalForm these are interpreted as functions.
In[12]:=
Click for copyable input
Out[12]=
ToExpression[input,form,h]create an expression, then wrap it with head h

Creating expressions wrapped with special heads.

This creates an expression, then immediately evaluates it.
In[13]:=
Click for copyable input
Out[13]=
This creates an expression using StandardForm rules, then wraps it in Hold.
In[14]:=
Click for copyable input
Out[14]=
You can get rid of the Hold using ReleaseHold.
In[15]:=
Click for copyable input
Out[15]=
SyntaxQ["string"]determine whether a string represents syntactically correct Mathematica input
SyntaxLength["string"]find out how long a sequence of characters starting at the beginning of a string is syntactically correct

Testing correctness of strings as input.

ToExpression will attempt to interpret any string as Mathematica input. But if you give it a string that does not correspond to syntactically correct input, then it will print a message, and return $Failed.

This is not syntactically correct input, so ToExpression does not convert it to an expression.
In[16]:=
Click for copyable input
Out[16]=
ToExpression requires that the string correspond to a complete Mathematica expression.
In[17]:=
Click for copyable input
Out[17]=

You can use the function SyntaxQ to test whether a particular string corresponds to syntactically correct Mathematica input. If SyntaxQ returns False, you can find out where the error occurred using SyntaxLength. SyntaxLength returns the number of characters which were successfully processed before a syntax error was detected.

SyntaxQ shows that this string does not correspond to syntactically correct Mathematica input.
In[18]:=
Click for copyable input
Out[18]=
SyntaxLength reveals that an error was detected after the third character in the string.
In[19]:=
Click for copyable input
Out[19]=
Here SyntaxLength returns a value greater than the length of the string, indicating that the input was correct so far as it went, but needs to be continued.
In[20]:=
Click for copyable input
Out[20]=
New to Mathematica? Find your learning path »
Have a question? Ask support »