Another thing the Wolfram Language lets you compute with is text. You enter text as a string, indicated by quotes (").

"This is a string."

Just like when you enter a number, a string on its own comes back unchanged—except that the quotes aren’t visible when the string is displayed. There are many functions that work on strings. Like StringLength, which gives the length of a string.

StringLength["hello"]

StringReverse["hello"]

ToUpperCase["I'm coding in the Wolfram Language!"]

StringTake["this is about strings",10]

StringLength[StringTake["this is about strings",10]]

StringJoin["Hello"," ", "there!", " How are you?"]

You can make lists of strings, then apply functions to them.

{"apple","banana","strawberry"}

StringTake[{"apple","banana","strawberry"},2]

StringJoin[{"apple","banana","strawberry"}]

Sometimes it’s useful to turn strings into lists of their constituent characters. Each character is actually a string itself, of length 1.

Characters["a string is made of characters"]

Once you’ve broken a string into a list of characters, you can use all the usual list functions on it.

Sort[Characters["a string of characters"]]

The invisible elements at the beginning of the list are space characters. If you want to see strings in the form you’d input them, complete with "...", use InputForm.

InputForm[Sort[Characters["a string of characters"]]]

Functions like StringJoin and Characters work on strings of any kind; it doesn’t matter if they’re meaningful text or not. There are other functions, like TextWords, that specifically work on meaningful text, written, say, in English.

TextWords["This is a sentence. Sentences are made of words."]

StringLength[TextWords["This is a sentence. Sentences are made of words."]]

TextSentences["This is a sentence. Sentences are made of words."]

There are lots of ways to get text into the Wolfram Language. One example is the WikipediaData function, which gets the current text of Wikipedia articles.

StringTake[WikipediaData["computers"],100]

A convenient way to get a sense of what’s in a piece of text is to create a word cloud. The function WordCloud does this.

WordCloud[WikipediaData["computers"]]

Not surprisingly, “computer” and “computers” are the most common words in the article.

The Wolfram Language has lots of built-in knowledge about words that appear in English and other languages. WordList gives lists of words.

Take[WordList[ ],20]

WordCloud[StringTake[WordList[ ],1]]

Strings don’t have to contain text. In a juxtaposition of ancient and modern, we can for example generate Roman numerals as strings.

RomanNumeral[1988]

Table[RomanNumeral[n],{n,20}]

As with everything, we can do computations on these strings. For example, we can plot the lengths of successive Roman numerals.

ListLinePlot[Table[StringLength[RomanNumeral[n]],{n,100}]]

IntegerName gives the English name of an integer.

IntegerName[56]

ListLinePlot[Table[StringLength[IntegerName[n]],{n,100}]]

There are various ways to turn letters into numbers (and vice versa).

Alphabet[ ]

LetterNumber[{"a","b","x","y","z"}]

FromLetterNumber[{10,11,12,13,14,15}]

Alphabet["Russian"]

Transliterate[Alphabet["Russian"]]

Transliterate["wolfram","Russian"]

If you want to, you can also turn text into images, which you can then manipulate using image processing. The function Rasterize makes a raster, or bitmap, of something.

Rasterize[Style["ABC",100]]

EdgeDetect[Rasterize[Style["ABC",100]]]

Join two copies of the string "Hello". »

Make a single string of the whole alphabet, in uppercase. »

Generate a string of the alphabet in reverse order. »

Join 100 copies of the string "AGCT". »

Use StringTake, StringJoin and Alphabet to get "abcdef". »

Create a column with increasing numbers of letters from the string "this is about strings". »

Make a bar chart of the lengths of the words in “A long time ago, in a galaxy far, far away”. »

Find the string length of the Wikipedia article for “computer”. »

Find how many words are in the Wikipedia article for “computer”. »

Find the first sentence in the Wikipedia article about “strings”. »

Make a string from the first letters of all sentences in the Wikipedia article about computers. »

Find the maximum word length among English words from WordList[ ]. »

Count the number of words in WordList[ ] that start with “q”. »

Make a line plot of the lengths of the first 1000 words from WordList[ ]. »

Use StringJoin and Characters to make a word cloud of all letters in the words from WordList[ ]. »

Use StringReverse to make a word cloud of the last letters in the words from WordList[ ]. »

Find the Roman numerals for the year 1959. »

Find the maximum string length of any Roman-numeral year from 1 to 2020. »

Make a word cloud from the first characters of the Roman numerals up to 100. »

Use Length to find the length of the Russian alphabet. »

Generate the uppercase Greek alphabet. »

Make a bar chart of the letter numbers in “wolfram”. »

Use FromLetterNumber to make a string of 1000 random letters. »

Make a list of 100 random 5-letter strings. »

Transliterate “wolfram” into Greek. »

Get the Arabic alphabet and transliterate it into English. »

Make a white-on-black size-200 letter “A”. »

Use Manipulate to make an interactive selector of size-100 characters from the alphabet, controlled by a slider. »

Use Manipulate to make an interactive selector of black-on-white outlines of rasterized size-100 characters from the alphabet, controlled by a menu. »

Use Manipulate to create a “vision simulator” that blurs a size-200 letter “A” by an amount from 0 to 50. »

Generate a string of the alphabet followed by the alphabet written in reverse. »

Make a column of a string of the alphabet and its reverse. »

Find how many sentences are in the Wikipedia article for “computer”. »

Join together without spaces, etc. the words in the first sentence in the Wikipedia article for “strings”. »

Find the length of the longest word in the Wikipedia article about computers. »

Plot the lengths of Roman numerals for numbers up to 2000. »

Generate a string by joining the Roman numerals up to 100. »

Make a line plot of the successive letter numbers for the concatenation of all Roman numerals up to 30. »

Find the maximum string length of the name of any integer up to 1000. »

Make a list of uppercase size-20 letters of the alphabet in random colors. »

Make a list of 100 random 5-letter strings with the Russian alphabet. »

Create a Manipulate to display edges in size-200 letter A, blurred from 0 to 50. »

Add together white-on-black size-200 letters A and B. »

What is the difference between "x" and x?

"x" is a string; x is a Wolfram Language symbol, just like Plus or Max, that can be defined to actually do computations. We’ll talk much more about symbols later.

How do I enter characters that aren’t on my keyboard?

You can use whatever methods your computer provides, or you can do it directly with the Wolfram Language using constructs such as \ [Alpha].

How do I put quotes (") inside a string?

Use \" (and if you want to put \" literally in the string, use \\\"). (You’ll use a lot of backslashes if you want to put \\\" in: \\\\\\\".)

How are the colors of elements in word clouds determined?

By default it’s random within a certain color palette. You can specify it if you want to.

How come the word cloud shows “s” as the most common letter?

Because it is the most common first letter for common words in English. If you look at all letters, the most common is “e”.

What about letters that aren’t English? How are they numbered?

LetterNumber["α","Greek"] gives numbering in the Greek alphabet. All characters are assigned a character code. You can find it using ToCharacterCode.

What alphabets does the Wolfram Language know about?

Basically all the ones that are used today. Try “Greek” or “Arabic”, or the name of a language. Note that when a language uses accented characters, it’s sometimes tricky to decide what’s “in” the alphabet, and what’s just derived from it.

Can I translate words instead of just transliterating their letters?

Yes. Use WordTranslation. See Section 35.

Can I get lists of common words in languages other than English?

Yes. Use WordList[Language->"Spanish"], etc.

Tech Notes