You saw an example of a string, "Game Over", in the previous chapter. But strings can become much longer and more complex. You may want to give a user several paragraphs of instructions. Or you might want to format your text in a very specific manner. Using quotes can help you to create strings to accomplish all of this.
Game Over 2.0 improves upon its predecessor program, Game Over, by displaying a more impressive version of the same message, which tells a player that his or her computer game has come to an end. Using single and double quotes, the result is more visually appealing. Check out Figure 2.2 to see a sample run.
Figure 2.2: Now I get it, the game is over.
The code for the program shows that it's pretty simple to present text using quotes in different ways:
# Game Over - Version 2 # Demonstrates the use of quotes in strings # Michael Dawson - 1/9/03 print "Program 'Game Over' 2.0" print \ """ """ raw_input("\n\nPress the enter key to exit.")
You've seen how to create simple strings by surrounding text with quotes. You can use either a pair of single (' ') or double quotes (" ") to create string values. The computer doesn't care. So, 'Game Over' is exactly the same string as "Game Over". But take a look at the first appearance of a string in the program:
print "Program 'Game Over' 2.0"
This statement uses both kinds of quotes. Check out the sample run in Figure 2.2 again. Only the single quotes show up, because they are part of the string, just like, for example, the letter G. But the double quotes are not part of the string. The double quotes are like bookends, telling the computer where the string begins and ends. So, if you use a pair of double quotes to "bookend" your string, you can use as many single quotes inside the string as you want. And, if you surround your string with a pair of single quotes, you can use as many double quotes inside the string as you like.
Once you've used one kind of quote as bookends for your string, you can't use that type of quote inside your string. This make sense, because once the computer sees the second appearance of the quote that began the string, it thinks the string is over. For example, "With the words, 'Houston, we have a problem.', Jim Lovell became one of our most famous astronauts." is a valid string. But, "With the words, "Houston, we have a problem.", Jim Lovell became one of our most famous astronauts." isn't valid, because once the computer sees the second double quote, it thinks the string is over. So, the computer sees the string "With the words," followed by the word, Houston. And since the computer has no idea what Houston is, you get a nasty syntax error.
The next line of code, print \, looks awfully lonely. And it should. It's not a complete statement. Generally, you write one statement per line. But you don't have to. You can stretch a single statement across multiple lines. All you have to do is use the line-continuation character, \ (which is just a backslash). Put it anywhere you'd normally use a space (but not inside a string) to continue your statement on the next line. The computer will act as if it sees one long line of code.
HINT | The computer doesn't care how long a programming line is, but people do. If a line of your code feels too long, or would be more clear as several lines, use the line-continuation character to split it up. |
Certainly the coolest part of the program is where it prints out "Game Over" in a big block of text. The following string is responsible:
""" """
This is what's called a triple-quoted string. It's a string enclosed by a pair of three quotes in a row. Like before, it doesn't matter which kind of quotes you use, as long as you bookend with the same type.
As you can see, triple-quoted strings can span multiple lines. They print on the screen exactly the way you type them. If you ever need to print more than a few lines of text, triple-quoted strings are the way to go.
If you like the letters made from multiple characters in Game Over 2.0, then you'll really like ASCII Art. ASCII Art is basically pictures made from just the characters on your keyboard. ASCII, by the way, stands for the American Standard Code for Information Interchange. It's a code that represents 128 standard characters.
Through ASCII art, you can make simple messages (like I did) or create elaborate pictures (which I can't), all with just the characters on your keyboard. You'd be amazed at some of the things true ASCII artists can do. For some great examples of this art form, check out http://www.chris.com/ascii/.
By the way, this kind of art isn't new, and it didn't start with the computer. The first recorded typewriter art dates back to 1898.