String-Oriented Output Formats

Text strings.

The quotes are not included in standard Wolfram System output form:

"This is a string."

In input form, the quotes are included:

InputForm[%]

You can put any kind of text into a Wolfram Language string. This includes non-English characters, as well as newlines and other control information. "Strings and Characters" discusses in more detail how strings work.

Using format strings.

In many situations, you may want to generate output using a string as a "template", but "splicing" in various Wolfram Language expressions. You can do this using (RefLink[StringForm, paclet:ref/StringForm]).

This generates output with each successive `` replaced by an expression:

StringForm["x = ``, y = ``",3,(1+u)^2]

You can use numbers to pick out expressions in any order:

StringForm["{`1`, `2`, `1`}",a,b]

The string in (RefLink[StringForm, paclet:ref/StringForm]) acts somewhat like a "format directive" in the formatted output statements of languages such as C and Fortran. You can determine how the expressions in (RefLink[StringForm, paclet:ref/StringForm]) will be formatted by wrapping them with standard output format functions.

You can specify how the expressions in (RefLink[StringForm, paclet:ref/StringForm]) are formatted using standard output format functions:

StringForm["The `` of `` is ``.",TeXForm,a/b,TeXForm[a/b]]

You should realize that (RefLink[StringForm, paclet:ref/StringForm]) is only an output format. It does not evaluate in any way. You can use the function (RefLink[ToString, paclet:ref/ToString]) to create an ordinary string from a (RefLink[StringForm, paclet:ref/StringForm]) object.

(RefLink[StringForm, paclet:ref/StringForm]) generates formatted output in standard Wolfram System output form:

StringForm["Q: `` -> ``",a,b]

In input form, you can see the actual (RefLink[StringForm, paclet:ref/StringForm]) object:

InputForm[%]

This creates an ordinary string from the (RefLink[StringForm, paclet:ref/StringForm]) object:

InputForm[ToString[%]]

(RefLink[StringForm, paclet:ref/StringForm]) allows you to specify a "template string", then fill in various expressions. Sometimes all you want to do is to concatenate together the output forms for a sequence of expressions. You can do this using (RefLink[Row, paclet:ref/Row]).

Output of sequences of expressions.

(RefLink[Row, paclet:ref/Row]) prints as a sequence of expressions concatenated together:

Row[{"[x = ",56,"]"}]

(RefLink[Row, paclet:ref/Row]) also works with typeset expressions:

Row[{"[y = ",Subscript[a,b],"]"}]

(RefLink[Row, paclet:ref/Row]) can automatically insert any expression between the displayed elements:

Row[{a, b, c, d}, "⟷"]

(RefLink[Spacer, paclet:ref/Spacer]) can be used to control the precise spacing between elements:

Row[{"x",Spacer[10],"y"}]

Output of columns of expressions.

This arranges the two expressions in a column:

Column[{a+b,x^2}]

Output of unevaluated expressions.

Using text strings and functions like (RefLink[Row, paclet:ref/Row]), you can generate pieces of output that do not necessarily correspond to valid Wolfram Language expressions. Sometimes, however, you want to generate output that corresponds to a valid Wolfram Language expression, but only so long as the expression is not evaluated. The function (RefLink[Defer, paclet:ref/Defer]) maintains its argument unevaluated, but allows it to be formatted in the standard Wolfram System output form.

(RefLink[Defer, paclet:ref/Defer]) maintains 1+1 unevaluated:

Defer[1+1]

The (RefLink[Defer, paclet:ref/Defer]) prevents the actual assignment from being done:

Defer[x=3]

When the output of (RefLink[Defer, paclet:ref/Defer]) is evaluated again, which might happen by modifying the output or by using copy and paste, it will evaluate normally.

The following output was copied from the previous output cell into an input cell:

x=3

It is also possible to produce output whose appearance has no direct correlation to how it evaluates by using (RefLink[Interpretation, paclet:ref/Interpretation]). This method is effectively used by the Wolfram System when formatting some kinds of outputs where the most readable form does not correspond well to the internal representation of the object. For example, (RefLink[Series, paclet:ref/Series]) always generates an (RefLink[Interpretation, paclet:ref/Interpretation]) object in its default output.

Although this output displays as y, it will evaluate as x:

Interpretation[y,x]

Copying and pasting the previous output will reference the value earlier assigned to x:

2 y