An Elementary Introduction to the Wolfram Language
11Strings and Text
Enter a string:
In[1]:=
Click for copyable input
Out[1]=
StringLength counts the number of characters in a string:
In[2]:=
Click for copyable input
Out[2]=
StringReverse reverses the characters in a string:
In[3]:=
Click for copyable input
Out[3]=
ToUpperCase makes all the characters in a string uppercase (capital letters):
In[4]:=
Click for copyable input
Out[4]=
StringTake takes a certain number of characters from the beginning of a string:
In[5]:=
Click for copyable input
Out[5]=
If you take 10 characters, you get a string of length 10:
In[6]:=
Click for copyable input
Out[6]=
StringJoin joins strings (dont forget spaces if you want to separate words):
In[7]:=
Click for copyable input
Out[7]=
You can make lists of strings, then apply functions to them.
A list of strings:
In[8]:=
Click for copyable input
Out[8]=
In[9]:=
Click for copyable input
Out[9]=
StringJoin joins the strings in a list:
In[10]:=
Click for copyable input
Out[10]=
Characters breaks a string into a list of its characters:
In[11]:=
Click for copyable input
Out[11]=
Sort the characters in a string:
In[12]:=
Click for copyable input
Out[12]=
InputForm shows strings as you would input them, including quotes:
In[13]:=
Click for copyable input
Out[13]=
Functions like StringJoin and Characters work on strings of any kind; it doesnt matter if theyre meaningful text or not. There are other functions, like TextWords, that specifically work on meaningful text, written, say, in English.
TextWords gives a list of the words in a string of text:
In[14]:=
Click for copyable input
Out[14]=
This gives the length of each word:
In[15]:=
Click for copyable input
Out[15]=
TextSentences breaks a text string into a list of sentences:
In[16]:=
Click for copyable input
Out[16]=
Get the first 100 characters of the Wikipedia article about computers:
In[17]:=
Click for copyable input
Out[17]=
Create a word cloud for the Wikipedia article on computers:
In[18]:=
Click for copyable input
Out[18]=
Get the first 20 words from a list of common English words:
In[19]:=
Click for copyable input
Out[19]=
In[20]:=
Click for copyable input
Out[20]=
Generate the Roman numeral string for 1988:
In[21]:=
Click for copyable input
Out[21]=
Make a table of the Roman numerals for numbers up to 20:
In[22]:=
Click for copyable input
Out[22]=
As with everything, we can do computations on these strings. For example, we can plot the lengths of successive Roman numerals.
In[23]:=
Click for copyable input
Out[23]=
IntegerName gives the English name of an integer.
Generate a string giving the name of the integer 56:
In[24]:=
Click for copyable input
Out[24]=
In[25]:=
Click for copyable input
Out[25]=
Alphabet gives the alphabet:
In[26]:=
Click for copyable input
Out[26]=
LetterNumber tells you where in the alphabet a letter appears:
In[27]:=
Click for copyable input
Out[27]=
FromLetterNumber does the opposite:
In[28]:=
Click for copyable input
Out[28]=
Alphabet knows about non-English alphabets too:
In[29]:=
Click for copyable input
Out[29]=
Transliterate converts to (approximately) equivalent English letters:
In[30]:=
Click for copyable input
Out[30]=
This transliterates the word wolfram into the Russian alphabet:
In[31]:=
Click for copyable input
Out[31]=
In[32]:=
Click for copyable input
Out[32]=
In[33]:=
Click for copyable input
Out[33]=
11.1Join two copies of the string "Hello"»
Expected output:
Out[]=
11.2Make a single string of the whole alphabet, in uppercase. »
Expected output:
Out[]=
11.3Generate a string of the alphabet in reverse order. »
Expected output:
Out[]=
Expected output:
Out[]=
11.5Use StringTake, StringJoin and Alphabet to get "abcdef"»
Expected output:
Out[]=
11.6Create a column with increasing numbers of letters from the string "this is about strings"»
Expected output:
Out[]=
11.7Make a bar chart of the lengths of the words in A long time ago, in a galaxy far, far away»
Expected output:
Out[]=
11.8Find the string length of the Wikipedia article for computer»
Sample expected output:
Out[]=
11.9Find how many words are in the Wikipedia article for computer»
Sample expected output:
Out[]=
11.10Find the first sentence in the Wikipedia article about strings»
Sample expected output:
Out[]=
11.11Make a string from the first letters of all sentences in the Wikipedia article about computers. »
Sample expected output:
Out[]=
11.12Find the maximum word length among English words from WordList[]»
Sample expected output:
Out[]=
11.13Count the number of words in WordList[ ] that start with q»
Sample expected output:
Out[]=
11.14Make a line plot of the lengths of the first 1000 words from WordList[]»
Sample expected output:
Out[]=
11.15Use StringJoin and Characters to make a word cloud of all letters in the words from WordList[]»
Sample expected output:
Out[]=
11.16Use StringReverse to make a word cloud of the last letters in the words from WordList[]»
Sample expected output:
Out[]=
11.17Find the Roman numerals for the year 1959. »
Expected output:
Out[]=
11.18Find the maximum string length of any Roman-numeral year from 1 to 2020. »
Expected output:
Out[]=
11.19Make a word cloud from the first characters of the Roman numerals up to 100. »
Expected output:
Out[]=
11.20Use Length to find the length of the Russian alphabet. »
Expected output:
Out[]=
Expected output:
Out[]=
11.22Make a bar chart of the letter numbers in wolfram»
Expected output:
Out[]=
11.23Use FromLetterNumber to make a string of 1000 random letters. »
Sample expected output:
Out[]=
11.24Make a list of 100 random 5-letter strings. »
Sample expected output:
Out[]=
11.25Transliterate wolfram into Greek. »
Expected output:
Out[]=
11.26Get the Arabic alphabet and transliterate it into English. »
Expected output:
Out[]=
11.27Make a white-on-black size-200 letter A»
Expected output:
Out[]=
11.28Use Manipulate to make an interactive selector of size-100 characters from the alphabet, controlled by a slider. »
Expected output:
Out[]=
11.29Use Manipulate to make an interactive selector of black-on-white outlines of rasterized size-100 characters from the alphabet, controlled by a menu. »
Expected output:
Out[]=
11.30Use Manipulate to create a vision simulator that blurs a size-200 letter A by an amount from 0 to 50. »
Expected output:
Out[]=
+11.1Generate a string of the alphabet followed by the alphabet written in reverse. »
Expected output:
Out[]=
+11.2Make a column of a string of the alphabet and its reverse. »
Expected output:
Out[]=
+11.3Find how many sentences are in the Wikipedia article for computer»
Sample expected output:
Out[]=
+11.4Join together without spaces, etc. the words in the first sentence in the Wikipedia article for strings»
Sample expected output:
Out[]=
+11.5Find the length of the longest word in the Wikipedia article about computers. »
Sample expected output:
Out[]=
+11.6Plot the lengths of Roman numerals for numbers up to 2000. »
Sample expected output:
Out[]=
+11.7Generate a string by joining the Roman numerals up to 100. »
Expected output:
Out[]=
+11.8Make a line plot of the successive letter numbers for the concatenation of all Roman numerals up to 30. »
Expected output:
Out[]=
+11.9Find the maximum string length of the name of any integer up to 1000. »
Expected output:
Out[]=
+11.10Make a list of uppercase size-20 letters of the alphabet in random colors. »
Sample expected output:
Out[]=
+11.11Make a list of 100 random 5-letter strings with the Russian alphabet. »
Sample expected output:
Out[]=
+11.12Create a Manipulate to display edges in size-200 letter A, blurred from 0 to 50. »
Expected output:
Out[]=
+11.13Add together white-on-black size-200 letters A and B. »
Expected output:
Out[]=
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. Well talk much more about symbols later.
How do I enter characters that arent on my keyboard?
Use \" (and if you want to put \" literally in the string, use \\ \"). (Youll use a lot of backslashes if you want to put \\ \" in: \\ \\ \\ \".)
How are the colors of elements in word clouds determined?
By default its 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.
LetterNumber["α", "Greek"] gives numbering in the Greek alphabet. All characters are assigned a character code. You can find it using ToCharacterCode.
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, its sometimes tricky to decide whats in the alphabet, and whats just derived from it.
Can I translate words instead of just transliterating their letters?
Yes. Use WordList[Language"Spanish"], etc.
 
Download Notebook Version