O'Reilly logo

Python: Essential Reference, Third Edition by David Beazley

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Operations on Sequences

The following operators can be applied to sequence types, including strings, lists, and tuples:

OperationDescription
s + rConcatenation
s * n, n * sMakes n copies of s, where n is an integer
s % dString formatting (strings only)
s[i]Indexing
s[i:j]Slicing
s[i:j:stride]Extended slicing
x in s, x not in sMembership
for x in s:Iteration
len(s)Length
min(s)Minimum item
max(s)Maximum item

The + operator concatenates two sequences of the same type. The s * n operator makes n copies of a sequence. However, these are shallow copies that replicate elements by reference only. For example, consider the following code:

 a = [3,4,5] # A list b = [a] # A list containing a c = 4*b # Make four copies of b # Now modify a a[0] = -7 # Look at c print ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required