Wolfram Computation Meets Knowledge

The Wolfram Language:
Fast Introduction for Programmers

Show additional notes for:
All sections 
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]:=1
"this is a string"
Out[1]=1

In[2]:=2
InputForm[%]
Out[2]=2

<> joins strings:

In[3]:=3
"this" <> "is" <> "aaaa" <> "bbb"
Out[3]=3

Notes for Java programmers:

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

Notes for Python programmers:

<> 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 .

Notes for Python programmers:

Both Python 3 and Wolfram Language strings are Unicode by default. In Python 2, strings are ASCII by default, unless explicitly defined as Unicode.


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

In[1]:=1
StringReplace["this is a string", Shortest[LetterCharacter ... ~~ "is"] -> "xxx"]
Out[1]=1

In patterns p... indicates zero or more repetitions of p.
RegularExpression allows compact regex notation.

Notes for Java programmers:

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.

Notes for Python programmers:

Wolfram Language string patterns offer powerful string matching using a general pattern syntax, optimized for readability and intuitiveness. The closest analog in Python is string matching with regex, which works similarly to the Wolfram Language's RegularExpression function.


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

In[1]:=1
TemplateApply["number `` date: <* Now *>", {56}]
Out[1]=1

Notes for Java programmers:

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 <* ... *>.

Notes for Python programmers:

Python's % or format function works similarly to TemplateApply in the Wolfram Language, except inline computations using <* ... *> are not supported in Python.

QUICK REFERENCE: Operations on Strings


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


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


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

© 2017 Wolfram. All rights reserved.