MATHEMATICA TUTORIAL

# 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.
 Out[1]=
This gives the InputForm of the expression as a string.
 Out[2]=
In FullForm explicit quotes are shown around the string.
 Out[3]//FullForm=
This gives a string representation for the StandardForm boxes that correspond to the expression.
 Out[4]//FullForm=
ToBoxes yields the boxes themselves.
 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.
 Out[6]=
The string consists of two lines, separated by an explicit newline.
 Out[7]//FullForm=
The string looks right only in a monospaced font.
 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.
 Out[9]//InputForm=
 InputForm strings corresponding to keyboard input StandardForm strings or boxes corresponding to standard two-dimensional input (default) TraditionalForm strings or boxes mimicking traditional mathematical notation

Some forms handled by ToExpression.

This creates an expression from an InputForm string.
 Out[10]=
This creates the same expression from StandardForm boxes.
 Out[11]=
In TraditionalForm these are interpreted as functions.
 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.
 Out[13]=
This creates an expression using StandardForm rules, then wraps it in Hold.
 Out[14]=
You can get rid of the Hold using ReleaseHold.
 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.
 Out[16]=
ToExpression requires that the string correspond to a complete Mathematica expression.
 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.
 Out[18]=
SyntaxLength reveals that an error was detected after the third character in the string.
 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.
 Out[20]=

## Tutorial CollectionTutorial Collection

New to Mathematica? Find your learning path »
Have a question? Ask support »