GNU Info

Info Node: (python2.1-lib.info)Sequence Types

(python2.1-lib.info)Sequence Types


Next: Mapping Types Prev: Numeric Types Up: Built-in Types
Enter node , (file) or (file)node

Sequence Types
--------------

There are six sequence types: strings, Unicode strings, lists, tuples,
buffers, and xrange objects.

Strings literals are written in single or double quotes: `'xyzzy'',
`"frobozz"'.  See chapter 2 of the  for more about string literals.
Unicode strings are much like strings, but are specified in the syntax
using a preceeding `u' character: `u'abc'', `u"def"'.  Lists are
constructed with square brackets, separating items with commas: `[a, b,
c]'.  Tuples are constructed by the comma operator (not within square
brackets), with or without enclosing parentheses, but an empty tuple
must have the enclosing parentheses, e.g., `a, b, c' or `()'.  A single
item tuple must have a trailing comma, e.g., `(d,)'.  Buffers are not
directly supported by Python syntax, but can be created by calling the
builtin function `buffer()'.    XRanges objects are similar to buffers
in that there is no specific syntax to create them, but they are
created using the `xrange()' function.

Sequence types support the following operations.  The `in' and `not in'
operations have the same priorities as the comparison operations.  The
`+' and `*' operations have the same priority as the corresponding
numeric operations.(1)

This table lists the sequence operations sorted in ascending priority
(operations in the same box have the same priority).  In the table, S
and T are sequences of the same type; N, I and J are integers:

Operation                Result                   Notes
------                   -----                    -----
X in S                   `1' if an item of S is   
                         equal to X, else `0'     
X not in S               `0' if an item of S is   
                         equal to X, else `1'     
S + T                    the concatenation of S   
                         and T                    
S * N, N * S             N copies of S            (1)
                         concatenated             
S[I]                     I'th item of S, origin   (2)
                         0                        
S[I:J]                   slice of S from I to J   (2), (3)
len(S)                   length of S              
min(S)                   smallest item of S       
max(S)                   largest item of S        

Notes:

`(1)'
     Values of N less than `0' are treated as `0' (which yields an
     empty sequence of the same type as S).

`(2)'
     If I or J is negative, the index is relative to the end of the
     string, i.e., `len(S) + I' or `len(S) + J' is substituted.  But
     note that `-0' is still `0'.

`(3)'
     The slice of S from I to J is defined as the sequence of items
     with index K such that `I <= K < J'.  If I or J is greater than
     `len(S)', use `len(S)'.  If I is omitted, use `0'.  If J is
     omitted, use `len(S)'.  If I is greater than or equal to J, the
     slice is empty.

String Methods
String Formatting Operations
XRange Type
Mutable Sequence Types
---------- Footnotes ----------

(1) They must have since the parser can't tell the type of the operands.


automatically generated by info2www version 1.2.2.9