There are three sequence types: strings, lists and tuples.
Strings literals are written in single or double quotes:
'xyzzy'
, "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
comma: (d,)
.
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:
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 concatenated | (1) |
s[i] |
i'th item of s, origin 0 | (2) |
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:
0
are treated as
0
(which yields an empty sequence of the same type as
s).
len(s) + i
or
len(s) + j
is substituted. But note that -0
is
still 0
.
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 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
operation.
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:
%
, c
, s
, i
, d
, u
, o
,
x
, X
, e
, E
, f
, g
, G
.
Width and precision may be a *
to specify that an integer argument
specifies the actual width or precision. The flag characters
-
, +
, blank, #
and 0
are understood. The
size specifiers h
, l
or L
may be present but are
ignored. The %s
conversion takes any Python object and
converts it to a string using str()
before formatting it. The
ANSI features %p
and %n
are not supported. Since
Python strings have an explicit length, %s
conversions don't
assume that '\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):
Operation | Result | Notes |
---|---|---|
s[i] = x |
item i of s is replaced by x | |
s[i:j] = t |
slice of s from i to j is replaced by t | |
del s[i:j] |
same as s[i:j] = [] |
|
s.append(x) |
same as s[len(s):len(s)] = [x] |
(1) |
s.extend(x) |
same as s[len(s):len(s)] = x |
(2) |
s.count(x) |
return number of i's for which s[i] == x |
|
s.index(x) |
return smallest i such that s[i] == x |
(3) |
s.insert(i, x) |
same as s[i:i] = [x] if i >= 0 |
|
s.pop([i]) |
same as x = s[i]; del s[i]; return x |
(4) |
s.remove(x) |
same as del s[s.index(x)] |
(3) |
s.reverse() |
reverses the items of s in place | (5) |
s.sort([cmpfunc]) |
sort the items of s in place | (5), (6) |
-1
, so that by default the last item is
removed and returned.
-1
, 0
or 1
depending 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.