Using Escape Sequences with Strings


Escape sequences allow you to put special characters into your strings. These give you greater control and flexibility over the text you display. The escape sequences you'll work with are made up of two characters: a backslash followed by another character. This may all sound a little mysterious, but once you see a few sequences in action, you'll realize just how easy they are to use.

Introducing the Fancy Credits Program

Besides telling a player that the game is over, a program often displays credits, a list of all the people who worked so hard to make it a reality. Fancy Credits uses escape sequences to achieve some effects it just couldn't without them. Figure 2.3 shows the results.

click to expand
Figure 2.3: Please, contain your applause.

The code looks a bit cryptic at first glance:

 # Fancy Credits # Demonstrates escape sequences # Michael Dawson 1/11/03 # sound the system bell print "\a" print "\t\t\tFancy Credits" print "\t\t\t \\ \\ \\ \\ \\ \\ \\" print "\t\t\t\tby" print "\t\t\tMichael Dawson" print "\t\t\t \\ \\ \\ \\ \\ \\ \\" print "\nSpecial thanks goes out to:" print "My hair stylist, Henry \'The Great\', who never says \"can\'t\"." raw_input("\n\nPress the enter key to exit.") 

But you'll soon understand it all.

Sounding the System Bell

Upon running this program, you'll notice something different right away. It makes noise! The very first statement in the program,

 print "\a" 

sounds the system bell of your computer. It does this through the escape sequence, \a, which represents the system bell character. Every time you print it, the bell rings. You can print a string with just this sequence, as I have, or you can put it inside a longer string. You can even use the sequence several times to ring the bell more than once.

Moving Forward a Tab Stop

Sometimes you'll want to set some text off from the left margin where it normally prints. In a word processor, you could use the Tab key. With strings, you can use the escape sequence for a tab, \t. That's exactly what I did in the following line:

 print "\t\t\tFancy Credits" 

I used the tab escape sequence, \t, three times in a row. So, when the program prints the string, it prints three tabs and then Fancy Credits. This makes Fancy Credits, look nearly centered in the console window. Tab sequences are good for setting off text, as in this program, but they're also perfect for arranging text into columns.

Printing a Backslash

If you've thought ahead, you may be wondering how you can print a backslash if the computer always interprets a backslash as the beginning of an escape sequence. Well, the solution is pretty simple: just use two backslashes in a row. Each of the following two lines prints three tabs, as a result of the three \t sequences:

 print "\t\t\t \\ \\ \\ \\ \\ \\ \\" print "\t\t\t \\ \\ \\ \\ \\ \\ \\" 

Then, each prints exactly eight backslashes, separated by spaces. Go ahead and count. You'll find exactly eight pairs of backslashes, separated by spaces.

Inserting a Newline

The most useful sequence at your disposal is the newline sequence. It's represented by \n. By using this sequence, you can insert a newline character into your strings for a blank line where you need it. Newlines are often used right at the beginning of a string to separate it from the text last printed. That's what I did in the line:

 print "\nSpecial thanks goes out to:" 

The computer sees the \n sequence, prints a blank line, then prints Special thanks goes out to:. This single statement is equivalent to the following two statements:

 print print "Special thanks goes out to:" 

Inserting a Quote

Inserting a quote into a string, even the type of quote you use to bookend it, is simple. Just use the sequence \' for a single quote and \" for a double quote. They mean "put a quote here", and won't be mistaken by the computer as a marker for the end of your string. This is what I used to get both kinds of quotes in one line of text:

 print "My hair stylist, Henry \'The Great\', who never says \"can\'t\"." 

The pair of double quotes at both ends are the bookends, defining the string. To make the string easier to understand, look at it in parts:

  • \'The Great\' prints as 'The Great'

  • Each \' sequence is printed as a single quote

  • \"can\'t\" prints as "can't"

  • Both \" sequences print as double quotes

  • The lone \' sequence prints as a single quote

As you can see, escape sequences aren't so bad once you've seen them in action. And they can come in quite handy. Table 2.1 summarizes some useful ones.

Table 2.1: SELECTED ESCAPE SEQUENCES

Sequence

Description

\\

Backslash. Prints one backslash.

\'

Single quote. Prints a single quote.

\"

Double quote. Prints a double quote.

\a

Bell. Sounds the system bell.

\b

Backspace. Moves cursor back one space.

\n

Newline. Moves cursor to beginning of next line.

\t

Horizontal tab. Moves cursor forward one tab stop.

TRAP

A few escape sequences only work as advertised if you run your program directly from the operating system and not through IDLE. The escape sequences \a and \b are good examples. Let's say I have a program that simply prints the escape sequence \a. If I run it through IDLE, I get a little square box printed on my screen—not what I wanted. But if I run that same program directly from Windows, by double-clicking the program file icon, my computer's system bell rings just as I intended.




Python Programming for the Absolute Beginner
Python Programming for the Absolute Beginner, 3rd Edition
ISBN: 1435455002
EAN: 2147483647
Year: 2003
Pages: 194

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net