There are three sequence types: strings, lists and tuples.
Strings literals are written in single or double quotes:
"frobozz". See chapter 2 of the Python
Reference Manual for more about string literals. 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
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.2.4
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:
||the concatenation of s and t|
||n copies of s concatenated||(1)|
||i'th item of s, origin 0||(2)|
||slice of s from i to j||(2), (3)|
||length of s|
||smallest item of s|
||largest item of s|
0are treated as
0(which yields an empty sequence of the same type as s).
len(s) + ior
len(s) + jis substituted. But note that
i <= k < j. If i or j is greater than
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 objects have one unique built-in operation: the
%operator (modulo) with a string left argument interprets this string
as a C sprintf() format string to be applied to the
right argument, and returns the string resulting from this formatting
The right argument should be a tuple with one item for each argument
required by the format string; if the string requires a single
argument, the right argument may also be a single non-tuple
object.2.5The following format characters are understood:
Width and precision may be a
* to specify that an integer argument
specifies the actual width or precision. The flag characters
0 are understood. The
L may be present but are
%s conversion takes any Python object and
converts it to a string using
str() before formatting it. The
%n are not supported. Since
Python strings have an explicit length,
%s conversions don't
'\0' is the end of the string.
For safety reasons, floating point precisions are clipped to 50;
%f conversions for numbers whose absolute value is over 1e25
are replaced by
%g conversions.2.6All other errors raise exceptions.
If the right argument is a dictionary (or any kind of mapping), then the formats in the string must have a parenthesized key into that dictionary inserted immediately after the "%" character, and each format formats the corresponding entry from the mapping. For example:
>>> count = 2 >>> language = 'Python' >>> print'%(language)s has %(count)03d quote types.' % vars() Python has 002 quote types.
In this case no
* specifiers may occur in a format (since they
require a sequential parameter list).
Additional string operations are defined in standard module string and in built-in module re.
List objects support additional operations that allow in-place modification of the object. These operations would be supported by other mutable sequence types (when added to the language) as well. Strings and tuples are immutable sequence types and such objects cannot be modified once created. The following operations are defined on mutable sequence types (where x is an arbitrary object):
||item i of s is replaced by x|
||slice of s from i to j is replaced by t|
||return number of i's for which
||return smallest i such that
||reverses the items of s in place||(5)|
||sort the items of s in place||(5), (6)|
-1, so that by default the last item is removed and returned.
1depending on whether the first argument is considered smaller than, equal to, or larger than the second argument. Note that this slows the sorting process down considerably; e.g. to sort a list in reverse order it is much faster to use calls to the methods sort() and reverse() than to use the built-in function sort() with a comparison function that reverses the ordering of the elements.