These functions work on values of the four sequence
types: str
, unicode
, tuple
, and list
.
These operators apply to sequences.
S_{1}
+S_{2}
Concatenation—for two sequences
and S_{1}
of the same type, a new sequence containing all the
elements from S_{2}
followed by
all the elements of S_{1}
.
S_{2}
>>> "vi" + "car" 'vicar' >>> [1,2,3]+[5,7,11,13]+[15] [1, 2, 3, 5, 7, 11, 13, 15] >>> ('roy', 'g')+('biv',) ('roy', 'g', 'biv')
S
*n
For a sequence
and a positive integer S
, the result
is a new sequence containing all the elements of
n
repeated S
times.
n
>>> 'worra'*8 'worraworraworraworraworraworraworraworra' >>> [0]*4 [0, 0, 0, 0] >>> (True, False)*5 (True, False, True, False, True, False, True, False, True, False)
x
in S
Is any element of a sequence
equal to S
?
x
For convenience in searching for substrings, if the
sequence to be searched is a string, the
operand can be a
multi-character string. In that case, the operation
returns x
True
if
is found anywhere in x
.
S
>>> 1 in [2,4,6,0,8,0] False >>> 0 in [2,4,6,0,8,0] True >>> 'a' in 'banana' True >>> 3.0 in (2.5, 3.0, 3.5) True >>> "baz" in "rowrbazzle" True
x
not in S
Are all the elements of a sequence
not equal
to S
?
x
>>> 'a' not in 'banana' False >>> 'x' not in 'banana' True
S
[i
]
Subscripting: retrieve the
th element of i
, counting from zero. If s
is greater
than or equal to the number of elements of i
, an S
IndexError
exception is raised.
>>> 'Perth'[0] 'P' >>> 'Perth'[1] 'e' >>> 'Perth'[4] 'h' >>> 'Perth'[5] Traceback (most recent call last): File "<stdin>", line 1, in <module> IndexError: string index out of range >>> ('red', 'yellow', 'green')[2] 'green'
S
[i
:j
]
Slicing: For a sequence
and two integers S
and i
, return a
new sequence with copies of the elements of j
between
positions S
and i
.
j
The values used in slicing refer to the positions between elements, where position zero is the position before the first element; position 1 is between the first and second element; and so on.
You can also specify positions relative to the end of a sequence. Position -1 is the position before the last element; -2 is the position before the second-to-last element; and so on.
You can omit the starting position to obtain a slice starting at the beginning. You can omit the ending position to get all the elements through the last.
For example, here is a diagram showing three slices
of the string 'abcdef'
.
>>> 'abcdef'[2:5] 'cde' >>> 'abcdef'[:3] 'abc' >>> 'abcdef'[3:] 'def' >>> (90, 91, 92, 93, 94, 95)[2:5] (92, 93, 94)
S
[i
:j
:k
]
You can use a slice expression like this to select
every
th element. Examples:
k
>>> teens = range(13,20) >>> teens [13, 14, 15, 16, 17, 18, 19] >>> teens[::2] [13, 15, 17, 19] >>> teens[1::2] [14, 16, 18] >>> teens[1:5] [14, 15, 16, 17] >>> teens[1:5:2] [14, 16]
The stride argument
may be negative. If so,
the default starting position is the length of
the sequence, and the default ending is the
beginning of the sequence.
k
>>> s='0123456789' >>> s[::-1] '9876543210' >>> s[5::-1] '543210' >>> s[7:1:-2] '753' >>> s[:2:-1] '9876543' >>> s[5:2:-1] '543'
These methods apply to any sequence value
:
S
S
.count(t
)
Returns the number of elements of
that are
equal to S
.
t
>>> shazam = 'abracadabra' >>> shazam.count('a') 5 >>> shazam.count('x') 0 >>> [0, 0, 0, 0, 18, 0].count(0) 5
S
.index(t
[,start
[,end
]])
Returns the index of the first element of
that
is equal to S
. If there is no such element, the
method raises a t
ValueError
exception.
>>> 'council'.index('co') 0 >>> 'council'.index('phd') Traceback (most recent call last): File "<stdin>", line 1, in <module> ValueError: substring not found >>> L=[5,17,23] >>> L.index(23) 2 >>> L.index(83) Traceback (most recent call last): File "<stdin>", line 1, in <module> ValueError: 83 is not in list