04.08.2014 Views

o_18ufhmfmq19t513t3lgmn5l1qa8a.pdf

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

CHAPTER 1 ■ INSTANT HACKING: THE BASICS 27<br />

Long Strings<br />

If you want to 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 too 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 into your<br />

string that you couldn’t normally write directly. For example, a new line is written \n, and can<br />

be put into 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 to include a backslash followed by an n? You might want to put the DOS pathname<br />

C:\nowhere into a string, for example:<br />

10. Especially when writing regular expressions. More about those in Chapter 10.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!