1.4. Compute with Words¶
The computer can also compute with words, or more accurately, with strings which are sequences of characters. We can create a string by typing characters between a pair of single (
'Hi'), double (
"Hi"), or triple quotes (‘’’Hi’’’). We can “compute” with strings using some of the same basic arithmetic operators – they just mean something different here. Here we generate silly song lyrics by using
+ to combine (append) two strings and
* to repeat strings.
Underneath the program below, to the right of the Run button , you’ll see the button to open the audio tour for this program: . Click on that button and then click on “Line-by-line Tour” to hear the audio tour. You can use the provided buttons to pause, play, jump ahead, or go back.
A string can also be asked to return a new string that is changed in some way from the original string. In the example below, we’ll take a string in all-caps and turn it into a nicely capitalized sentence. This example uses dot-notation (
sentence.lower()) which is the way to tell a string how we want it to change.
Discuss topics in this section with classmates.