MATHEMATICA TUTORIAL

Properties of Strings

Much of what Mathematica does revolves around manipulating structured expressions. But you can also use Mathematica as a system for handling unstructured strings of text.

"text"a string containing arbitrary text

Text strings.

When you input a string of text to Mathematica you must always enclose it in quotes. However, when Mathematica outputs the string it usually does not explicitly show the quotes.

You can see the quotes by asking for the input form of the string. In addition, in a Mathematica notebook, quotes will typically appear automatically as soon as you start to edit a string.

When Mathematica outputs a string, it usually does not explicitly show the quotes.
In[1]:=
Click for copyable input
Out[1]=
You can see the quotes, however, by asking for the input form of the string.
In[2]:=
Click for copyable input
Out[2]//InputForm=

The fact that Mathematica does not usually show explicit quotes around strings makes it possible for you to use strings to specify quite directly the textual output you want.

The strings are printed out here without explicit quotes.

You should understand, however, that even though the string often appears as in output, it is still a quite different object from the symbol .

The string is not the same as the symbol .
In[4]:=
Click for copyable input
Out[4]=

You can test whether any particular expression is a string by looking at its head. The head of any string is always String.

All strings have head String.
In[5]:=
Click for copyable input
Out[5]=
The pattern matches any string.
In[6]:=
Click for copyable input
Out[6]=

You can use strings just like other expressions as elements of patterns and transformations. Note, however, that you cannot assign values directly to strings.

This gives a definition for an expression that involves a string.
In[7]:=
Click for copyable input
Out[7]=
This replaces each occurrence of the string by the symbol .
In[8]:=
Click for copyable input
Out[8]=
New to Mathematica? Find your learning path »
Have a question? Ask support »