B.4 Sequence Operators

Python has three sequential data types: strings, lists, and tuples. Table B.4 summarizes the operators that apply to all sequences.

Table B.4 Operations on Any Sequence in Python

Operation Operator Explanation
Indexing [n] Accesses the nth element of a sequence.
Concatenation + Combines sequences.
Repetition * Concatenates a repeated number of times.
Membership in Returns True if an item is in a sequence; False otherwise.
Membership not in Returns True if an item is not in a sequence; False otherwise.
Slicing [ : ] Extracts a part of a sequence.

B.4.1 String

Strings are immutable sequences of characters that are indexed by integers, starting at zero. To create string literals, you can enclose them ...

Get Python Programming in Context, 4th Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.