Subsections

- 2.1.5.1 More String Operations
- 2.1.5.2 Mutable Sequence Types

##
2.1.5 Sequence Types

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:

(1)
  Values of n less than `0` are treated as `0` (which yields an empty sequence of the same type as s).

(2)
  If i or j is negative, the index is relative to the end of the string, i.e., `len(s) + i` or `len(s) + j` is substituted. But note that `-0` is still `0`.
(3)
  The slice of s from i to j is defined as the sequence of items with index k such that `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.

###
2.1.5.1 More String Operations

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`.

###
2.1.5.2 Mutable Sequence Types

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)

Notes:

(1)
  The C implementation of Python has historically accepted multiple parameters and implicitly joined them into a tuple; this will no longer work in Python 1.6. Use of this misfeature has been deprecated since Python 1.4.

(2)
  Raises an exception when x is not a list object. The `extend()` method is experimental and not supported by mutable sequence types other than lists.

(3)
  Raises `ValueError` when x is not found in s.

(4)
  The `pop()` method is experimental and not supported by other mutable sequence types than lists. The optional argument i defaults to `-1`, so that by default the last item is removed and returned.

(5)
  The `sort()` and `reverse()` methods modify the list in place for economy of space when sorting or reversing a large list. They don't return the sorted or reversed list to remind you of this side effect.

(6)
  The `sort()` method takes an optional argument specifying a comparison function of two arguments (list items) which should return `-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.

#### Footnotes

... operations.2.4
  They must have since the parser can't tell the type of the operands.
... object.2.5
  A tuple object in this case should be a singleton.
... conversions.2.6
  These numbers are fairly arbitrary. They are intended to avoid printing endless strings of meaningless digits without hampering correct use and without having to know the exact precision of floating point values on a particular machine.