Wolfram Language Fast Introduction for Programmers
Get Started »

Strings

Strings in the Wolfram Language are indicated by double quotes ( "..." ).

The quotes are not included when strings are output, unless you use InputForm:

In[1]:=
X
Out[1]=
In[2]:=
X
Out[2]=

<> joins strings:

In[3]:=
X
Out[3]=

<> is specifically reserved for joining strings in the Wolfram Language, avoiding the ambiguity of using + for this purpose.

Strings can contain any Unicode characters, entered using names like \[Alpha], shortcuts like

ESC a ESC, explicit Unicode like \:03b1—or entered from a palette button .

Python 3 strings are Unicode by default as well. In Python 2, strings are ASCII by default unless explicitly defined as Unicode.

String patterns use ~~ to combine strings with pattern constructs:

In[1]:=
X
Out[1]=

In patterns p... indicates zero or more repetitions of p.

RegularExpression allows compact regex notation.

String patterns offer powerful string matching using the Wolfram Language pattern syntax. You can also string match using the Wolfram Language's regular expressions, with a notation very similar to Java's regex support.

String patterns offer powerful string matching using the Wolfram Language's general pattern syntax. You can also match strings using RegularExpression, with a notation very similar to Java's regex.

String templates use `` to indicate slots and <* ... *> to indicate expressions to evaluate:

In[1]:=
X
Out[1]=

TemplateApply performs a similar function to Java's System.out.printf method, but the Wolfram Language uses `` instead of %, doesn't require a type specifier and allows inline computations using <* ... *>.

TemplateApply works similar to Python's % or .format function and the functionality provided in the string package, although an equivalent syntax for the <* *> operator is not present in Python.

QUICK REFERENCE: Operations on Strings »

Check Your Understanding

Which of the following joins together two strings to form another string?

"this is a " + "string"

Incorrect. Other languages might use this syntax, but not the Wolfram Language.

"this is a " ~~ "string"

Incorrect. The ~~ is for string patterns.

"this is a " <> "string"

Correct.

Which of the following string replacements turns "lead" into "gold"?

StringReplace["lead", {"l" "g", "e" "o", "a" "l"}]

Correct.

StringReplace["lead", "gold"]

Incorrect. The second argument should not be just "gold" but a replacement rule or list of rules, like "lead" "gold".

StringReplace["lead", _ "gold"]

This replaces each letter separately, resulting in goldgoldgoldgold, instead of making just one replacement of the whole string.

Which one uses a template to create a string with the value of x at the current time, labeled by the variable label?

TemplateApply["label `` date: <*Now*> value: <*x*>", label]

Correct.

TemplateApply["label `` date: Now value: x", label]

Incorrect. The expressions to be evaluated should be inside of "<*...*>"

TemplateApply["label '' date: <*Now*> value: <*x*>", label]

Incorrect. The slots are indicated by ``, not ''.