Next: Reformatting expressions
Up: Construction
Previous: Output formats
In[4]:= str = ToString[expr]; In[5]:= FullForm[str] Out[5]//FullForm= "1 + Sin(x)**2"A string is much more flexible for formatting purposes than an expression. For example, long expressions can be broken into multiple lines at any point, without regard to syntax. lhs variables, array indices and assignment operators (=), can be efficiently attached using StringJoin, in order to convert an expression into an assignment. There are also many built-in string manipulation tools available.
In[6]:= ToLowerCase[str] Out[6]= 1 + sin(x)**2When print forms of expressions are converted to strings,the ability to manipulate the evaluated forms (as was possible in section 3.1) is forfeited. The ability to recover expressions using the expression parser ToExpression can also be lost. In the following example the expression no longer evaluates to Power[Sin[x],2].
In[7]:= ToExpression[str] //FullForm Out[7]//FullForm= Plus[1, Times[Sin, NonCommutativeMultiply[x, 2]]]In this way the behaviour of Mathematica's print form wrappers has not been maintained. This is considered a price worth paying in order to provide improved formatting tools.