Chapter 2 Letters

Learn to Program, Third Edition — by Chris Pine (15 / 116)

The Pragmatic Programmers
The Pragmatic Programmers

--

👈 Progress Checkpoint | TOC | String Arithmetic 👉

In the previous chapter, you learned about numbers. But what about letters? Words? Text?

In a program, groups of letters are known as strings. (You can picture little beads with letters on them, tied together with a string.) Here are a few strings to get you started:

​ ​"Hello!"​
​ ​"99 Red Balloons"​
​ ​"Snoopy says #%^?&*@! when he stubs his toe."​
​ ​" "​
​ ​""​

Notice that strings can have more than letters — they can include punctuation, digits, symbols, spaces, or nothing at all. The last string in that example is known as an empty string because it contains nothing.

Since you’re already familiar with using puts to print numbers, let’s use puts to print strings:

​1: puts ​"Hello, world!"​ 
​2: puts ​""​
​3: puts ​"Good-bye."​
​<= Hello, world!
​  
​ Good-bye.

Hopefully that’s the output you were expecting. Now let’s look at some more things you can do with strings.

👈 Progress Checkpoint | TOC | String Arithmetic 👉

Learn to Program, Third Edition by Chris Pine can be purchased in other book formats directly from the Pragmatic Programmers. If you notice a code error or formatting mistake, please let us know here so that we can fix it.

--

--

The Pragmatic Programmers
The Pragmatic Programmers

We create timely, practical books and learning resources on classic and cutting-edge topics to help you practice your craft and accelerate your career.