STRING THEORY

Let’s talk about "strings." When we talk about strings in programming, we’re generally just talking about text. For example, in the very first project, we printed out the text 'Hello, World!'  In this example, 'Hello, World!' is a string.

In fact, a string is any combination of letters, numbers and symbols that is used as text, and we typically indicate something is a string by putting quotation marks (') around it. Both single quotes (') and double quotes (") work just as well, as long as you are you use the same type of quotes on both ends of your string. Here are some examples of strings:

'What is brown and sticky? A stick!'

"What happens to a frog's car when it breaks down?"

'It gets toad away.'

'4 score and 7 years ago.'

'1+1'

Two important notes about these strings:

  1. The second string above is surrounded by double quotes (") instead of single quotes ('). This wasn't just for fun. Double quotes are required because the string already contains a single quote (the apostrophe in “frog's”). To the computer, an apostrophe and a single quote are exactly the same thing! If single quotes were used instead of double quotes, the program would think the string ended after the word “frog”, which obviously is not correct.

  2. You might look at that last string ('1+1') and think to yourself, "That is the same as 2." While you’re obviously correct that 1+1 = 2, when the computer sees the quotation marks around that string, it just thinks of it as a series of three characters put together. If you're wondering how to get the computer to print the value of the calculation, check out CALCULATIONS.

Step #1: Enter Your Code in The Code Window

Starting with a blank Code Window (you can create a new file, if needed), enter the following lines:


Step #2: Run Your Code

To run the program, click on the Play icon at the top of the Code Window.

Step #3: Verify the Output of Your Code

If you’ve done everything correctly, you should see the following in your Output Window:


What is brown and sticky? A stick!
What happens to a frog's car when it breaks down?
It gets toad away.
4 score and 7 years ago.
1+1