Beginning Python - From Novice to Professional
Beginning Python - From Novice to Professional Beginning Python - From Novice to Professional
CHAPTER 1 ■ INSTANT HACKING: THE BASICS 27 Long Strings If you want to write a really long string, one that spans several lines, you can use triple quotes instead of ordinary quotes: print '''This is a very long string. It continues here. And it's not over yet. "Hello, world!" Still here.''' You can also use triple double quotes, """like this""". Note that because of the distinctive enclosing quotes, both single and double quotes are allowed inside, without being backslashescaped. ■Tip Ordinary strings can also span several lines. If the last character on a line is a backslash, the line break itself is “escaped,” and is ignored. For example: print "Hello, \ world!" would print out Hello, world!. The same goes for expressions and statements in general: >>> 1 + 2 + \ 4 + 5 12 >>> print \ 'Hello, world' Hello, world Raw Strings Raw strings aren’t too picky about backslashes, which can be very useful sometimes. 10 In ordinary strings, the backslash has a special role: It escapes things, letting you put things into your string that you couldn’t normally write directly. For example, a new line is written \n, and can be put into a string like this: >>> print 'Hello,\nworld!' Hello, world! This is normally just dandy, but in some cases it’s not what you want. What if you wanted the string to include a backslash followed by an n? You might want to put the DOS pathname C:\nowhere into a string, for example: 10. Especially when writing regular expressions. More about those in Chapter 10.
28 CHAPTER 1 ■ INSTANT HACKING: THE BASICS >>> path = 'C:\nowhere' >>> path 'C:\nowhere' This looks correct, until you print it and discover the flaw: >>> print path C: owhere Not exactly what we were after, is it? So what do we do? We can escape the backslash itself: >>> print 'C:\\nowhere' C:\nowhere This is just fine. But for long paths, you wind up with a lot of backslashes: path = 'C:\\Program Files\\fnord\\foo\\bar\\baz\\frozz\\bozz' Raw strings are useful in such cases. They don’t treat the backslash as a special character at all. Every character you put into a raw string stays the way you wrote it: >>> print r'C:\nowhere' C:\nowhere >>> print r'C:\Program Files\fnord\foo\bar\baz\frozz\bozz' C:\Program Files\fnord\foo\bar\baz\frozz\bozz As you can see, raw strings are written with an r in front. It would seem that you can put almost anything inside a raw string, and that is almost true. Of course, quotes have to be escaped as usual, although that means that you get a backslash in your final string, too: >>> print r'Let\'s go!' Let\'s go! The one thing you can’t have in a raw string is a final backslash. In other words, the last character in a raw string cannot be a backslash. Given the previous example, that ought to be obvious. If the last character (before the final quote) is a backslash, Python won’t know whether to end the string or not: >>> print r"This is illegal\" SyntaxError: invalid token Okay, so it’s reasonable, but what if you want the last character in your raw string to be a backslash? (Perhaps it’s the end of a DOS path, for example.) Well, I’ve given you a whole bag of tricks in this section that should help you solve that problem, but basically you need to put the backslash in a separate string. A simple way of doing that is the following: >>> print r'C:\Program Files\foo\bar' '\\' C:\Program Files\foo\bar\ Note that you can use both single and double quotes with raw strings. Even triple-quoted strings can be raw.
- Page 7 and 8: ■CHAPTER 23 Project 4: In the New
- Page 9 and 10: viii ■CONTENTS Strings . . . . .
- Page 11 and 12: x ■CONTENTS ■CHAPTER 5 Conditio
- Page 13 and 14: xii ■CONTENTS ■CHAPTER 8 Except
- Page 15 and 16: xiv ■CONTENTS ■CHAPTER 11 Files
- Page 17 and 18: xvi ■CONTENTS Dynamic Web Pages w
- Page 19 and 20: xviii ■CONTENTS ■CHAPTER 20 Pro
- Page 21 and 22: xx ■CONTENTS Second Implementatio
- Page 23 and 24: xxii ■CONTENTS Preparations . . .
- Page 26: About the Technical Reviewer ■JER
- Page 30 and 31: Introduction A C program is like a
- Page 32 and 33: CHAPTER 1 ■ ■ ■ Instant Hacki
- Page 34 and 35: CHAPTER 1 ■ INSTANT HACKING: THE
- Page 36 and 37: CHAPTER 1 ■ INSTANT HACKING: THE
- Page 38 and 39: CHAPTER 1 ■ INSTANT HACKING: THE
- Page 40 and 41: CHAPTER 1 ■ INSTANT HACKING: THE
- Page 42 and 43: CHAPTER 1 ■ INSTANT HACKING: THE
- Page 44 and 45: CHAPTER 1 ■ INSTANT HACKING: THE
- Page 46 and 47: CHAPTER 1 ■ INSTANT HACKING: THE
- Page 48 and 49: CHAPTER 1 ■ INSTANT HACKING: THE
- Page 50 and 51: CHAPTER 1 ■ INSTANT HACKING: THE
- Page 52 and 53: CHAPTER 1 ■ INSTANT HACKING: THE
- Page 54 and 55: CHAPTER 1 ■ INSTANT HACKING: THE
- Page 56 and 57: CHAPTER 1 ■ INSTANT HACKING: THE
- Page 60 and 61: CHAPTER 1 ■ INSTANT HACKING: THE
- Page 62 and 63: CHAPTER 2 ■ ■ ■ Lists and Tup
- Page 64 and 65: CHAPTER 2 ■ LISTS AND TUPLES 33 I
- Page 66 and 67: CHAPTER 2 ■ LISTS AND TUPLES 35 A
- Page 68 and 69: CHAPTER 2 ■ LISTS AND TUPLES 37 >
- Page 70 and 71: CHAPTER 2 ■ LISTS AND TUPLES 39 M
- Page 72 and 73: CHAPTER 2 ■ LISTS AND TUPLES 41 T
- Page 74 and 75: CHAPTER 2 ■ LISTS AND TUPLES 43 L
- Page 76 and 77: CHAPTER 2 ■ LISTS AND TUPLES 45 W
- Page 78 and 79: CHAPTER 2 ■ LISTS AND TUPLES 47
- Page 80 and 81: CHAPTER 2 ■ LISTS AND TUPLES 49 a
- Page 82 and 83: CHAPTER 2 ■ LISTS AND TUPLES 51 S
- Page 84 and 85: CHAPTER 3 ■ ■ ■ Working with
- Page 86 and 87: CHAPTER 3 ■ WORKING WITH STRINGS
- Page 88 and 89: CHAPTER 3 ■ WORKING WITH STRINGS
- Page 90 and 91: CHAPTER 3 ■ WORKING WITH STRINGS
- Page 92 and 93: CHAPTER 3 ■ WORKING WITH STRINGS
- Page 94 and 95: CHAPTER 3 ■ WORKING WITH STRINGS
- Page 96 and 97: CHAPTER 3 ■ WORKING WITH STRINGS
- Page 98 and 99: CHAPTER 4 ■ ■ ■ Dictionaries:
- Page 100 and 101: CHAPTER 4 ■ DICTIONARIES: WHEN IN
- Page 102 and 103: CHAPTER 4 ■ DICTIONARIES: WHEN IN
- Page 104 and 105: CHAPTER 4 ■ DICTIONARIES: WHEN IN
- Page 106 and 107: CHAPTER 4 ■ DICTIONARIES: WHEN IN
CHAPTER 1 ■ INSTANT HACKING: THE BASICS 27<br />
Long Strings<br />
If you want <strong>to</strong> write a really long string, one that spans several lines, you can use triple quotes<br />
instead of ordinary quotes:<br />
print '''This is a very long string.<br />
It continues here.<br />
And it's not over yet.<br />
"Hello, world!"<br />
Still here.'''<br />
You can also use triple double quotes, """like this""". Note that because of the distinctive<br />
enclosing quotes, both single and double quotes are allowed inside, without being backslashescaped.<br />
■Tip Ordinary strings can also span several lines. If the last character on a line is a backslash, the line break<br />
itself is “escaped,” and is ignored. For example:<br />
print "Hello, \<br />
world!"<br />
would print out Hello, world!. The same goes for expressions and statements in general:<br />
>>> 1 + 2 + \<br />
4 + 5<br />
12<br />
>>> print \<br />
'Hello, world'<br />
Hello, world<br />
Raw Strings<br />
Raw strings aren’t <strong>to</strong>o picky about backslashes, which can be very useful sometimes. 10 In ordinary<br />
strings, the backslash has a special role: It escapes things, letting you put things in<strong>to</strong> your<br />
string that you couldn’t normally write directly. For example, a new line is written \n, and can<br />
be put in<strong>to</strong> a string like this:<br />
>>> print 'Hello,\nworld!'<br />
Hello,<br />
world!<br />
This is normally just dandy, but in some cases it’s not what you want. What if you wanted the<br />
string <strong>to</strong> include a backslash followed by an n? You might want <strong>to</strong> put the DOS pathname<br />
C:\nowhere in<strong>to</strong> a string, for example:<br />
10. Especially when writing regular expressions. More about those in Chapter 10.