Linux/Unix > Python Essential Reference, Second Edition > 2. Lexical Conventions and Syntax > Literals | See All Titles

< BACK | Make Note | Bookmark | CONTINUE >

155117184014003188065099048180054212144238241179195140058238110137104027027051146142231049

### Literals

There are four built-in numeric types: integers, long integers, floating-point numbers, and complex numbers.

A number such as `1234` is interpreted as a decimal integer. To specify octal and hexadecimal integers, precede the value with `0` or `0x`, respectively— `0644` or `0x100fea8`. Write a long integer using a trailing `l` (ell) or `L` character, as in `1234567890L`. Unlike integers, which are limited by machine precision, long integers can be of any length (up to the maximum memory of the machine). Numbers such as `123.34` and `1.2334e+02` are interpreted as floating-point numbers. An integer or floating-point number with a trailing `j` or `J`, such as `12.34J`, is an imaginary number. You can create complex numbers with real and imaginary parts by adding a real number and an imaginary number, as in `1.2 + 12.34J`.

Python currently supports two types of string literals:

- 8-bit character data (ASCII)

- Unicode (16-bit wide character data)

The most commonly used string type is 8-bit character data, because of its use in representing characters from the ASCII or ISO-Latin character set as well as representing raw binary data as a sequence of bytes. By default, 8-bit string literals are defined by enclosing text in single (`'`), double (`"`), or triple (`'''` or `"""`) quotes. You must use the same type of quote to start and terminate a string. The backslash (`\`) character is used to escape special characters such as newlines, the backslash itself, quotes, and nonprinting characters. Table 2.1 shows the accepted escape codes. Unrecognized escape sequences are left in the string unmodified and include the leading backslash. Furthermore, strings may contain embedded null bytes and binary data. Triple-quoted strings can span multiple lines and include unescaped newlines and quotes.

##### Table 2.1. Standard Character Escape Codes

Character | Description
--- | ---
`\` | Newline continuation
`\\` | Backslash
`\'` | Single quote
`\"` | Double quote
`\a` | Bell
`\b` | Backspace
`\e` | Escape
`\0` | Null
`\n` | Line feed
`\v` | Vertical tab
`\t` | Horizontal tab
`\r` | Carriage return
`\f` | Form feed
`\OOO` | Octal value (`000` to `\377`)
`\xhh` | Hexadecimal value (`x00` to `\xff`)
`\uhhh` | Unicode character value (Unicode strings only)

Unicode strings are used to represent multibyte international character sets and allow for 65,536 unique characters. Unicode string literals are defined by preceding an ordinary string literal with a `u` or `U`, such as in `u"hello"`. In Unicode, each character is internally represented by a 16-bit integer value. For the purposes of notation, this value is written as U+XXXX, where XXXX is a four-digit hexadecimal number. (Note: This notation is only a convention used to describe Unicode characters and is not Python syntax.) For example, `U+0068` is the Unicode character for the letter `h` in the Latin-1 character set. When Unicode string literals are defined, ordinary characters and escape codes are directly mapped as Unicode ordinals in the range [U+0000, U+00FF]. For example, the string `"hello\n"` is mapped to the sequence of ASCII values 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x0a, whereas the Unicode string `u"hello\n"` is mapped to the sequence U+0068, U+0065, U+006C, U+006C, U+006F, U+000A. Arbitrary Unicode characters are defined using the `\u` `XXXX` escape sequence. This sequence can only appear inside a Unicode string literal and must always specify a four-digit hexadecimal value. For example:

    s = u"\u0068\u0065\u006c\u006c\u006f\u000a"

In older versions of Python, the `\x` `XXXX` escape could be used to define Unicode characters. Although this is still allowed, the `\u` `XXXX` sequence should be used instead. In addition, the `\OOO` octal escape sequence can be used to define Unicode characters in the range [U+0000, U+01FF].

Unicode string literals should not be defined using a sequence of raw bytes that correspond to a multibyte Unicode data encoding such as UTF-8 or UTF-16. For example, writing a raw UTF-8 encoded string such as `u'M\303\274ller'` produces the seven-character Unicode sequence U+004D, U+00C3, U+00BC, U+006C, U+006C, U+0065, U+0072, which is probably not what you wanted. This is because in UTF-8, the multibyte sequence `\303\274` is supposed to represent U+00FC, not U+00C3, U+00BC. For more details about Unicode encodings, see Chapter 3, “Types and Objects,” Chapter 4, “Operators and Expressions,” and Chapter 9, “Input and Output.”

Optionally, you can precede a string with an `r` or `R`, such as in `r"\n\" '`. These strings are known as raw strings because all their backslash characters are left intact—that is, the string literally contains the enclosed text, including the backslashes. Raw strings cannot end in a single backslash, such as `r"\"`. When raw Unicode strings are defined by preceding the string with `ur` or `UR`, `\u` `XXXX` escape sequences are still interpreted as Unicode characters, provided that the number of preceding `\` characters is odd. For instance, `ur"\u1234"` defines a raw Unicode string with the character U+1234, whereas `ur"\\u1234"` defines a seven-character Unicode string in which the first two characters are slashes and the remaining five characters are the literal `"u1234"`. Also, when defining raw Unicode string literals the “`r`” must appear after the “`u`” as shown.

Adjacent strings (separated by white space or a newline continuation character) such as `"hello" 'world'` are concatenated to form a single string: `"helloworld"`. String concatenation works with any mix of ordinary, raw, and Unicode strings. However, whenever one of the strings is Unicode, the final result is always coerced to Unicode. Therefore, `"hello" u"world"` is the same as `u"hello" + u"world"`. In addition, due to subtle implementation aspects of Unicode, writing `"s1" u"s2"` may produce a result that’s different from writing `u"s1s2"`. The details of this coercion process are described further in Chapter 4 and Appendix A (the Python library).

If Python is run with the `-U` command-line option, all string literals are interpreted as Unicode.

Values enclosed in square brackets `[...]`, parentheses `(...)`, and braces `{...}` denote lists, tuples, and dictionaries, respectively, as in the following example:

     a = [ 1, 3.4, 'hello' ]           # A list
     b = ( 10, 20, 30 )                # A tuple
     c = { 'a': 3, 'b':42 }            # A dictionary

| Last updated on 3/28/2002 Python Essential Reference, Second Edition, © 2002 New Riders Publishing

< BACK | Make Note | Bookmark | CONTINUE >

© 2002, O'Reilly & Associates, Inc.