12.07.2015 Views

Is Python a

Is Python a

Is Python a

SHOW MORE
SHOW LESS
  • No tags were found...

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

The two characters \n stand for a single character—the byte containing the binaryvalue of the newline character in your character set (usually, ASCII code 10). Similarly,the sequence \t is replaced with the tab character. The way this string lookswhen printed depends on how you print it. The interactive echo shows the specialcharacters as escapes, but print interprets them instead:>>> s'a\nb\tc'>>> print sab cTo be completely sure how many bytes are in this string, use the built-in len function—itreturns the actual number of bytes in a string, regardless of how it is displayed:>>> len(s)5This string is five bytes long: it contains an ASCII a byte, a newline byte, an ASCII bbyte, and so on. Note that the original backslash characters are not really stored withthe string in memory. For coding such special bytes, <strong>Python</strong> recognizes a full set ofescape code sequences, listed in Table 7-2.Table 7-2. String backslash charactersaEscapeMeaning\newlineIgnored (continuation)\\ Backslash (keeps a \)\'Single quote (keeps ')\" Double quote (keeps ")\a Bell\b Backspace\f Formfeed\n Newline (linefeed)\r Carriage return\t Horizontal tab\v Vertical tab\N{id}\uhhhhUnicode database IDUnicode 16-bit hex\Uhhhh...Unicode 32-bit hex a\xhhHex digits value\oooOctal digits value\0 Null (doesn’t end string)\otherNot an escape (kept)The \Uhhhh... escape sequence takes exactly eight hexadecimaldigits (h); both \u and \U can be used only in Unicode string literals.126 | Chapter 7: Strings

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

Saved successfully!

Ooh no, something went wrong!