blob: 87c64214c67db7cc30a6a1ae45003d9623990e35 [file] [log] [blame]
Georg Brandl116aa622007-08-15 14:28:22 +00001.. XXX: reference/datamodel and this have quite a few overlaps!
2
3
4.. _bltin-types:
5
6**************
7Built-in Types
8**************
9
10The following sections describe the standard types that are built into the
11interpreter.
12
Georg Brandl116aa622007-08-15 14:28:22 +000013.. index:: pair: built-in; types
14
Antoine Pitroue231e392009-12-19 18:22:15 +000015The principal built-in types are numerics, sequences, mappings, classes,
Georg Brandl116aa622007-08-15 14:28:22 +000016instances and exceptions.
17
Georg Brandl388349a2011-10-08 18:32:40 +020018Some collection classes are mutable. The methods that add, subtract, or
19rearrange their members in place, and don't return a specific item, never return
20the collection instance itself but ``None``.
21
Georg Brandl116aa622007-08-15 14:28:22 +000022Some operations are supported by several object types; in particular,
23practically all objects can be compared, tested for truth value, and converted
24to a string (with the :func:`repr` function or the slightly different
25:func:`str` function). The latter function is implicitly used when an object is
26written by the :func:`print` function.
27
28
29.. _truth:
30
31Truth Value Testing
32===================
33
34.. index::
35 statement: if
36 statement: while
37 pair: truth; value
38 pair: Boolean; operations
39 single: false
40
41Any object can be tested for truth value, for use in an :keyword:`if` or
Peter Thomassencaa12802017-07-29 21:18:13 +020042:keyword:`while` condition or as operand of the Boolean operations below.
Georg Brandl116aa622007-08-15 14:28:22 +000043
44.. index:: single: true
45
Peter Thomassencaa12802017-07-29 21:18:13 +020046By default, an object is considered true unless its class defines either a
47:meth:`__bool__` method that returns ``False`` or a :meth:`__len__` method that
48returns zero, when called with the object. [1]_ Here are most of the built-in
49objects considered false:
50
51 .. index::
52 single: None (Built-in object)
53 single: False (Built-in object)
54
55* constants defined to be false: ``None`` and ``False``.
56
57* zero of any numeric type: ``0``, ``0.0``, ``0j``, ``Decimal(0)``,
58 ``Fraction(0, 1)``
59
60* empty sequences and collections: ``''``, ``()``, ``[]``, ``{}``, ``set()``,
61 ``range(0)``
Georg Brandl116aa622007-08-15 14:28:22 +000062
63.. index::
64 operator: or
65 operator: and
66 single: False
67 single: True
68
69Operations and built-in functions that have a Boolean result always return ``0``
70or ``False`` for false and ``1`` or ``True`` for true, unless otherwise stated.
71(Important exception: the Boolean operations ``or`` and ``and`` always return
72one of their operands.)
73
74
75.. _boolean:
76
77Boolean Operations --- :keyword:`and`, :keyword:`or`, :keyword:`not`
78====================================================================
79
80.. index:: pair: Boolean; operations
81
82These are the Boolean operations, ordered by ascending priority:
83
84+-------------+---------------------------------+-------+
85| Operation | Result | Notes |
86+=============+=================================+=======+
87| ``x or y`` | if *x* is false, then *y*, else | \(1) |
88| | *x* | |
89+-------------+---------------------------------+-------+
90| ``x and y`` | if *x* is false, then *x*, else | \(2) |
91| | *y* | |
92+-------------+---------------------------------+-------+
93| ``not x`` | if *x* is false, then ``True``, | \(3) |
94| | else ``False`` | |
95+-------------+---------------------------------+-------+
96
97.. index::
98 operator: and
99 operator: or
100 operator: not
101
102Notes:
103
104(1)
105 This is a short-circuit operator, so it only evaluates the second
Mariatta8eb531d2017-03-03 13:16:29 -0800106 argument if the first one is false.
Georg Brandl116aa622007-08-15 14:28:22 +0000107
108(2)
109 This is a short-circuit operator, so it only evaluates the second
Mariatta8eb531d2017-03-03 13:16:29 -0800110 argument if the first one is true.
Georg Brandl116aa622007-08-15 14:28:22 +0000111
112(3)
113 ``not`` has a lower priority than non-Boolean operators, so ``not a == b`` is
114 interpreted as ``not (a == b)``, and ``a == not b`` is a syntax error.
115
116
117.. _stdcomparisons:
118
119Comparisons
120===========
121
Alexandre Vassalotti6d3dfc32009-07-29 19:54:39 +0000122.. index::
123 pair: chaining; comparisons
124 pair: operator; comparison
125 operator: ==
126 operator: <
127 operator: <=
128 operator: >
129 operator: >=
130 operator: !=
131 operator: is
132 operator: is not
Georg Brandl116aa622007-08-15 14:28:22 +0000133
Georg Brandl905ec322007-09-28 13:39:25 +0000134There are eight comparison operations in Python. They all have the same
135priority (which is higher than that of the Boolean operations). Comparisons can
Georg Brandl116aa622007-08-15 14:28:22 +0000136be chained arbitrarily; for example, ``x < y <= z`` is equivalent to ``x < y and
137y <= z``, except that *y* is evaluated only once (but in both cases *z* is not
138evaluated at all when ``x < y`` is found to be false).
139
140This table summarizes the comparison operations:
141
Georg Brandlfd855162008-01-07 09:13:03 +0000142+------------+-------------------------+
143| Operation | Meaning |
144+============+=========================+
145| ``<`` | strictly less than |
146+------------+-------------------------+
147| ``<=`` | less than or equal |
148+------------+-------------------------+
149| ``>`` | strictly greater than |
150+------------+-------------------------+
151| ``>=`` | greater than or equal |
152+------------+-------------------------+
153| ``==`` | equal |
154+------------+-------------------------+
155| ``!=`` | not equal |
156+------------+-------------------------+
157| ``is`` | object identity |
158+------------+-------------------------+
159| ``is not`` | negated object identity |
160+------------+-------------------------+
Christian Heimes5b5e81c2007-12-31 16:14:33 +0000161
162.. index::
Georg Brandl116aa622007-08-15 14:28:22 +0000163 pair: object; numeric
164 pair: objects; comparing
165
Georg Brandl905ec322007-09-28 13:39:25 +0000166Objects of different types, except different numeric types, never compare equal.
Antoine Pitroue231e392009-12-19 18:22:15 +0000167Furthermore, some types (for example, function objects) support only a degenerate
Georg Brandl905ec322007-09-28 13:39:25 +0000168notion of comparison where any two objects of that type are unequal. The ``<``,
169``<=``, ``>`` and ``>=`` operators will raise a :exc:`TypeError` exception when
Mark Dickinsonf673f0c2010-03-13 09:48:39 +0000170comparing a complex number with another built-in numeric type, when the objects
171are of different types that cannot be compared, or in other cases where there is
172no defined ordering.
Georg Brandl116aa622007-08-15 14:28:22 +0000173
Georg Brandl48310cd2009-01-03 21:18:54 +0000174.. index::
Georg Brandl905ec322007-09-28 13:39:25 +0000175 single: __eq__() (instance method)
176 single: __ne__() (instance method)
177 single: __lt__() (instance method)
178 single: __le__() (instance method)
179 single: __gt__() (instance method)
180 single: __ge__() (instance method)
Georg Brandl116aa622007-08-15 14:28:22 +0000181
Georg Brandl05f5ab72008-09-24 09:11:47 +0000182Non-identical instances of a class normally compare as non-equal unless the
183class defines the :meth:`__eq__` method.
Georg Brandl116aa622007-08-15 14:28:22 +0000184
Georg Brandl905ec322007-09-28 13:39:25 +0000185Instances of a class cannot be ordered with respect to other instances of the
186same class, or other types of object, unless the class defines enough of the
Georg Brandl05f5ab72008-09-24 09:11:47 +0000187methods :meth:`__lt__`, :meth:`__le__`, :meth:`__gt__`, and :meth:`__ge__` (in
188general, :meth:`__lt__` and :meth:`__eq__` are sufficient, if you want the
189conventional meanings of the comparison operators).
Georg Brandl905ec322007-09-28 13:39:25 +0000190
191The behavior of the :keyword:`is` and :keyword:`is not` operators cannot be
192customized; also they can be applied to any two objects and never raise an
193exception.
Georg Brandl116aa622007-08-15 14:28:22 +0000194
195.. index::
196 operator: in
197 operator: not in
198
Georg Brandl375aec22011-01-15 17:03:02 +0000199Two more operations with the same syntactic priority, :keyword:`in` and
200:keyword:`not in`, are supported only by sequence types (below).
Georg Brandl116aa622007-08-15 14:28:22 +0000201
202
203.. _typesnumeric:
204
Georg Brandl905ec322007-09-28 13:39:25 +0000205Numeric Types --- :class:`int`, :class:`float`, :class:`complex`
206================================================================
Georg Brandl116aa622007-08-15 14:28:22 +0000207
208.. index::
209 object: numeric
210 object: Boolean
211 object: integer
Georg Brandl116aa622007-08-15 14:28:22 +0000212 object: floating point
213 object: complex number
214 pair: C; language
215
Mark Summerfieldbbfd71d2008-07-01 15:50:04 +0000216There are three distinct numeric types: :dfn:`integers`, :dfn:`floating
217point numbers`, and :dfn:`complex numbers`. In addition, Booleans are a
218subtype of integers. Integers have unlimited precision. Floating point
Georg Brandl60203b42010-10-06 10:11:56 +0000219numbers are usually implemented using :c:type:`double` in C; information
Mark Dickinson74f59022010-08-04 18:42:43 +0000220about the precision and internal representation of floating point
221numbers for the machine on which your program is running is available
222in :data:`sys.float_info`. Complex numbers have a real and imaginary
223part, which are each a floating point number. To extract these parts
224from a complex number *z*, use ``z.real`` and ``z.imag``. (The standard
225library includes additional numeric types, :mod:`fractions` that hold
226rationals, and :mod:`decimal` that hold floating-point numbers with
227user-definable precision.)
Georg Brandl116aa622007-08-15 14:28:22 +0000228
229.. index::
230 pair: numeric; literals
231 pair: integer; literals
Georg Brandl116aa622007-08-15 14:28:22 +0000232 pair: floating point; literals
233 pair: complex number; literals
234 pair: hexadecimal; literals
235 pair: octal; literals
Neal Norwitz1d2aef52007-10-02 07:26:14 +0000236 pair: binary; literals
Georg Brandl116aa622007-08-15 14:28:22 +0000237
238Numbers are created by numeric literals or as the result of built-in functions
Georg Brandl905ec322007-09-28 13:39:25 +0000239and operators. Unadorned integer literals (including hex, octal and binary
240numbers) yield integers. Numeric literals containing a decimal point or an
241exponent sign yield floating point numbers. Appending ``'j'`` or ``'J'`` to a
242numeric literal yields an imaginary number (a complex number with a zero real
243part) which you can add to an integer or float to get a complex number with real
244and imaginary parts.
Georg Brandl116aa622007-08-15 14:28:22 +0000245
246.. index::
247 single: arithmetic
248 builtin: int
Georg Brandl116aa622007-08-15 14:28:22 +0000249 builtin: float
250 builtin: complex
Alexandre Vassalotti6d3dfc32009-07-29 19:54:39 +0000251 operator: +
252 operator: -
253 operator: *
254 operator: /
255 operator: //
256 operator: %
257 operator: **
Georg Brandl116aa622007-08-15 14:28:22 +0000258
259Python fully supports mixed arithmetic: when a binary arithmetic operator has
260operands of different numeric types, the operand with the "narrower" type is
Georg Brandl905ec322007-09-28 13:39:25 +0000261widened to that of the other, where integer is narrower than floating point,
262which is narrower than complex. Comparisons between numbers of mixed type use
Ezio Melotti0656a562011-08-15 14:27:19 +0300263the same rule. [2]_ The constructors :func:`int`, :func:`float`, and
Georg Brandl905ec322007-09-28 13:39:25 +0000264:func:`complex` can be used to produce numbers of a specific type.
Georg Brandl116aa622007-08-15 14:28:22 +0000265
266All numeric types (except complex) support the following operations, sorted by
Georg Brandle4196d32014-10-31 09:41:46 +0100267ascending priority (all numeric operations have a higher priority than
268comparison operations):
Georg Brandl116aa622007-08-15 14:28:22 +0000269
Raymond Hettingerc706dbf2011-03-22 17:33:17 -0700270+---------------------+---------------------------------+---------+--------------------+
271| Operation | Result | Notes | Full documentation |
272+=====================+=================================+=========+====================+
273| ``x + y`` | sum of *x* and *y* | | |
274+---------------------+---------------------------------+---------+--------------------+
275| ``x - y`` | difference of *x* and *y* | | |
276+---------------------+---------------------------------+---------+--------------------+
277| ``x * y`` | product of *x* and *y* | | |
278+---------------------+---------------------------------+---------+--------------------+
279| ``x / y`` | quotient of *x* and *y* | | |
280+---------------------+---------------------------------+---------+--------------------+
281| ``x // y`` | floored quotient of *x* and | \(1) | |
282| | *y* | | |
283+---------------------+---------------------------------+---------+--------------------+
284| ``x % y`` | remainder of ``x / y`` | \(2) | |
285+---------------------+---------------------------------+---------+--------------------+
286| ``-x`` | *x* negated | | |
287+---------------------+---------------------------------+---------+--------------------+
288| ``+x`` | *x* unchanged | | |
289+---------------------+---------------------------------+---------+--------------------+
290| ``abs(x)`` | absolute value or magnitude of | | :func:`abs` |
291| | *x* | | |
292+---------------------+---------------------------------+---------+--------------------+
293| ``int(x)`` | *x* converted to integer | \(3)\(6)| :func:`int` |
294+---------------------+---------------------------------+---------+--------------------+
295| ``float(x)`` | *x* converted to floating point | \(4)\(6)| :func:`float` |
296+---------------------+---------------------------------+---------+--------------------+
297| ``complex(re, im)`` | a complex number with real part | \(6) | :func:`complex` |
298| | *re*, imaginary part *im*. | | |
299| | *im* defaults to zero. | | |
300+---------------------+---------------------------------+---------+--------------------+
301| ``c.conjugate()`` | conjugate of the complex number | | |
302| | *c* | | |
303+---------------------+---------------------------------+---------+--------------------+
304| ``divmod(x, y)`` | the pair ``(x // y, x % y)`` | \(2) | :func:`divmod` |
305+---------------------+---------------------------------+---------+--------------------+
306| ``pow(x, y)`` | *x* to the power *y* | \(5) | :func:`pow` |
307+---------------------+---------------------------------+---------+--------------------+
308| ``x ** y`` | *x* to the power *y* | \(5) | |
309+---------------------+---------------------------------+---------+--------------------+
Georg Brandl116aa622007-08-15 14:28:22 +0000310
311.. index::
312 triple: operations on; numeric; types
313 single: conjugate() (complex number method)
314
315Notes:
316
317(1)
Georg Brandl905ec322007-09-28 13:39:25 +0000318 Also referred to as integer division. The resultant value is a whole
319 integer, though the result's type is not necessarily int. The result is
320 always rounded towards minus infinity: ``1//2`` is ``0``, ``(-1)//2`` is
321 ``-1``, ``1//(-2)`` is ``-1``, and ``(-1)//(-2)`` is ``0``.
Georg Brandl116aa622007-08-15 14:28:22 +0000322
323(2)
Georg Brandl905ec322007-09-28 13:39:25 +0000324 Not for complex numbers. Instead convert to floats using :func:`abs` if
325 appropriate.
326
327(3)
Georg Brandl116aa622007-08-15 14:28:22 +0000328 .. index::
329 module: math
330 single: floor() (in module math)
331 single: ceil() (in module math)
Benjamin Peterson28d88b42009-01-09 03:03:23 +0000332 single: trunc() (in module math)
Georg Brandl116aa622007-08-15 14:28:22 +0000333 pair: numeric; conversions
334 pair: C; language
335
Georg Brandlba956ae2007-11-29 17:24:34 +0000336 Conversion from floating point to integer may round or truncate
Serhiy Storchaka0d196ed2013-10-09 14:02:31 +0300337 as in C; see functions :func:`math.floor` and :func:`math.ceil` for
338 well-defined conversions.
Georg Brandl116aa622007-08-15 14:28:22 +0000339
Georg Brandl74f36692008-01-06 17:39:49 +0000340(4)
Georg Brandl48310cd2009-01-03 21:18:54 +0000341 float also accepts the strings "nan" and "inf" with an optional prefix "+"
Christian Heimes99170a52007-12-19 02:07:34 +0000342 or "-" for Not a Number (NaN) and positive or negative infinity.
Christian Heimes7f044312008-01-06 17:05:40 +0000343
Georg Brandl74f36692008-01-06 17:39:49 +0000344(5)
Christian Heimes7f044312008-01-06 17:05:40 +0000345 Python defines ``pow(0, 0)`` and ``0 ** 0`` to be ``1``, as is common for
346 programming languages.
347
Raymond Hettingerc706dbf2011-03-22 17:33:17 -0700348(6)
349 The numeric literals accepted include the digits ``0`` to ``9`` or any
350 Unicode equivalent (code points with the ``Nd`` property).
351
Benjamin Peterson279a9622017-06-22 22:31:08 -0700352 See http://www.unicode.org/Public/10.0.0/ucd/extracted/DerivedNumericType.txt
Raymond Hettingerc706dbf2011-03-22 17:33:17 -0700353 for a complete list of code points with the ``Nd`` property.
Georg Brandl48310cd2009-01-03 21:18:54 +0000354
Christian Heimes5b5e81c2007-12-31 16:14:33 +0000355
Benjamin Peterson10116d42011-05-01 17:38:17 -0500356All :class:`numbers.Real` types (:class:`int` and :class:`float`) also include
357the following operations:
Christian Heimesfaf2f632008-01-06 16:59:19 +0000358
Martin Panter129fe042016-05-08 12:22:37 +0000359+--------------------+---------------------------------------------+
360| Operation | Result |
361+====================+=============================================+
362| :func:`math.trunc(\| *x* truncated to :class:`~numbers.Integral` |
363| x) <math.trunc>` | |
364+--------------------+---------------------------------------------+
365| :func:`round(x[, | *x* rounded to *n* digits, |
366| n]) <round>` | rounding half to even. If *n* is |
367| | omitted, it defaults to 0. |
368+--------------------+---------------------------------------------+
369| :func:`math.floor(\| the greatest :class:`~numbers.Integral` |
370| x) <math.floor>` | <= *x* |
371+--------------------+---------------------------------------------+
372| :func:`math.ceil(x)| the least :class:`~numbers.Integral` >= *x* |
373| <math.ceil>` | |
374+--------------------+---------------------------------------------+
Christian Heimesfaf2f632008-01-06 16:59:19 +0000375
Mark Summerfieldbbfd71d2008-07-01 15:50:04 +0000376For additional numeric operations see the :mod:`math` and :mod:`cmath`
377modules.
378
Christian Heimes5b5e81c2007-12-31 16:14:33 +0000379.. XXXJH exceptions: overflow (when? what operations?) zerodivision
Georg Brandl116aa622007-08-15 14:28:22 +0000380
381
382.. _bitstring-ops:
383
Benjamin Petersone9fca252012-01-25 16:29:03 -0500384Bitwise Operations on Integer Types
Sanyam Khuranab4bc5ca2018-07-28 10:45:50 +0530385-----------------------------------
Georg Brandl116aa622007-08-15 14:28:22 +0000386
Alexandre Vassalotti6d3dfc32009-07-29 19:54:39 +0000387.. index::
388 triple: operations on; integer; types
Benjamin Petersone9fca252012-01-25 16:29:03 -0500389 pair: bitwise; operations
Alexandre Vassalotti6d3dfc32009-07-29 19:54:39 +0000390 pair: shifting; operations
391 pair: masking; operations
Marco Buttu5eb70752017-06-09 16:28:28 +0200392 operator: |
Alexandre Vassalotti6d3dfc32009-07-29 19:54:39 +0000393 operator: ^
394 operator: &
395 operator: <<
396 operator: >>
Marco Buttu5eb70752017-06-09 16:28:28 +0200397 operator: ~
Georg Brandl116aa622007-08-15 14:28:22 +0000398
Sanyam Khuranab4bc5ca2018-07-28 10:45:50 +0530399Bitwise operations only make sense for integers. The result of bitwise
400operations is calculated as though carried out in two's complement with an
401infinite number of sign bits.
Georg Brandl116aa622007-08-15 14:28:22 +0000402
Christian Heimesfaf2f632008-01-06 16:59:19 +0000403The priorities of the binary bitwise operations are all lower than the numeric
Georg Brandl116aa622007-08-15 14:28:22 +0000404operations and higher than the comparisons; the unary operation ``~`` has the
405same priority as the other unary numeric operations (``+`` and ``-``).
406
Georg Brandle4196d32014-10-31 09:41:46 +0100407This table lists the bitwise operations sorted in ascending priority:
Georg Brandl116aa622007-08-15 14:28:22 +0000408
409+------------+--------------------------------+----------+
410| Operation | Result | Notes |
411+============+================================+==========+
Andrés Delfino2e5d2ea2018-08-25 11:08:20 -0300412| ``x | y`` | bitwise :dfn:`or` of *x* and | \(4) |
Georg Brandl116aa622007-08-15 14:28:22 +0000413| | *y* | |
414+------------+--------------------------------+----------+
Andrés Delfino2e5d2ea2018-08-25 11:08:20 -0300415| ``x ^ y`` | bitwise :dfn:`exclusive or` of | \(4) |
Georg Brandl116aa622007-08-15 14:28:22 +0000416| | *x* and *y* | |
417+------------+--------------------------------+----------+
Andrés Delfino2e5d2ea2018-08-25 11:08:20 -0300418| ``x & y`` | bitwise :dfn:`and` of *x* and | \(4) |
Georg Brandl116aa622007-08-15 14:28:22 +0000419| | *y* | |
420+------------+--------------------------------+----------+
Christian Heimes043d6f62008-01-07 17:19:16 +0000421| ``x << n`` | *x* shifted left by *n* bits | (1)(2) |
Georg Brandl116aa622007-08-15 14:28:22 +0000422+------------+--------------------------------+----------+
Christian Heimes043d6f62008-01-07 17:19:16 +0000423| ``x >> n`` | *x* shifted right by *n* bits | (1)(3) |
Georg Brandl116aa622007-08-15 14:28:22 +0000424+------------+--------------------------------+----------+
425| ``~x`` | the bits of *x* inverted | |
426+------------+--------------------------------+----------+
427
Georg Brandl116aa622007-08-15 14:28:22 +0000428Notes:
429
430(1)
431 Negative shift counts are illegal and cause a :exc:`ValueError` to be raised.
432
433(2)
434 A left shift by *n* bits is equivalent to multiplication by ``pow(2, n)``
435 without overflow check.
436
437(3)
438 A right shift by *n* bits is equivalent to division by ``pow(2, n)`` without
439 overflow check.
440
Sanyam Khuranab4bc5ca2018-07-28 10:45:50 +0530441(4)
442 Performing these calculations with at least one extra sign extension bit in
443 a finite two's complement representation (a working bit-width of
444 ``1 + max(x.bit_length(), y.bit_length()`` or more) is sufficient to get the
445 same result as if there were an infinite number of sign bits.
446
Georg Brandl116aa622007-08-15 14:28:22 +0000447
Mark Dickinson54bc1ec2008-12-17 16:19:07 +0000448Additional Methods on Integer Types
449-----------------------------------
450
Raymond Hettinger9b2fd322011-05-01 18:14:49 -0700451The int type implements the :class:`numbers.Integral` :term:`abstract base
Georg Brandle4196d32014-10-31 09:41:46 +0100452class`. In addition, it provides a few more methods:
Benjamin Peterson10116d42011-05-01 17:38:17 -0500453
Mark Dickinson54bc1ec2008-12-17 16:19:07 +0000454.. method:: int.bit_length()
455
Raymond Hettingerd3e18b72008-12-19 09:11:49 +0000456 Return the number of bits necessary to represent an integer in binary,
457 excluding the sign and leading zeros::
Mark Dickinson54bc1ec2008-12-17 16:19:07 +0000458
Raymond Hettingerd3e18b72008-12-19 09:11:49 +0000459 >>> n = -37
Mark Dickinson54bc1ec2008-12-17 16:19:07 +0000460 >>> bin(n)
Raymond Hettingerd3e18b72008-12-19 09:11:49 +0000461 '-0b100101'
Mark Dickinson54bc1ec2008-12-17 16:19:07 +0000462 >>> n.bit_length()
463 6
Mark Dickinson54bc1ec2008-12-17 16:19:07 +0000464
Raymond Hettingerd3e18b72008-12-19 09:11:49 +0000465 More precisely, if ``x`` is nonzero, then ``x.bit_length()`` is the
466 unique positive integer ``k`` such that ``2**(k-1) <= abs(x) < 2**k``.
467 Equivalently, when ``abs(x)`` is small enough to have a correctly
468 rounded logarithm, then ``k = 1 + int(log(abs(x), 2))``.
469 If ``x`` is zero, then ``x.bit_length()`` returns ``0``.
Mark Dickinson54bc1ec2008-12-17 16:19:07 +0000470
471 Equivalent to::
472
473 def bit_length(self):
Senthil Kumaran0aae6dc2010-06-22 02:57:23 +0000474 s = bin(self) # binary representation: bin(-37) --> '-0b100101'
Raymond Hettingerd3e18b72008-12-19 09:11:49 +0000475 s = s.lstrip('-0b') # remove leading zeros and minus sign
476 return len(s) # len('100101') --> 6
Mark Dickinson54bc1ec2008-12-17 16:19:07 +0000477
478 .. versionadded:: 3.1
479
Georg Brandl67b21b72010-08-17 15:07:14 +0000480.. method:: int.to_bytes(length, byteorder, \*, signed=False)
Alexandre Vassalottic36c3782010-01-09 20:35:09 +0000481
482 Return an array of bytes representing an integer.
483
484 >>> (1024).to_bytes(2, byteorder='big')
485 b'\x04\x00'
486 >>> (1024).to_bytes(10, byteorder='big')
487 b'\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00'
488 >>> (-1024).to_bytes(10, byteorder='big', signed=True)
489 b'\xff\xff\xff\xff\xff\xff\xff\xff\xfc\x00'
490 >>> x = 1000
INADA Naoki3e3e9f32016-10-31 17:41:47 +0900491 >>> x.to_bytes((x.bit_length() + 7) // 8, byteorder='little')
Alexandre Vassalottic36c3782010-01-09 20:35:09 +0000492 b'\xe8\x03'
493
494 The integer is represented using *length* bytes. An :exc:`OverflowError`
495 is raised if the integer is not representable with the given number of
496 bytes.
497
498 The *byteorder* argument determines the byte order used to represent the
499 integer. If *byteorder* is ``"big"``, the most significant byte is at the
500 beginning of the byte array. If *byteorder* is ``"little"``, the most
501 significant byte is at the end of the byte array. To request the native
502 byte order of the host system, use :data:`sys.byteorder` as the byte order
503 value.
504
505 The *signed* argument determines whether two's complement is used to
506 represent the integer. If *signed* is ``False`` and a negative integer is
507 given, an :exc:`OverflowError` is raised. The default value for *signed*
508 is ``False``.
509
510 .. versionadded:: 3.2
511
Georg Brandl67b21b72010-08-17 15:07:14 +0000512.. classmethod:: int.from_bytes(bytes, byteorder, \*, signed=False)
Alexandre Vassalottic36c3782010-01-09 20:35:09 +0000513
514 Return the integer represented by the given array of bytes.
515
516 >>> int.from_bytes(b'\x00\x10', byteorder='big')
517 16
518 >>> int.from_bytes(b'\x00\x10', byteorder='little')
519 4096
520 >>> int.from_bytes(b'\xfc\x00', byteorder='big', signed=True)
521 -1024
522 >>> int.from_bytes(b'\xfc\x00', byteorder='big', signed=False)
523 64512
524 >>> int.from_bytes([255, 0, 0], byteorder='big')
525 16711680
526
Ezio Melottic228e962013-05-04 18:06:34 +0300527 The argument *bytes* must either be a :term:`bytes-like object` or an
528 iterable producing bytes.
Alexandre Vassalottic36c3782010-01-09 20:35:09 +0000529
530 The *byteorder* argument determines the byte order used to represent the
531 integer. If *byteorder* is ``"big"``, the most significant byte is at the
532 beginning of the byte array. If *byteorder* is ``"little"``, the most
533 significant byte is at the end of the byte array. To request the native
534 byte order of the host system, use :data:`sys.byteorder` as the byte order
535 value.
536
537 The *signed* argument indicates whether two's complement is used to
538 represent the integer.
539
540 .. versionadded:: 3.2
541
Mark Dickinson54bc1ec2008-12-17 16:19:07 +0000542
Mark Dickinson65fe25e2008-07-16 11:30:51 +0000543Additional Methods on Float
544---------------------------
545
Benjamin Peterson10116d42011-05-01 17:38:17 -0500546The float type implements the :class:`numbers.Real` :term:`abstract base
547class`. float also has the following additional methods.
Benjamin Petersond7b03282008-09-13 15:58:53 +0000548
549.. method:: float.as_integer_ratio()
550
Mark Dickinson4a3c7c42010-11-07 12:48:18 +0000551 Return a pair of integers whose ratio is exactly equal to the
552 original float and with a positive denominator. Raises
553 :exc:`OverflowError` on infinities and a :exc:`ValueError` on
554 NaNs.
555
556.. method:: float.is_integer()
557
558 Return ``True`` if the float instance is finite with integral
559 value, and ``False`` otherwise::
560
561 >>> (-2.0).is_integer()
562 True
563 >>> (3.2).is_integer()
564 False
Georg Brandl48310cd2009-01-03 21:18:54 +0000565
Benjamin Petersond7b03282008-09-13 15:58:53 +0000566Two methods support conversion to
Mark Dickinson65fe25e2008-07-16 11:30:51 +0000567and from hexadecimal strings. Since Python's floats are stored
568internally as binary numbers, converting a float to or from a
569*decimal* string usually involves a small rounding error. In
570contrast, hexadecimal strings allow exact representation and
571specification of floating-point numbers. This can be useful when
572debugging, and in numerical work.
573
574
575.. method:: float.hex()
576
577 Return a representation of a floating-point number as a hexadecimal
578 string. For finite floating-point numbers, this representation
579 will always include a leading ``0x`` and a trailing ``p`` and
580 exponent.
581
582
Georg Brandlabc38772009-04-12 15:51:51 +0000583.. classmethod:: float.fromhex(s)
Mark Dickinson65fe25e2008-07-16 11:30:51 +0000584
585 Class method to return the float represented by a hexadecimal
586 string *s*. The string *s* may have leading and trailing
587 whitespace.
588
589
590Note that :meth:`float.hex` is an instance method, while
591:meth:`float.fromhex` is a class method.
592
593A hexadecimal string takes the form::
594
595 [sign] ['0x'] integer ['.' fraction] ['p' exponent]
596
597where the optional ``sign`` may by either ``+`` or ``-``, ``integer``
598and ``fraction`` are strings of hexadecimal digits, and ``exponent``
599is a decimal integer with an optional leading sign. Case is not
600significant, and there must be at least one hexadecimal digit in
601either the integer or the fraction. This syntax is similar to the
602syntax specified in section 6.4.4.2 of the C99 standard, and also to
603the syntax used in Java 1.5 onwards. In particular, the output of
604:meth:`float.hex` is usable as a hexadecimal floating-point literal in
605C or Java code, and hexadecimal strings produced by C's ``%a`` format
606character or Java's ``Double.toHexString`` are accepted by
607:meth:`float.fromhex`.
608
609
610Note that the exponent is written in decimal rather than hexadecimal,
611and that it gives the power of 2 by which to multiply the coefficient.
612For example, the hexadecimal string ``0x3.a7p10`` represents the
613floating-point number ``(3 + 10./16 + 7./16**2) * 2.0**10``, or
614``3740.0``::
615
616 >>> float.fromhex('0x3.a7p10')
617 3740.0
618
619
620Applying the reverse conversion to ``3740.0`` gives a different
621hexadecimal string representing the same number::
622
623 >>> float.hex(3740.0)
624 '0x1.d380000000000p+11'
625
626
Mark Dickinsondc787d22010-05-23 13:33:13 +0000627.. _numeric-hash:
628
629Hashing of numeric types
630------------------------
631
632For numbers ``x`` and ``y``, possibly of different types, it's a requirement
633that ``hash(x) == hash(y)`` whenever ``x == y`` (see the :meth:`__hash__`
634method documentation for more details). For ease of implementation and
635efficiency across a variety of numeric types (including :class:`int`,
636:class:`float`, :class:`decimal.Decimal` and :class:`fractions.Fraction`)
637Python's hash for numeric types is based on a single mathematical function
638that's defined for any rational number, and hence applies to all instances of
Serhiy Storchaka0d196ed2013-10-09 14:02:31 +0300639:class:`int` and :class:`fractions.Fraction`, and all finite instances of
Mark Dickinsondc787d22010-05-23 13:33:13 +0000640:class:`float` and :class:`decimal.Decimal`. Essentially, this function is
641given by reduction modulo ``P`` for a fixed prime ``P``. The value of ``P`` is
642made available to Python as the :attr:`modulus` attribute of
643:data:`sys.hash_info`.
644
645.. impl-detail::
646
647 Currently, the prime used is ``P = 2**31 - 1`` on machines with 32-bit C
648 longs and ``P = 2**61 - 1`` on machines with 64-bit C longs.
649
650Here are the rules in detail:
651
Georg Brandl226ed7e2012-03-24 08:12:41 +0100652- If ``x = m / n`` is a nonnegative rational number and ``n`` is not divisible
653 by ``P``, define ``hash(x)`` as ``m * invmod(n, P) % P``, where ``invmod(n,
654 P)`` gives the inverse of ``n`` modulo ``P``.
Mark Dickinsondc787d22010-05-23 13:33:13 +0000655
Georg Brandl226ed7e2012-03-24 08:12:41 +0100656- If ``x = m / n`` is a nonnegative rational number and ``n`` is
657 divisible by ``P`` (but ``m`` is not) then ``n`` has no inverse
658 modulo ``P`` and the rule above doesn't apply; in this case define
659 ``hash(x)`` to be the constant value ``sys.hash_info.inf``.
Mark Dickinsondc787d22010-05-23 13:33:13 +0000660
Georg Brandl226ed7e2012-03-24 08:12:41 +0100661- If ``x = m / n`` is a negative rational number define ``hash(x)``
662 as ``-hash(-x)``. If the resulting hash is ``-1``, replace it with
663 ``-2``.
Mark Dickinsondc787d22010-05-23 13:33:13 +0000664
Georg Brandl226ed7e2012-03-24 08:12:41 +0100665- The particular values ``sys.hash_info.inf``, ``-sys.hash_info.inf``
666 and ``sys.hash_info.nan`` are used as hash values for positive
667 infinity, negative infinity, or nans (respectively). (All hashable
668 nans have the same hash value.)
Mark Dickinsondc787d22010-05-23 13:33:13 +0000669
Georg Brandl226ed7e2012-03-24 08:12:41 +0100670- For a :class:`complex` number ``z``, the hash values of the real
671 and imaginary parts are combined by computing ``hash(z.real) +
672 sys.hash_info.imag * hash(z.imag)``, reduced modulo
673 ``2**sys.hash_info.width`` so that it lies in
674 ``range(-2**(sys.hash_info.width - 1), 2**(sys.hash_info.width -
675 1))``. Again, if the result is ``-1``, it's replaced with ``-2``.
Mark Dickinsondc787d22010-05-23 13:33:13 +0000676
677
678To clarify the above rules, here's some example Python code,
Nick Coghlan273069c2012-08-20 17:14:07 +1000679equivalent to the built-in hash, for computing the hash of a rational
Mark Dickinsondc787d22010-05-23 13:33:13 +0000680number, :class:`float`, or :class:`complex`::
681
682
683 import sys, math
684
685 def hash_fraction(m, n):
686 """Compute the hash of a rational number m / n.
687
688 Assumes m and n are integers, with n positive.
689 Equivalent to hash(fractions.Fraction(m, n)).
690
691 """
692 P = sys.hash_info.modulus
693 # Remove common factors of P. (Unnecessary if m and n already coprime.)
694 while m % P == n % P == 0:
695 m, n = m // P, n // P
696
697 if n % P == 0:
Berker Peksagaa46bd42016-07-25 04:55:51 +0300698 hash_value = sys.hash_info.inf
Mark Dickinsondc787d22010-05-23 13:33:13 +0000699 else:
700 # Fermat's Little Theorem: pow(n, P-1, P) is 1, so
701 # pow(n, P-2, P) gives the inverse of n modulo P.
Berker Peksagaa46bd42016-07-25 04:55:51 +0300702 hash_value = (abs(m) % P) * pow(n, P - 2, P) % P
Mark Dickinsondc787d22010-05-23 13:33:13 +0000703 if m < 0:
Berker Peksagaa46bd42016-07-25 04:55:51 +0300704 hash_value = -hash_value
705 if hash_value == -1:
706 hash_value = -2
707 return hash_value
Mark Dickinsondc787d22010-05-23 13:33:13 +0000708
709 def hash_float(x):
710 """Compute the hash of a float x."""
711
712 if math.isnan(x):
713 return sys.hash_info.nan
714 elif math.isinf(x):
715 return sys.hash_info.inf if x > 0 else -sys.hash_info.inf
716 else:
717 return hash_fraction(*x.as_integer_ratio())
718
719 def hash_complex(z):
720 """Compute the hash of a complex number z."""
721
Berker Peksagaa46bd42016-07-25 04:55:51 +0300722 hash_value = hash_float(z.real) + sys.hash_info.imag * hash_float(z.imag)
Mark Dickinsondc787d22010-05-23 13:33:13 +0000723 # do a signed reduction modulo 2**sys.hash_info.width
724 M = 2**(sys.hash_info.width - 1)
Berker Peksagaa46bd42016-07-25 04:55:51 +0300725 hash_value = (hash_value & (M - 1)) - (hash_value & M)
726 if hash_value == -1:
727 hash_value = -2
728 return hash_value
Mark Dickinsondc787d22010-05-23 13:33:13 +0000729
Georg Brandl6ea420b2008-07-16 12:58:29 +0000730.. _typeiter:
731
Georg Brandl116aa622007-08-15 14:28:22 +0000732Iterator Types
733==============
734
Georg Brandl116aa622007-08-15 14:28:22 +0000735.. index::
736 single: iterator protocol
737 single: protocol; iterator
738 single: sequence; iteration
739 single: container; iteration over
740
741Python supports a concept of iteration over containers. This is implemented
742using two distinct methods; these are used to allow user-defined classes to
743support iteration. Sequences, described below in more detail, always support
744the iteration methods.
745
746One method needs to be defined for container objects to provide iteration
747support:
748
Christian Heimes790c8232008-01-07 21:14:23 +0000749.. XXX duplicated in reference/datamodel!
Georg Brandl116aa622007-08-15 14:28:22 +0000750
Christian Heimes790c8232008-01-07 21:14:23 +0000751.. method:: container.__iter__()
Georg Brandl116aa622007-08-15 14:28:22 +0000752
753 Return an iterator object. The object is required to support the iterator
754 protocol described below. If a container supports different types of
755 iteration, additional methods can be provided to specifically request
756 iterators for those iteration types. (An example of an object supporting
757 multiple forms of iteration would be a tree structure which supports both
758 breadth-first and depth-first traversal.) This method corresponds to the
Antoine Pitrou39668f52013-08-01 21:12:45 +0200759 :c:member:`~PyTypeObject.tp_iter` slot of the type structure for Python objects in the Python/C
Georg Brandl116aa622007-08-15 14:28:22 +0000760 API.
761
762The iterator objects themselves are required to support the following two
763methods, which together form the :dfn:`iterator protocol`:
764
765
766.. method:: iterator.__iter__()
767
768 Return the iterator object itself. This is required to allow both containers
769 and iterators to be used with the :keyword:`for` and :keyword:`in` statements.
Antoine Pitrou39668f52013-08-01 21:12:45 +0200770 This method corresponds to the :c:member:`~PyTypeObject.tp_iter` slot of the type structure for
Georg Brandl116aa622007-08-15 14:28:22 +0000771 Python objects in the Python/C API.
772
773
Georg Brandl905ec322007-09-28 13:39:25 +0000774.. method:: iterator.__next__()
Georg Brandl116aa622007-08-15 14:28:22 +0000775
776 Return the next item from the container. If there are no further items, raise
777 the :exc:`StopIteration` exception. This method corresponds to the
Antoine Pitrou39668f52013-08-01 21:12:45 +0200778 :c:member:`~PyTypeObject.tp_iternext` slot of the type structure for Python objects in the
Georg Brandl116aa622007-08-15 14:28:22 +0000779 Python/C API.
780
781Python defines several iterator objects to support iteration over general and
782specific sequence types, dictionaries, and other more specialized forms. The
783specific types are not important beyond their implementation of the iterator
784protocol.
785
Ezio Melotti7fa82222012-10-12 13:42:08 +0300786Once an iterator's :meth:`~iterator.__next__` method raises
787:exc:`StopIteration`, it must continue to do so on subsequent calls.
788Implementations that do not obey this property are deemed broken.
Georg Brandl116aa622007-08-15 14:28:22 +0000789
Benjamin Peterson0289b152009-06-28 17:22:03 +0000790
791.. _generator-types:
792
793Generator Types
794---------------
795
Georg Brandl9afde1c2007-11-01 20:32:30 +0000796Python's :term:`generator`\s provide a convenient way to implement the iterator
797protocol. If a container object's :meth:`__iter__` method is implemented as a
798generator, it will automatically return an iterator object (technically, a
Ezio Melotti7fa82222012-10-12 13:42:08 +0300799generator object) supplying the :meth:`__iter__` and :meth:`~generator.__next__`
800methods.
Benjamin Peterson0289b152009-06-28 17:22:03 +0000801More information about generators can be found in :ref:`the documentation for
802the yield expression <yieldexpr>`.
Georg Brandl116aa622007-08-15 14:28:22 +0000803
804
805.. _typesseq:
806
Nick Coghlan273069c2012-08-20 17:14:07 +1000807Sequence Types --- :class:`list`, :class:`tuple`, :class:`range`
808================================================================
Georg Brandl116aa622007-08-15 14:28:22 +0000809
Nick Coghlan273069c2012-08-20 17:14:07 +1000810There are three basic sequence types: lists, tuples, and range objects.
811Additional sequence types tailored for processing of
812:ref:`binary data <binaryseq>` and :ref:`text strings <textseq>` are
813described in dedicated sections.
Georg Brandle17d5862009-01-18 10:40:25 +0000814
Georg Brandl116aa622007-08-15 14:28:22 +0000815
Nick Coghlan273069c2012-08-20 17:14:07 +1000816.. _typesseq-common:
Georg Brandl116aa622007-08-15 14:28:22 +0000817
Nick Coghlan273069c2012-08-20 17:14:07 +1000818Common Sequence Operations
819--------------------------
Georg Brandl7c676132007-10-23 18:17:00 +0000820
Nick Coghlan273069c2012-08-20 17:14:07 +1000821.. index:: object: sequence
Georg Brandl4b491312007-08-31 09:22:56 +0000822
Nick Coghlan273069c2012-08-20 17:14:07 +1000823The operations in the following table are supported by most sequence types,
824both mutable and immutable. The :class:`collections.abc.Sequence` ABC is
825provided to make it easier to correctly implement these operations on
826custom sequence types.
Georg Brandl116aa622007-08-15 14:28:22 +0000827
Georg Brandle4196d32014-10-31 09:41:46 +0100828This table lists the sequence operations sorted in ascending priority. In the
829table, *s* and *t* are sequences of the same type, *n*, *i*, *j* and *k* are
830integers and *x* is an arbitrary object that meets any type and value
831restrictions imposed by *s*.
Georg Brandl116aa622007-08-15 14:28:22 +0000832
Nick Coghlan273069c2012-08-20 17:14:07 +1000833The ``in`` and ``not in`` operations have the same priorities as the
834comparison operations. The ``+`` (concatenation) and ``*`` (repetition)
Serhiy Storchakad97b7dc2017-05-16 23:18:09 +0300835operations have the same priority as the corresponding numeric operations. [3]_
Georg Brandl116aa622007-08-15 14:28:22 +0000836
Nick Coghlan83c0ae52012-08-21 17:42:52 +1000837.. index::
838 triple: operations on; sequence; types
839 builtin: len
840 builtin: min
841 builtin: max
842 pair: concatenation; operation
843 pair: repetition; operation
844 pair: subscript; operation
845 pair: slice; operation
846 operator: in
847 operator: not in
848 single: count() (sequence method)
849 single: index() (sequence method)
850
Nick Coghlan273069c2012-08-20 17:14:07 +1000851+--------------------------+--------------------------------+----------+
852| Operation | Result | Notes |
853+==========================+================================+==========+
854| ``x in s`` | ``True`` if an item of *s* is | \(1) |
855| | equal to *x*, else ``False`` | |
856+--------------------------+--------------------------------+----------+
857| ``x not in s`` | ``False`` if an item of *s* is | \(1) |
858| | equal to *x*, else ``True`` | |
859+--------------------------+--------------------------------+----------+
860| ``s + t`` | the concatenation of *s* and | (6)(7) |
861| | *t* | |
862+--------------------------+--------------------------------+----------+
Martin Panter7f02d6d2015-09-07 02:08:55 +0000863| ``s * n`` or | equivalent to adding *s* to | (2)(7) |
864| ``n * s`` | itself *n* times | |
Nick Coghlan273069c2012-08-20 17:14:07 +1000865+--------------------------+--------------------------------+----------+
866| ``s[i]`` | *i*\ th item of *s*, origin 0 | \(3) |
867+--------------------------+--------------------------------+----------+
868| ``s[i:j]`` | slice of *s* from *i* to *j* | (3)(4) |
869+--------------------------+--------------------------------+----------+
870| ``s[i:j:k]`` | slice of *s* from *i* to *j* | (3)(5) |
871| | with step *k* | |
872+--------------------------+--------------------------------+----------+
873| ``len(s)`` | length of *s* | |
874+--------------------------+--------------------------------+----------+
875| ``min(s)`` | smallest item of *s* | |
876+--------------------------+--------------------------------+----------+
877| ``max(s)`` | largest item of *s* | |
878+--------------------------+--------------------------------+----------+
Ned Deily0995c472013-07-14 12:43:16 -0700879| ``s.index(x[, i[, j]])`` | index of the first occurrence | \(8) |
Nick Coghlan273069c2012-08-20 17:14:07 +1000880| | of *x* in *s* (at or after | |
881| | index *i* and before index *j*)| |
882+--------------------------+--------------------------------+----------+
Ned Deily0995c472013-07-14 12:43:16 -0700883| ``s.count(x)`` | total number of occurrences of | |
Nick Coghlan273069c2012-08-20 17:14:07 +1000884| | *x* in *s* | |
885+--------------------------+--------------------------------+----------+
886
887Sequences of the same type also support comparisons. In particular, tuples
888and lists are compared lexicographically by comparing corresponding elements.
889This means that to compare equal, every element must compare equal and the
890two sequences must be of the same type and have the same length. (For full
891details see :ref:`comparisons` in the language reference.)
Georg Brandl116aa622007-08-15 14:28:22 +0000892
Georg Brandl116aa622007-08-15 14:28:22 +0000893Notes:
894
895(1)
Nick Coghlan273069c2012-08-20 17:14:07 +1000896 While the ``in`` and ``not in`` operations are used only for simple
897 containment testing in the general case, some specialised sequences
898 (such as :class:`str`, :class:`bytes` and :class:`bytearray`) also use
899 them for subsequence testing::
900
901 >>> "gg" in "eggs"
902 True
Georg Brandl116aa622007-08-15 14:28:22 +0000903
904(2)
905 Values of *n* less than ``0`` are treated as ``0`` (which yields an empty
Martin Panter7f02d6d2015-09-07 02:08:55 +0000906 sequence of the same type as *s*). Note that items in the sequence *s*
907 are not copied; they are referenced multiple times. This often haunts
908 new Python programmers; consider::
Georg Brandl116aa622007-08-15 14:28:22 +0000909
910 >>> lists = [[]] * 3
911 >>> lists
912 [[], [], []]
913 >>> lists[0].append(3)
914 >>> lists
915 [[3], [3], [3]]
916
917 What has happened is that ``[[]]`` is a one-element list containing an empty
Martin Panter7f02d6d2015-09-07 02:08:55 +0000918 list, so all three elements of ``[[]] * 3`` are references to this single empty
Christian Heimesfe337bf2008-03-23 21:54:12 +0000919 list. Modifying any of the elements of ``lists`` modifies this single list.
Nick Coghlan273069c2012-08-20 17:14:07 +1000920 You can create a list of different lists this way::
Georg Brandl116aa622007-08-15 14:28:22 +0000921
922 >>> lists = [[] for i in range(3)]
923 >>> lists[0].append(3)
924 >>> lists[1].append(5)
925 >>> lists[2].append(7)
926 >>> lists
927 [[3], [5], [7]]
928
Martin Panter7f02d6d2015-09-07 02:08:55 +0000929 Further explanation is available in the FAQ entry
930 :ref:`faq-multidimensional-list`.
931
Georg Brandl116aa622007-08-15 14:28:22 +0000932(3)
Xiang Zhangcea904f2016-12-30 11:57:09 +0800933 If *i* or *j* is negative, the index is relative to the end of sequence *s*:
Georg Brandl7c676132007-10-23 18:17:00 +0000934 ``len(s) + i`` or ``len(s) + j`` is substituted. But note that ``-0`` is
935 still ``0``.
Georg Brandl116aa622007-08-15 14:28:22 +0000936
937(4)
938 The slice of *s* from *i* to *j* is defined as the sequence of items with index
939 *k* such that ``i <= k < j``. If *i* or *j* is greater than ``len(s)``, use
940 ``len(s)``. If *i* is omitted or ``None``, use ``0``. If *j* is omitted or
941 ``None``, use ``len(s)``. If *i* is greater than or equal to *j*, the slice is
942 empty.
943
944(5)
945 The slice of *s* from *i* to *j* with step *k* is defined as the sequence of
Christian Heimes2c181612007-12-17 20:04:13 +0000946 items with index ``x = i + n*k`` such that ``0 <= n < (j-i)/k``. In other words,
Georg Brandl116aa622007-08-15 14:28:22 +0000947 the indices are ``i``, ``i+k``, ``i+2*k``, ``i+3*k`` and so on, stopping when
Martin Panter3dbd87f2016-12-24 08:25:15 +0000948 *j* is reached (but never including *j*). When *k* is positive,
949 *i* and *j* are reduced to ``len(s)`` if they are greater.
950 When *k* is negative, *i* and *j* are reduced to ``len(s) - 1`` if
951 they are greater. If *i* or *j* are omitted or ``None``, they become
Georg Brandl116aa622007-08-15 14:28:22 +0000952 "end" values (which end depends on the sign of *k*). Note, *k* cannot be zero.
953 If *k* is ``None``, it is treated like ``1``.
954
955(6)
Nick Coghlan273069c2012-08-20 17:14:07 +1000956 Concatenating immutable sequences always results in a new object. This
957 means that building up a sequence by repeated concatenation will have a
958 quadratic runtime cost in the total sequence length. To get a linear
959 runtime cost, you must switch to one of the alternatives below:
Georg Brandl495f7b52009-10-27 15:28:25 +0000960
Antoine Pitroufd9ebd42011-11-25 16:33:53 +0100961 * if concatenating :class:`str` objects, you can build a list and use
Martin Panter7462b6492015-11-02 03:37:02 +0000962 :meth:`str.join` at the end or else write to an :class:`io.StringIO`
Nick Coghlan83c0ae52012-08-21 17:42:52 +1000963 instance and retrieve its value when complete
Antoine Pitroufd9ebd42011-11-25 16:33:53 +0100964
965 * if concatenating :class:`bytes` objects, you can similarly use
Nick Coghlan273069c2012-08-20 17:14:07 +1000966 :meth:`bytes.join` or :class:`io.BytesIO`, or you can do in-place
967 concatenation with a :class:`bytearray` object. :class:`bytearray`
Nick Coghlan83c0ae52012-08-21 17:42:52 +1000968 objects are mutable and have an efficient overallocation mechanism
Georg Brandl116aa622007-08-15 14:28:22 +0000969
Nick Coghlan83c0ae52012-08-21 17:42:52 +1000970 * if concatenating :class:`tuple` objects, extend a :class:`list` instead
Nick Coghlan273069c2012-08-20 17:14:07 +1000971
972 * for other types, investigate the relevant class documentation
973
974
975(7)
976 Some sequence types (such as :class:`range`) only support item sequences
977 that follow specific patterns, and hence don't support sequence
978 concatenation or repetition.
979
980(8)
981 ``index`` raises :exc:`ValueError` when *x* is not found in *s*.
Nitish Chandra5ce0a2a2017-12-12 15:52:30 +0530982 Not all implementations support passing the additional arguments *i* and *j*.
983 These arguments allow efficient searching of subsections of the sequence. Passing
984 the extra arguments is roughly equivalent to using ``s[i:j].index(x)``, only
Nick Coghlan273069c2012-08-20 17:14:07 +1000985 without copying any data and with the returned index being relative to
986 the start of the sequence rather than the start of the slice.
987
988
989.. _typesseq-immutable:
990
991Immutable Sequence Types
992------------------------
993
994.. index::
995 triple: immutable; sequence; types
996 object: tuple
Nick Coghlan83c0ae52012-08-21 17:42:52 +1000997 builtin: hash
Nick Coghlan273069c2012-08-20 17:14:07 +1000998
999The only operation that immutable sequence types generally implement that is
1000not also implemented by mutable sequence types is support for the :func:`hash`
1001built-in.
1002
1003This support allows immutable sequences, such as :class:`tuple` instances, to
1004be used as :class:`dict` keys and stored in :class:`set` and :class:`frozenset`
1005instances.
1006
1007Attempting to hash an immutable sequence that contains unhashable values will
1008result in :exc:`TypeError`.
1009
1010
1011.. _typesseq-mutable:
1012
1013Mutable Sequence Types
1014----------------------
1015
1016.. index::
1017 triple: mutable; sequence; types
1018 object: list
1019 object: bytearray
1020
1021The operations in the following table are defined on mutable sequence types.
1022The :class:`collections.abc.MutableSequence` ABC is provided to make it
1023easier to correctly implement these operations on custom sequence types.
1024
1025In the table *s* is an instance of a mutable sequence type, *t* is any
1026iterable object and *x* is an arbitrary object that meets any type
1027and value restrictions imposed by *s* (for example, :class:`bytearray` only
1028accepts integers that meet the value restriction ``0 <= x <= 255``).
1029
1030
1031.. index::
1032 triple: operations on; sequence; types
1033 triple: operations on; list; type
1034 pair: subscript; assignment
1035 pair: slice; assignment
1036 statement: del
1037 single: append() (sequence method)
Nick Coghlan83c0ae52012-08-21 17:42:52 +10001038 single: clear() (sequence method)
1039 single: copy() (sequence method)
Nick Coghlan273069c2012-08-20 17:14:07 +10001040 single: extend() (sequence method)
Nick Coghlan273069c2012-08-20 17:14:07 +10001041 single: insert() (sequence method)
1042 single: pop() (sequence method)
1043 single: remove() (sequence method)
1044 single: reverse() (sequence method)
1045
1046+------------------------------+--------------------------------+---------------------+
1047| Operation | Result | Notes |
1048+==============================+================================+=====================+
1049| ``s[i] = x`` | item *i* of *s* is replaced by | |
1050| | *x* | |
1051+------------------------------+--------------------------------+---------------------+
1052| ``s[i:j] = t`` | slice of *s* from *i* to *j* | |
1053| | is replaced by the contents of | |
1054| | the iterable *t* | |
1055+------------------------------+--------------------------------+---------------------+
1056| ``del s[i:j]`` | same as ``s[i:j] = []`` | |
1057+------------------------------+--------------------------------+---------------------+
1058| ``s[i:j:k] = t`` | the elements of ``s[i:j:k]`` | \(1) |
1059| | are replaced by those of *t* | |
1060+------------------------------+--------------------------------+---------------------+
1061| ``del s[i:j:k]`` | removes the elements of | |
1062| | ``s[i:j:k]`` from the list | |
1063+------------------------------+--------------------------------+---------------------+
Nick Coghlan83c0ae52012-08-21 17:42:52 +10001064| ``s.append(x)`` | appends *x* to the end of the | |
1065| | sequence (same as | |
1066| | ``s[len(s):len(s)] = [x]``) | |
Nick Coghlan273069c2012-08-20 17:14:07 +10001067+------------------------------+--------------------------------+---------------------+
Andrés Delfino2e5d2ea2018-08-25 11:08:20 -03001068| ``s.clear()`` | removes all items from *s* | \(5) |
Nick Coghlan273069c2012-08-20 17:14:07 +10001069| | (same as ``del s[:]``) | |
1070+------------------------------+--------------------------------+---------------------+
Andrés Delfino2e5d2ea2018-08-25 11:08:20 -03001071| ``s.copy()`` | creates a shallow copy of *s* | \(5) |
Nick Coghlan273069c2012-08-20 17:14:07 +10001072| | (same as ``s[:]``) | |
1073+------------------------------+--------------------------------+---------------------+
Martin Panter3795d122015-10-03 07:46:04 +00001074| ``s.extend(t)`` or | extends *s* with the | |
1075| ``s += t`` | contents of *t* (for the | |
1076| | most part the same as | |
Nick Coghlan83c0ae52012-08-21 17:42:52 +10001077| | ``s[len(s):len(s)] = t``) | |
Nick Coghlan273069c2012-08-20 17:14:07 +10001078+------------------------------+--------------------------------+---------------------+
Martin Panter3795d122015-10-03 07:46:04 +00001079| ``s *= n`` | updates *s* with its contents | \(6) |
1080| | repeated *n* times | |
1081+------------------------------+--------------------------------+---------------------+
Nick Coghlan83c0ae52012-08-21 17:42:52 +10001082| ``s.insert(i, x)`` | inserts *x* into *s* at the | |
1083| | index given by *i* | |
1084| | (same as ``s[i:i] = [x]``) | |
Nick Coghlan273069c2012-08-20 17:14:07 +10001085+------------------------------+--------------------------------+---------------------+
Nick Coghlan83c0ae52012-08-21 17:42:52 +10001086| ``s.pop([i])`` | retrieves the item at *i* and | \(2) |
1087| | also removes it from *s* | |
Nick Coghlan273069c2012-08-20 17:14:07 +10001088+------------------------------+--------------------------------+---------------------+
Nick Coghlan83c0ae52012-08-21 17:42:52 +10001089| ``s.remove(x)`` | remove the first item from *s* | \(3) |
Xiang Zhangb2d77172017-03-13 10:09:16 +08001090| | where ``s[i]`` is equal to *x* | |
Nick Coghlan273069c2012-08-20 17:14:07 +10001091+------------------------------+--------------------------------+---------------------+
1092| ``s.reverse()`` | reverses the items of *s* in | \(4) |
1093| | place | |
1094+------------------------------+--------------------------------+---------------------+
1095
1096
1097Notes:
1098
1099(1)
1100 *t* must have the same length as the slice it is replacing.
1101
1102(2)
1103 The optional argument *i* defaults to ``-1``, so that by default the last
1104 item is removed and returned.
1105
1106(3)
1107 ``remove`` raises :exc:`ValueError` when *x* is not found in *s*.
1108
1109(4)
1110 The :meth:`reverse` method modifies the sequence in place for economy of
1111 space when reversing a large sequence. To remind users that it operates by
1112 side effect, it does not return the reversed sequence.
1113
1114(5)
1115 :meth:`clear` and :meth:`!copy` are included for consistency with the
1116 interfaces of mutable containers that don't support slicing operations
1117 (such as :class:`dict` and :class:`set`)
1118
1119 .. versionadded:: 3.3
1120 :meth:`clear` and :meth:`!copy` methods.
1121
Martin Panter3795d122015-10-03 07:46:04 +00001122(6)
1123 The value *n* is an integer, or an object implementing
1124 :meth:`~object.__index__`. Zero and negative values of *n* clear
1125 the sequence. Items in the sequence are not copied; they are referenced
1126 multiple times, as explained for ``s * n`` under :ref:`typesseq-common`.
1127
Nick Coghlan273069c2012-08-20 17:14:07 +10001128
1129.. _typesseq-list:
1130
1131Lists
1132-----
1133
1134.. index:: object: list
1135
1136Lists are mutable sequences, typically used to store collections of
1137homogeneous items (where the precise degree of similarity will vary by
1138application).
1139
Nick Coghlan83c0ae52012-08-21 17:42:52 +10001140.. class:: list([iterable])
Nick Coghlan273069c2012-08-20 17:14:07 +10001141
Nick Coghlan83c0ae52012-08-21 17:42:52 +10001142 Lists may be constructed in several ways:
Nick Coghlan273069c2012-08-20 17:14:07 +10001143
Nick Coghlan83c0ae52012-08-21 17:42:52 +10001144 * Using a pair of square brackets to denote the empty list: ``[]``
1145 * Using square brackets, separating items with commas: ``[a]``, ``[a, b, c]``
1146 * Using a list comprehension: ``[x for x in iterable]``
1147 * Using the type constructor: ``list()`` or ``list(iterable)``
Nick Coghlan273069c2012-08-20 17:14:07 +10001148
Nick Coghlan83c0ae52012-08-21 17:42:52 +10001149 The constructor builds a list whose items are the same and in the same
1150 order as *iterable*'s items. *iterable* may be either a sequence, a
1151 container that supports iteration, or an iterator object. If *iterable*
1152 is already a list, a copy is made and returned, similar to ``iterable[:]``.
1153 For example, ``list('abc')`` returns ``['a', 'b', 'c']`` and
1154 ``list( (1, 2, 3) )`` returns ``[1, 2, 3]``.
1155 If no argument is given, the constructor creates a new empty list, ``[]``.
Nick Coghlan273069c2012-08-20 17:14:07 +10001156
Nick Coghlan273069c2012-08-20 17:14:07 +10001157
Nick Coghlan83c0ae52012-08-21 17:42:52 +10001158 Many other operations also produce lists, including the :func:`sorted`
1159 built-in.
Nick Coghlan273069c2012-08-20 17:14:07 +10001160
Nick Coghlan83c0ae52012-08-21 17:42:52 +10001161 Lists implement all of the :ref:`common <typesseq-common>` and
1162 :ref:`mutable <typesseq-mutable>` sequence operations. Lists also provide the
1163 following additional method:
Nick Coghlan273069c2012-08-20 17:14:07 +10001164
Łukasz Rogalskibe37beb2017-07-14 21:23:39 +02001165 .. method:: list.sort(*, key=None, reverse=False)
Nick Coghlan273069c2012-08-20 17:14:07 +10001166
Nick Coghlan83c0ae52012-08-21 17:42:52 +10001167 This method sorts the list in place, using only ``<`` comparisons
1168 between items. Exceptions are not suppressed - if any comparison operations
1169 fail, the entire sort operation will fail (and the list will likely be left
1170 in a partially modified state).
Nick Coghlan273069c2012-08-20 17:14:07 +10001171
Zachary Waree1391a02013-11-22 13:58:34 -06001172 :meth:`sort` accepts two arguments that can only be passed by keyword
1173 (:ref:`keyword-only arguments <keyword-only_parameter>`):
1174
Nick Coghlan83c0ae52012-08-21 17:42:52 +10001175 *key* specifies a function of one argument that is used to extract a
1176 comparison key from each list element (for example, ``key=str.lower``).
1177 The key corresponding to each item in the list is calculated once and
1178 then used for the entire sorting process. The default value of ``None``
1179 means that list items are sorted directly without calculating a separate
1180 key value.
Nick Coghlan273069c2012-08-20 17:14:07 +10001181
Nick Coghlan83c0ae52012-08-21 17:42:52 +10001182 The :func:`functools.cmp_to_key` utility is available to convert a 2.x
1183 style *cmp* function to a *key* function.
Nick Coghlan273069c2012-08-20 17:14:07 +10001184
Nick Coghlan83c0ae52012-08-21 17:42:52 +10001185 *reverse* is a boolean value. If set to ``True``, then the list elements
1186 are sorted as if each comparison were reversed.
Nick Coghlan273069c2012-08-20 17:14:07 +10001187
Nick Coghlan83c0ae52012-08-21 17:42:52 +10001188 This method modifies the sequence in place for economy of space when
1189 sorting a large sequence. To remind users that it operates by side
1190 effect, it does not return the sorted sequence (use :func:`sorted` to
1191 explicitly request a new sorted list instance).
1192
1193 The :meth:`sort` method is guaranteed to be stable. A sort is stable if it
1194 guarantees not to change the relative order of elements that compare equal
1195 --- this is helpful for sorting in multiple passes (for example, sort by
1196 department, then by salary grade).
1197
1198 .. impl-detail::
1199
1200 While a list is being sorted, the effect of attempting to mutate, or even
1201 inspect, the list is undefined. The C implementation of Python makes the
1202 list appear empty for the duration, and raises :exc:`ValueError` if it can
1203 detect that the list has been mutated during a sort.
Nick Coghlan273069c2012-08-20 17:14:07 +10001204
1205
1206.. _typesseq-tuple:
1207
1208Tuples
1209------
1210
1211.. index:: object: tuple
1212
1213Tuples are immutable sequences, typically used to store collections of
1214heterogeneous data (such as the 2-tuples produced by the :func:`enumerate`
1215built-in). Tuples are also used for cases where an immutable sequence of
1216homogeneous data is needed (such as allowing storage in a :class:`set` or
1217:class:`dict` instance).
1218
Nick Coghlan83c0ae52012-08-21 17:42:52 +10001219.. class:: tuple([iterable])
Nick Coghlan273069c2012-08-20 17:14:07 +10001220
Nick Coghlan83c0ae52012-08-21 17:42:52 +10001221 Tuples may be constructed in a number of ways:
Nick Coghlan273069c2012-08-20 17:14:07 +10001222
Nick Coghlan83c0ae52012-08-21 17:42:52 +10001223 * Using a pair of parentheses to denote the empty tuple: ``()``
1224 * Using a trailing comma for a singleton tuple: ``a,`` or ``(a,)``
1225 * Separating items with commas: ``a, b, c`` or ``(a, b, c)``
1226 * Using the :func:`tuple` built-in: ``tuple()`` or ``tuple(iterable)``
Nick Coghlan273069c2012-08-20 17:14:07 +10001227
Nick Coghlan83c0ae52012-08-21 17:42:52 +10001228 The constructor builds a tuple whose items are the same and in the same
1229 order as *iterable*'s items. *iterable* may be either a sequence, a
1230 container that supports iteration, or an iterator object. If *iterable*
1231 is already a tuple, it is returned unchanged. For example,
1232 ``tuple('abc')`` returns ``('a', 'b', 'c')`` and
1233 ``tuple( [1, 2, 3] )`` returns ``(1, 2, 3)``.
1234 If no argument is given, the constructor creates a new empty tuple, ``()``.
Nick Coghlan273069c2012-08-20 17:14:07 +10001235
Nick Coghlan83c0ae52012-08-21 17:42:52 +10001236 Note that it is actually the comma which makes a tuple, not the parentheses.
1237 The parentheses are optional, except in the empty tuple case, or
1238 when they are needed to avoid syntactic ambiguity. For example,
1239 ``f(a, b, c)`` is a function call with three arguments, while
1240 ``f((a, b, c))`` is a function call with a 3-tuple as the sole argument.
1241
1242 Tuples implement all of the :ref:`common <typesseq-common>` sequence
1243 operations.
1244
1245For heterogeneous collections of data where access by name is clearer than
1246access by index, :func:`collections.namedtuple` may be a more appropriate
1247choice than a simple tuple object.
Nick Coghlan273069c2012-08-20 17:14:07 +10001248
1249
1250.. _typesseq-range:
1251
1252Ranges
1253------
1254
1255.. index:: object: range
1256
1257The :class:`range` type represents an immutable sequence of numbers and is
Nick Coghlan83c0ae52012-08-21 17:42:52 +10001258commonly used for looping a specific number of times in :keyword:`for`
1259loops.
Nick Coghlan273069c2012-08-20 17:14:07 +10001260
Ezio Melotti8429b672012-09-14 06:35:09 +03001261.. class:: range(stop)
1262 range(start, stop[, step])
Nick Coghlan273069c2012-08-20 17:14:07 +10001263
Nick Coghlan83c0ae52012-08-21 17:42:52 +10001264 The arguments to the range constructor must be integers (either built-in
1265 :class:`int` or any object that implements the ``__index__`` special
1266 method). If the *step* argument is omitted, it defaults to ``1``.
1267 If the *start* argument is omitted, it defaults to ``0``.
1268 If *step* is zero, :exc:`ValueError` is raised.
1269
1270 For a positive *step*, the contents of a range ``r`` are determined by the
1271 formula ``r[i] = start + step*i`` where ``i >= 0`` and
1272 ``r[i] < stop``.
1273
1274 For a negative *step*, the contents of the range are still determined by
1275 the formula ``r[i] = start + step*i``, but the constraints are ``i >= 0``
1276 and ``r[i] > stop``.
1277
Sandro Tosi4c1b9f42013-01-27 00:33:04 +01001278 A range object will be empty if ``r[0]`` does not meet the value
Nick Coghlan83c0ae52012-08-21 17:42:52 +10001279 constraint. Ranges do support negative indices, but these are interpreted
1280 as indexing from the end of the sequence determined by the positive
1281 indices.
1282
1283 Ranges containing absolute values larger than :data:`sys.maxsize` are
1284 permitted but some features (such as :func:`len`) may raise
1285 :exc:`OverflowError`.
1286
1287 Range examples::
1288
1289 >>> list(range(10))
1290 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
1291 >>> list(range(1, 11))
1292 [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
1293 >>> list(range(0, 30, 5))
1294 [0, 5, 10, 15, 20, 25]
1295 >>> list(range(0, 10, 3))
1296 [0, 3, 6, 9]
1297 >>> list(range(0, -10, -1))
1298 [0, -1, -2, -3, -4, -5, -6, -7, -8, -9]
1299 >>> list(range(0))
1300 []
1301 >>> list(range(1, 0))
1302 []
1303
1304 Ranges implement all of the :ref:`common <typesseq-common>` sequence operations
1305 except concatenation and repetition (due to the fact that range objects can
1306 only represent sequences that follow a strict pattern and repetition and
1307 concatenation will usually violate that pattern).
1308
Georg Brandl8c16cb92016-02-25 20:17:45 +01001309 .. attribute:: start
Nick Coghlan83c0ae52012-08-21 17:42:52 +10001310
1311 The value of the *start* parameter (or ``0`` if the parameter was
1312 not supplied)
1313
Georg Brandl8c16cb92016-02-25 20:17:45 +01001314 .. attribute:: stop
Nick Coghlan83c0ae52012-08-21 17:42:52 +10001315
1316 The value of the *stop* parameter
1317
Georg Brandl8c16cb92016-02-25 20:17:45 +01001318 .. attribute:: step
Nick Coghlan83c0ae52012-08-21 17:42:52 +10001319
1320 The value of the *step* parameter (or ``1`` if the parameter was
1321 not supplied)
Nick Coghlan273069c2012-08-20 17:14:07 +10001322
1323The advantage of the :class:`range` type over a regular :class:`list` or
1324:class:`tuple` is that a :class:`range` object will always take the same
1325(small) amount of memory, no matter the size of the range it represents (as it
1326only stores the ``start``, ``stop`` and ``step`` values, calculating individual
1327items and subranges as needed).
1328
Serhiy Storchaka0d196ed2013-10-09 14:02:31 +03001329Range objects implement the :class:`collections.abc.Sequence` ABC, and provide
Nick Coghlan83c0ae52012-08-21 17:42:52 +10001330features such as containment tests, element index lookup, slicing and
1331support for negative indices (see :ref:`typesseq`):
1332
1333 >>> r = range(0, 20, 2)
1334 >>> r
1335 range(0, 20, 2)
1336 >>> 11 in r
1337 False
1338 >>> 10 in r
1339 True
1340 >>> r.index(10)
1341 5
1342 >>> r[5]
1343 10
1344 >>> r[:5]
1345 range(0, 10, 2)
1346 >>> r[-1]
1347 18
1348
1349Testing range objects for equality with ``==`` and ``!=`` compares
1350them as sequences. That is, two range objects are considered equal if
1351they represent the same sequence of values. (Note that two range
Serhiy Storchaka0d196ed2013-10-09 14:02:31 +03001352objects that compare equal might have different :attr:`~range.start`,
1353:attr:`~range.stop` and :attr:`~range.step` attributes, for example
1354``range(0) == range(2, 1, 3)`` or ``range(0, 3, 2) == range(0, 4, 2)``.)
Nick Coghlan83c0ae52012-08-21 17:42:52 +10001355
1356.. versionchanged:: 3.2
1357 Implement the Sequence ABC.
1358 Support slicing and negative indices.
1359 Test :class:`int` objects for membership in constant time instead of
1360 iterating through all items.
1361
1362.. versionchanged:: 3.3
1363 Define '==' and '!=' to compare range objects based on the
1364 sequence of values they define (instead of comparing based on
1365 object identity).
1366
1367.. versionadded:: 3.3
Serhiy Storchaka0d196ed2013-10-09 14:02:31 +03001368 The :attr:`~range.start`, :attr:`~range.stop` and :attr:`~range.step`
1369 attributes.
Nick Coghlan273069c2012-08-20 17:14:07 +10001370
Raymond Hettingere256acc2016-09-06 16:35:34 -07001371.. seealso::
1372
1373 * The `linspace recipe <http://code.activestate.com/recipes/579000/>`_
Andrés Delfinob6efc2c2018-08-03 02:12:51 -03001374 shows how to implement a lazy version of range suitable for floating
Raymond Hettingere256acc2016-09-06 16:35:34 -07001375 point applications.
Nick Coghlan273069c2012-08-20 17:14:07 +10001376
Chris Jerdonek5fae0e52012-11-20 17:45:51 -08001377.. index::
1378 single: string; text sequence type
Chris Jerdonekbb4e9412012-11-28 01:38:40 -08001379 single: str (built-in class); (see also string)
Chris Jerdonek5fae0e52012-11-20 17:45:51 -08001380 object: string
1381
Nick Coghlan273069c2012-08-20 17:14:07 +10001382.. _textseq:
1383
1384Text Sequence Type --- :class:`str`
1385===================================
1386
Chris Jerdonek5fae0e52012-11-20 17:45:51 -08001387Textual data in Python is handled with :class:`str` objects, or :dfn:`strings`.
1388Strings are immutable
Chris Jerdonekc33899b2012-10-11 18:57:48 -07001389:ref:`sequences <typesseq>` of Unicode code points. String literals are
Nick Coghlan273069c2012-08-20 17:14:07 +10001390written in a variety of ways:
1391
1392* Single quotes: ``'allows embedded "double" quotes'``
1393* Double quotes: ``"allows embedded 'single' quotes"``.
1394* Triple quoted: ``'''Three single quotes'''``, ``"""Three double quotes"""``
1395
1396Triple quoted strings may span multiple lines - all associated whitespace will
1397be included in the string literal.
1398
1399String literals that are part of a single expression and have only whitespace
Nick Coghlan83c0ae52012-08-21 17:42:52 +10001400between them will be implicitly converted to a single string literal. That
1401is, ``("spam " "eggs") == "spam eggs"``.
Nick Coghlan273069c2012-08-20 17:14:07 +10001402
1403See :ref:`strings` for more about the various forms of string literal,
1404including supported escape sequences, and the ``r`` ("raw") prefix that
1405disables most escape sequence processing.
1406
Chris Jerdonekbb4e9412012-11-28 01:38:40 -08001407Strings may also be created from other objects using the :class:`str`
1408constructor.
Nick Coghlan273069c2012-08-20 17:14:07 +10001409
1410Since there is no separate "character" type, indexing a string produces
1411strings of length 1. That is, for a non-empty string *s*, ``s[0] == s[0:1]``.
1412
Chris Jerdonek5fae0e52012-11-20 17:45:51 -08001413.. index::
1414 object: io.StringIO
1415
Nick Coghlan273069c2012-08-20 17:14:07 +10001416There is also no mutable string type, but :meth:`str.join` or
1417:class:`io.StringIO` can be used to efficiently construct strings from
1418multiple fragments.
1419
1420.. versionchanged:: 3.3
1421 For backwards compatibility with the Python 2 series, the ``u`` prefix is
1422 once again permitted on string literals. It has no effect on the meaning
1423 of string literals and cannot be combined with the ``r`` prefix.
Georg Brandl116aa622007-08-15 14:28:22 +00001424
Chris Jerdonekbb4e9412012-11-28 01:38:40 -08001425
1426.. index::
1427 single: string; str (built-in class)
1428
1429.. class:: str(object='')
1430 str(object=b'', encoding='utf-8', errors='strict')
1431
1432 Return a :ref:`string <textseq>` version of *object*. If *object* is not
1433 provided, returns the empty string. Otherwise, the behavior of ``str()``
1434 depends on whether *encoding* or *errors* is given, as follows.
1435
1436 If neither *encoding* nor *errors* is given, ``str(object)`` returns
1437 :meth:`object.__str__() <object.__str__>`, which is the "informal" or nicely
1438 printable string representation of *object*. For string objects, this is
1439 the string itself. If *object* does not have a :meth:`~object.__str__`
1440 method, then :func:`str` falls back to returning
1441 :meth:`repr(object) <repr>`.
1442
1443 .. index::
1444 single: buffer protocol; str (built-in class)
1445 single: bytes; str (built-in class)
1446
1447 If at least one of *encoding* or *errors* is given, *object* should be a
Ezio Melottic228e962013-05-04 18:06:34 +03001448 :term:`bytes-like object` (e.g. :class:`bytes` or :class:`bytearray`). In
1449 this case, if *object* is a :class:`bytes` (or :class:`bytearray`) object,
1450 then ``str(bytes, encoding, errors)`` is equivalent to
Chris Jerdonekbb4e9412012-11-28 01:38:40 -08001451 :meth:`bytes.decode(encoding, errors) <bytes.decode>`. Otherwise, the bytes
1452 object underlying the buffer object is obtained before calling
1453 :meth:`bytes.decode`. See :ref:`binaryseq` and
1454 :ref:`bufferobjects` for information on buffer objects.
1455
1456 Passing a :class:`bytes` object to :func:`str` without the *encoding*
1457 or *errors* arguments falls under the first case of returning the informal
1458 string representation (see also the :option:`-b` command-line option to
1459 Python). For example::
1460
1461 >>> str(b'Zoot!')
1462 "b'Zoot!'"
1463
1464 For more information on the ``str`` class and its methods, see
1465 :ref:`textseq` and the :ref:`string-methods` section below. To output
Martin Panterbc1ee462016-02-13 00:41:37 +00001466 formatted strings, see the :ref:`f-strings` and :ref:`formatstrings`
1467 sections. In addition, see the :ref:`stringservices` section.
Chris Jerdonekbb4e9412012-11-28 01:38:40 -08001468
1469
1470.. index::
1471 pair: string; methods
1472
Georg Brandl116aa622007-08-15 14:28:22 +00001473.. _string-methods:
1474
1475String Methods
1476--------------
1477
Nick Coghlan273069c2012-08-20 17:14:07 +10001478.. index::
Nick Coghlan273069c2012-08-20 17:14:07 +10001479 module: re
Georg Brandl116aa622007-08-15 14:28:22 +00001480
Nick Coghlan273069c2012-08-20 17:14:07 +10001481Strings implement all of the :ref:`common <typesseq-common>` sequence
1482operations, along with the additional methods described below.
Thomas Wouters8ce81f72007-09-20 18:22:40 +00001483
Nick Coghlan273069c2012-08-20 17:14:07 +10001484Strings also support two styles of string formatting, one providing a large
1485degree of flexibility and customization (see :meth:`str.format`,
1486:ref:`formatstrings` and :ref:`string-formatting`) and the other based on C
1487``printf`` style formatting that handles a narrower range of types and is
1488slightly harder to use correctly, but is often faster for the cases it can
1489handle (:ref:`old-string-formatting`).
1490
1491The :ref:`textservices` section of the standard library covers a number of
1492other modules that provide various text related utilities (including regular
1493expression support in the :mod:`re` module).
Georg Brandl116aa622007-08-15 14:28:22 +00001494
1495.. method:: str.capitalize()
1496
Senthil Kumaranfa897982010-07-05 11:41:42 +00001497 Return a copy of the string with its first character capitalized and the
Senthil Kumaran37c63a32010-07-06 02:08:36 +00001498 rest lowercased.
Georg Brandl116aa622007-08-15 14:28:22 +00001499
Georg Brandl116aa622007-08-15 14:28:22 +00001500
Benjamin Petersond5890c82012-01-14 13:23:30 -05001501.. method:: str.casefold()
1502
1503 Return a casefolded copy of the string. Casefolded strings may be used for
Benjamin Peterson94303542012-01-18 23:09:32 -05001504 caseless matching.
1505
1506 Casefolding is similar to lowercasing but more aggressive because it is
1507 intended to remove all case distinctions in a string. For example, the German
1508 lowercase letter ``'ß'`` is equivalent to ``"ss"``. Since it is already
1509 lowercase, :meth:`lower` would do nothing to ``'ß'``; :meth:`casefold`
1510 converts it to ``"ss"``.
1511
1512 The casefolding algorithm is described in section 3.13 of the Unicode
1513 Standard.
Benjamin Petersond5890c82012-01-14 13:23:30 -05001514
1515 .. versionadded:: 3.3
1516
1517
Georg Brandl116aa622007-08-15 14:28:22 +00001518.. method:: str.center(width[, fillchar])
1519
1520 Return centered in a string of length *width*. Padding is done using the
Nick Coghlane4936b82014-08-09 16:14:04 +10001521 specified *fillchar* (default is an ASCII space). The original string is
1522 returned if *width* is less than or equal to ``len(s)``.
1523
Georg Brandl116aa622007-08-15 14:28:22 +00001524
Georg Brandl116aa622007-08-15 14:28:22 +00001525
1526.. method:: str.count(sub[, start[, end]])
1527
Benjamin Petersonad3d5c22009-02-26 03:38:59 +00001528 Return the number of non-overlapping occurrences of substring *sub* in the
1529 range [*start*, *end*]. Optional arguments *start* and *end* are
1530 interpreted as in slice notation.
Georg Brandl116aa622007-08-15 14:28:22 +00001531
1532
Victor Stinnere14e2122010-11-07 18:41:46 +00001533.. method:: str.encode(encoding="utf-8", errors="strict")
Georg Brandl116aa622007-08-15 14:28:22 +00001534
Victor Stinnere14e2122010-11-07 18:41:46 +00001535 Return an encoded version of the string as a bytes object. Default encoding
1536 is ``'utf-8'``. *errors* may be given to set a different error handling scheme.
1537 The default for *errors* is ``'strict'``, meaning that encoding errors raise
1538 a :exc:`UnicodeError`. Other possible
Georg Brandl4f5f98d2009-05-04 21:01:20 +00001539 values are ``'ignore'``, ``'replace'``, ``'xmlcharrefreplace'``,
1540 ``'backslashreplace'`` and any other name registered via
Nick Coghlanb9fdb7a2015-01-07 00:22:00 +10001541 :func:`codecs.register_error`, see section :ref:`error-handlers`. For a
Georg Brandl4f5f98d2009-05-04 21:01:20 +00001542 list of possible encodings, see section :ref:`standard-encodings`.
Georg Brandl116aa622007-08-15 14:28:22 +00001543
Benjamin Peterson308d6372009-09-18 21:42:35 +00001544 .. versionchanged:: 3.1
Georg Brandl67b21b72010-08-17 15:07:14 +00001545 Support for keyword arguments added.
1546
Georg Brandl116aa622007-08-15 14:28:22 +00001547
1548.. method:: str.endswith(suffix[, start[, end]])
1549
1550 Return ``True`` if the string ends with the specified *suffix*, otherwise return
1551 ``False``. *suffix* can also be a tuple of suffixes to look for. With optional
1552 *start*, test beginning at that position. With optional *end*, stop comparing
1553 at that position.
1554
Georg Brandl116aa622007-08-15 14:28:22 +00001555
Ezio Melotti745d54d2013-11-16 19:10:57 +02001556.. method:: str.expandtabs(tabsize=8)
Georg Brandl116aa622007-08-15 14:28:22 +00001557
Ned Deilybebe91a2013-04-21 13:05:21 -07001558 Return a copy of the string where all tab characters are replaced by one or
1559 more spaces, depending on the current column and the given tab size. Tab
1560 positions occur every *tabsize* characters (default is 8, giving tab
1561 positions at columns 0, 8, 16 and so on). To expand the string, the current
1562 column is set to zero and the string is examined character by character. If
1563 the character is a tab (``\t``), one or more space characters are inserted
1564 in the result until the current column is equal to the next tab position.
1565 (The tab character itself is not copied.) If the character is a newline
1566 (``\n``) or return (``\r``), it is copied and the current column is reset to
1567 zero. Any other character is copied unchanged and the current column is
1568 incremented by one regardless of how the character is represented when
1569 printed.
1570
1571 >>> '01\t012\t0123\t01234'.expandtabs()
1572 '01 012 0123 01234'
1573 >>> '01\t012\t0123\t01234'.expandtabs(4)
1574 '01 012 0123 01234'
Georg Brandl116aa622007-08-15 14:28:22 +00001575
1576
1577.. method:: str.find(sub[, start[, end]])
1578
Senthil Kumaran114a1d62016-01-03 17:57:10 -08001579 Return the lowest index in the string where substring *sub* is found within
1580 the slice ``s[start:end]``. Optional arguments *start* and *end* are
1581 interpreted as in slice notation. Return ``-1`` if *sub* is not found.
Georg Brandl116aa622007-08-15 14:28:22 +00001582
Ezio Melotti0ed8c682011-05-09 03:54:30 +03001583 .. note::
1584
1585 The :meth:`~str.find` method should be used only if you need to know the
1586 position of *sub*. To check if *sub* is a substring or not, use the
1587 :keyword:`in` operator::
1588
1589 >>> 'Py' in 'Python'
1590 True
1591
Georg Brandl116aa622007-08-15 14:28:22 +00001592
Benjamin Petersonad3d5c22009-02-26 03:38:59 +00001593.. method:: str.format(*args, **kwargs)
Georg Brandl4b491312007-08-31 09:22:56 +00001594
Georg Brandl1f70cdf2010-03-21 09:04:24 +00001595 Perform a string formatting operation. The string on which this method is
1596 called can contain literal text or replacement fields delimited by braces
1597 ``{}``. Each replacement field contains either the numeric index of a
1598 positional argument, or the name of a keyword argument. Returns a copy of
1599 the string where each replacement field is replaced with the string value of
1600 the corresponding argument.
Georg Brandl4b491312007-08-31 09:22:56 +00001601
1602 >>> "The sum of 1 + 2 is {0}".format(1+2)
1603 'The sum of 1 + 2 is 3'
1604
1605 See :ref:`formatstrings` for a description of the various formatting options
1606 that can be specified in format strings.
1607
Victor Stinnercb064fc2018-01-15 15:58:02 +01001608 .. note::
Andrés Delfino93b56552018-08-18 14:36:24 -03001609 When formatting a number (:class:`int`, :class:`float`, :class:`complex`,
1610 :class:`decimal.Decimal` and subclasses) with the ``n`` type
1611 (ex: ``'{:n}'.format(1234)``), the function temporarily sets the
1612 ``LC_CTYPE`` locale to the ``LC_NUMERIC`` locale to decode
1613 ``decimal_point`` and ``thousands_sep`` fields of :c:func:`localeconv` if
1614 they are non-ASCII or longer than 1 byte, and the ``LC_NUMERIC`` locale is
1615 different than the ``LC_CTYPE`` locale. This temporary change affects
1616 other threads.
Victor Stinnercb064fc2018-01-15 15:58:02 +01001617
1618 .. versionchanged:: 3.7
1619 When formatting a number with the ``n`` type, the function sets
1620 temporarily the ``LC_CTYPE`` locale to the ``LC_NUMERIC`` locale in some
1621 cases.
1622
Georg Brandl4b491312007-08-31 09:22:56 +00001623
Eric Smith27bbca62010-11-04 17:06:58 +00001624.. method:: str.format_map(mapping)
1625
Éric Araujo2642ad02010-11-06 04:59:27 +00001626 Similar to ``str.format(**mapping)``, except that ``mapping`` is
Serhiy Storchakaa4d170d2013-12-23 18:20:51 +02001627 used directly and not copied to a :class:`dict`. This is useful
Eric Smith5ad85f82010-11-06 13:22:13 +00001628 if for example ``mapping`` is a dict subclass:
Eric Smith27bbca62010-11-04 17:06:58 +00001629
Eric Smith5ad85f82010-11-06 13:22:13 +00001630 >>> class Default(dict):
1631 ... def __missing__(self, key):
1632 ... return key
1633 ...
1634 >>> '{name} was born in {country}'.format_map(Default(name='Guido'))
1635 'Guido was born in country'
1636
1637 .. versionadded:: 3.2
1638
Eric Smith27bbca62010-11-04 17:06:58 +00001639
Georg Brandl116aa622007-08-15 14:28:22 +00001640.. method:: str.index(sub[, start[, end]])
1641
Nick Coghlane4936b82014-08-09 16:14:04 +10001642 Like :meth:`~str.find`, but raise :exc:`ValueError` when the substring is
1643 not found.
Georg Brandl116aa622007-08-15 14:28:22 +00001644
1645
1646.. method:: str.isalnum()
1647
1648 Return true if all characters in the string are alphanumeric and there is at
Alexander Belopolsky0d267982010-12-23 02:58:25 +00001649 least one character, false otherwise. A character ``c`` is alphanumeric if one
1650 of the following returns ``True``: ``c.isalpha()``, ``c.isdecimal()``,
1651 ``c.isdigit()``, or ``c.isnumeric()``.
Georg Brandl116aa622007-08-15 14:28:22 +00001652
Georg Brandl116aa622007-08-15 14:28:22 +00001653
1654.. method:: str.isalpha()
1655
1656 Return true if all characters in the string are alphabetic and there is at least
Alexander Belopolsky0d267982010-12-23 02:58:25 +00001657 one character, false otherwise. Alphabetic characters are those characters defined
1658 in the Unicode character database as "Letter", i.e., those with general category
1659 property being one of "Lm", "Lt", "Lu", "Ll", or "Lo". Note that this is different
1660 from the "Alphabetic" property defined in the Unicode Standard.
Georg Brandl116aa622007-08-15 14:28:22 +00001661
Georg Brandl116aa622007-08-15 14:28:22 +00001662
INADA Naokia49ac992018-01-27 14:06:21 +09001663.. method:: str.isascii()
1664
1665 Return true if the string is empty or all characters in the string are ASCII,
1666 false otherwise.
1667 ASCII characters have code points in the range U+0000-U+007F.
1668
1669 .. versionadded:: 3.7
1670
1671
Mark Summerfieldbbfd71d2008-07-01 15:50:04 +00001672.. method:: str.isdecimal()
1673
1674 Return true if all characters in the string are decimal
1675 characters and there is at least one character, false
Martin Panter49c14d82016-12-11 01:08:25 +00001676 otherwise. Decimal characters are those that can be used to form
1677 numbers in base 10, e.g. U+0660, ARABIC-INDIC DIGIT
1678 ZERO. Formally a decimal character is a character in the Unicode
1679 General Category "Nd".
Georg Brandl48310cd2009-01-03 21:18:54 +00001680
Mark Summerfieldbbfd71d2008-07-01 15:50:04 +00001681
Georg Brandl116aa622007-08-15 14:28:22 +00001682.. method:: str.isdigit()
1683
1684 Return true if all characters in the string are digits and there is at least one
Alexander Belopolsky0d267982010-12-23 02:58:25 +00001685 character, false otherwise. Digits include decimal characters and digits that need
Martin Panter49c14d82016-12-11 01:08:25 +00001686 special handling, such as the compatibility superscript digits.
1687 This covers digits which cannot be used to form numbers in base 10,
1688 like the Kharosthi numbers. Formally, a digit is a character that has the
1689 property value Numeric_Type=Digit or Numeric_Type=Decimal.
Georg Brandl116aa622007-08-15 14:28:22 +00001690
Georg Brandl116aa622007-08-15 14:28:22 +00001691
1692.. method:: str.isidentifier()
1693
1694 Return true if the string is a valid identifier according to the language
Georg Brandl4b491312007-08-31 09:22:56 +00001695 definition, section :ref:`identifiers`.
Georg Brandl116aa622007-08-15 14:28:22 +00001696
Raymond Hettinger378170d2013-03-23 08:21:12 -07001697 Use :func:`keyword.iskeyword` to test for reserved identifiers such as
1698 :keyword:`def` and :keyword:`class`.
Georg Brandl116aa622007-08-15 14:28:22 +00001699
1700.. method:: str.islower()
1701
Ezio Melotti0656a562011-08-15 14:27:19 +03001702 Return true if all cased characters [4]_ in the string are lowercase and
1703 there is at least one cased character, false otherwise.
Georg Brandl116aa622007-08-15 14:28:22 +00001704
Georg Brandl116aa622007-08-15 14:28:22 +00001705
Mark Summerfieldbbfd71d2008-07-01 15:50:04 +00001706.. method:: str.isnumeric()
1707
1708 Return true if all characters in the string are numeric
1709 characters, and there is at least one character, false
1710 otherwise. Numeric characters include digit characters, and all characters
1711 that have the Unicode numeric value property, e.g. U+2155,
Alexander Belopolsky0d267982010-12-23 02:58:25 +00001712 VULGAR FRACTION ONE FIFTH. Formally, numeric characters are those with the property
1713 value Numeric_Type=Digit, Numeric_Type=Decimal or Numeric_Type=Numeric.
Mark Summerfieldbbfd71d2008-07-01 15:50:04 +00001714
Georg Brandl48310cd2009-01-03 21:18:54 +00001715
Georg Brandl559e5d72008-06-11 18:37:52 +00001716.. method:: str.isprintable()
1717
1718 Return true if all characters in the string are printable or the string is
1719 empty, false otherwise. Nonprintable characters are those characters defined
1720 in the Unicode character database as "Other" or "Separator", excepting the
1721 ASCII space (0x20) which is considered printable. (Note that printable
1722 characters in this context are those which should not be escaped when
1723 :func:`repr` is invoked on a string. It has no bearing on the handling of
1724 strings written to :data:`sys.stdout` or :data:`sys.stderr`.)
1725
1726
Georg Brandl116aa622007-08-15 14:28:22 +00001727.. method:: str.isspace()
1728
1729 Return true if there are only whitespace characters in the string and there is
Alexander Belopolsky0d267982010-12-23 02:58:25 +00001730 at least one character, false otherwise. Whitespace characters are those
1731 characters defined in the Unicode character database as "Other" or "Separator"
1732 and those with bidirectional property being one of "WS", "B", or "S".
Georg Brandl116aa622007-08-15 14:28:22 +00001733
1734.. method:: str.istitle()
1735
1736 Return true if the string is a titlecased string and there is at least one
1737 character, for example uppercase characters may only follow uncased characters
1738 and lowercase characters only cased ones. Return false otherwise.
1739
Georg Brandl116aa622007-08-15 14:28:22 +00001740
1741.. method:: str.isupper()
1742
Ezio Melotti0656a562011-08-15 14:27:19 +03001743 Return true if all cased characters [4]_ in the string are uppercase and
1744 there is at least one cased character, false otherwise.
Georg Brandl116aa622007-08-15 14:28:22 +00001745
Georg Brandl116aa622007-08-15 14:28:22 +00001746
Georg Brandl495f7b52009-10-27 15:28:25 +00001747.. method:: str.join(iterable)
Georg Brandl116aa622007-08-15 14:28:22 +00001748
Sanyam Khurana08e2f352017-05-27 11:14:41 +05301749 Return a string which is the concatenation of the strings in *iterable*.
1750 A :exc:`TypeError` will be raised if there are any non-string values in
1751 *iterable*, including :class:`bytes` objects. The separator between
1752 elements is the string providing this method.
Georg Brandl116aa622007-08-15 14:28:22 +00001753
1754
1755.. method:: str.ljust(width[, fillchar])
1756
Nick Coghlane4936b82014-08-09 16:14:04 +10001757 Return the string left justified in a string of length *width*. Padding is
1758 done using the specified *fillchar* (default is an ASCII space). The
1759 original string is returned if *width* is less than or equal to ``len(s)``.
Georg Brandl116aa622007-08-15 14:28:22 +00001760
Georg Brandl116aa622007-08-15 14:28:22 +00001761
1762.. method:: str.lower()
1763
Ezio Melotti0656a562011-08-15 14:27:19 +03001764 Return a copy of the string with all the cased characters [4]_ converted to
1765 lowercase.
Georg Brandl116aa622007-08-15 14:28:22 +00001766
Benjamin Peterson94303542012-01-18 23:09:32 -05001767 The lowercasing algorithm used is described in section 3.13 of the Unicode
1768 Standard.
1769
Georg Brandl116aa622007-08-15 14:28:22 +00001770
1771.. method:: str.lstrip([chars])
1772
1773 Return a copy of the string with leading characters removed. The *chars*
1774 argument is a string specifying the set of characters to be removed. If omitted
1775 or ``None``, the *chars* argument defaults to removing whitespace. The *chars*
Nick Coghlane4936b82014-08-09 16:14:04 +10001776 argument is not a prefix; rather, all combinations of its values are stripped::
Georg Brandl116aa622007-08-15 14:28:22 +00001777
1778 >>> ' spacious '.lstrip()
1779 'spacious '
1780 >>> 'www.example.com'.lstrip('cmowz.')
1781 'example.com'
1782
Georg Brandl116aa622007-08-15 14:28:22 +00001783
Georg Brandlabc38772009-04-12 15:51:51 +00001784.. staticmethod:: str.maketrans(x[, y[, z]])
Georg Brandlceee0772007-11-27 23:48:05 +00001785
1786 This static method returns a translation table usable for :meth:`str.translate`.
1787
1788 If there is only one argument, it must be a dictionary mapping Unicode
1789 ordinals (integers) or characters (strings of length 1) to Unicode ordinals,
Serhiy Storchakaecf41da2016-10-19 16:29:26 +03001790 strings (of arbitrary lengths) or ``None``. Character keys will then be
Georg Brandlceee0772007-11-27 23:48:05 +00001791 converted to ordinals.
1792
1793 If there are two arguments, they must be strings of equal length, and in the
1794 resulting dictionary, each character in x will be mapped to the character at
1795 the same position in y. If there is a third argument, it must be a string,
Serhiy Storchakaecf41da2016-10-19 16:29:26 +03001796 whose characters will be mapped to ``None`` in the result.
Georg Brandlceee0772007-11-27 23:48:05 +00001797
1798
Georg Brandl116aa622007-08-15 14:28:22 +00001799.. method:: str.partition(sep)
1800
1801 Split the string at the first occurrence of *sep*, and return a 3-tuple
1802 containing the part before the separator, the separator itself, and the part
1803 after the separator. If the separator is not found, return a 3-tuple containing
1804 the string itself, followed by two empty strings.
1805
Georg Brandl116aa622007-08-15 14:28:22 +00001806
1807.. method:: str.replace(old, new[, count])
1808
1809 Return a copy of the string with all occurrences of substring *old* replaced by
1810 *new*. If the optional argument *count* is given, only the first *count*
1811 occurrences are replaced.
1812
1813
Georg Brandl226878c2007-08-31 10:15:37 +00001814.. method:: str.rfind(sub[, start[, end]])
Georg Brandl116aa622007-08-15 14:28:22 +00001815
Benjamin Petersond99cd812010-04-27 22:58:50 +00001816 Return the highest index in the string where substring *sub* is found, such
1817 that *sub* is contained within ``s[start:end]``. Optional arguments *start*
1818 and *end* are interpreted as in slice notation. Return ``-1`` on failure.
Georg Brandl116aa622007-08-15 14:28:22 +00001819
1820
1821.. method:: str.rindex(sub[, start[, end]])
1822
1823 Like :meth:`rfind` but raises :exc:`ValueError` when the substring *sub* is not
1824 found.
1825
1826
1827.. method:: str.rjust(width[, fillchar])
1828
Nick Coghlane4936b82014-08-09 16:14:04 +10001829 Return the string right justified in a string of length *width*. Padding is
1830 done using the specified *fillchar* (default is an ASCII space). The
1831 original string is returned if *width* is less than or equal to ``len(s)``.
Georg Brandl116aa622007-08-15 14:28:22 +00001832
Georg Brandl116aa622007-08-15 14:28:22 +00001833
1834.. method:: str.rpartition(sep)
1835
1836 Split the string at the last occurrence of *sep*, and return a 3-tuple
1837 containing the part before the separator, the separator itself, and the part
1838 after the separator. If the separator is not found, return a 3-tuple containing
1839 two empty strings, followed by the string itself.
1840
Georg Brandl116aa622007-08-15 14:28:22 +00001841
Ezio Melotticda6b6d2012-02-26 09:39:55 +02001842.. method:: str.rsplit(sep=None, maxsplit=-1)
Georg Brandl116aa622007-08-15 14:28:22 +00001843
1844 Return a list of the words in the string, using *sep* as the delimiter string.
1845 If *maxsplit* is given, at most *maxsplit* splits are done, the *rightmost*
1846 ones. If *sep* is not specified or ``None``, any whitespace string is a
1847 separator. Except for splitting from the right, :meth:`rsplit` behaves like
1848 :meth:`split` which is described in detail below.
1849
Georg Brandl116aa622007-08-15 14:28:22 +00001850
1851.. method:: str.rstrip([chars])
1852
1853 Return a copy of the string with trailing characters removed. The *chars*
1854 argument is a string specifying the set of characters to be removed. If omitted
1855 or ``None``, the *chars* argument defaults to removing whitespace. The *chars*
Nick Coghlane4936b82014-08-09 16:14:04 +10001856 argument is not a suffix; rather, all combinations of its values are stripped::
Georg Brandl116aa622007-08-15 14:28:22 +00001857
1858 >>> ' spacious '.rstrip()
1859 ' spacious'
1860 >>> 'mississippi'.rstrip('ipz')
1861 'mississ'
1862
Georg Brandl116aa622007-08-15 14:28:22 +00001863
Ezio Melotticda6b6d2012-02-26 09:39:55 +02001864.. method:: str.split(sep=None, maxsplit=-1)
Georg Brandl116aa622007-08-15 14:28:22 +00001865
Georg Brandl226878c2007-08-31 10:15:37 +00001866 Return a list of the words in the string, using *sep* as the delimiter
1867 string. If *maxsplit* is given, at most *maxsplit* splits are done (thus,
1868 the list will have at most ``maxsplit+1`` elements). If *maxsplit* is not
Ezio Melottibf3165b2012-05-10 15:30:42 +03001869 specified or ``-1``, then there is no limit on the number of splits
1870 (all possible splits are made).
Georg Brandl9afde1c2007-11-01 20:32:30 +00001871
Guido van Rossum2cc30da2007-11-02 23:46:40 +00001872 If *sep* is given, consecutive delimiters are not grouped together and are
Georg Brandl226878c2007-08-31 10:15:37 +00001873 deemed to delimit empty strings (for example, ``'1,,2'.split(',')`` returns
1874 ``['1', '', '2']``). The *sep* argument may consist of multiple characters
Georg Brandl9afde1c2007-11-01 20:32:30 +00001875 (for example, ``'1<>2<>3'.split('<>')`` returns ``['1', '2', '3']``).
Georg Brandl226878c2007-08-31 10:15:37 +00001876 Splitting an empty string with a specified separator returns ``['']``.
Georg Brandl116aa622007-08-15 14:28:22 +00001877
Nick Coghlane4936b82014-08-09 16:14:04 +10001878 For example::
1879
1880 >>> '1,2,3'.split(',')
1881 ['1', '2', '3']
1882 >>> '1,2,3'.split(',', maxsplit=1)
Benjamin Petersoneb83ffe2014-09-22 22:43:50 -04001883 ['1', '2,3']
Nick Coghlane4936b82014-08-09 16:14:04 +10001884 >>> '1,2,,3,'.split(',')
1885 ['1', '2', '', '3', '']
1886
Georg Brandl116aa622007-08-15 14:28:22 +00001887 If *sep* is not specified or is ``None``, a different splitting algorithm is
Georg Brandl9afde1c2007-11-01 20:32:30 +00001888 applied: runs of consecutive whitespace are regarded as a single separator,
1889 and the result will contain no empty strings at the start or end if the
1890 string has leading or trailing whitespace. Consequently, splitting an empty
1891 string or a string consisting of just whitespace with a ``None`` separator
1892 returns ``[]``.
1893
Nick Coghlane4936b82014-08-09 16:14:04 +10001894 For example::
1895
1896 >>> '1 2 3'.split()
1897 ['1', '2', '3']
1898 >>> '1 2 3'.split(maxsplit=1)
1899 ['1', '2 3']
1900 >>> ' 1 2 3 '.split()
1901 ['1', '2', '3']
Georg Brandl116aa622007-08-15 14:28:22 +00001902
1903
R David Murray1b00f252012-08-15 10:43:58 -04001904.. index::
1905 single: universal newlines; str.splitlines method
1906
Georg Brandl116aa622007-08-15 14:28:22 +00001907.. method:: str.splitlines([keepends])
1908
Benjamin Peterson8218bd42015-03-31 21:20:36 -04001909 Return a list of the lines in the string, breaking at line boundaries. Line
1910 breaks are not included in the resulting list unless *keepends* is given and
1911 true.
1912
1913 This method splits on the following line boundaries. In particular, the
1914 boundaries are a superset of :term:`universal newlines`.
1915
1916 +-----------------------+-----------------------------+
1917 | Representation | Description |
1918 +=======================+=============================+
1919 | ``\n`` | Line Feed |
1920 +-----------------------+-----------------------------+
1921 | ``\r`` | Carriage Return |
1922 +-----------------------+-----------------------------+
1923 | ``\r\n`` | Carriage Return + Line Feed |
1924 +-----------------------+-----------------------------+
1925 | ``\v`` or ``\x0b`` | Line Tabulation |
1926 +-----------------------+-----------------------------+
1927 | ``\f`` or ``\x0c`` | Form Feed |
1928 +-----------------------+-----------------------------+
1929 | ``\x1c`` | File Separator |
1930 +-----------------------+-----------------------------+
1931 | ``\x1d`` | Group Separator |
1932 +-----------------------+-----------------------------+
1933 | ``\x1e`` | Record Separator |
1934 +-----------------------+-----------------------------+
1935 | ``\x85`` | Next Line (C1 Control Code) |
1936 +-----------------------+-----------------------------+
1937 | ``\u2028`` | Line Separator |
1938 +-----------------------+-----------------------------+
1939 | ``\u2029`` | Paragraph Separator |
1940 +-----------------------+-----------------------------+
1941
1942 .. versionchanged:: 3.2
1943
1944 ``\v`` and ``\f`` added to list of line boundaries.
R David Murrayae1b94b2012-06-01 16:19:36 -04001945
Nick Coghlane4936b82014-08-09 16:14:04 +10001946 For example::
1947
1948 >>> 'ab c\n\nde fg\rkl\r\n'.splitlines()
Larry Hastingsc6256e52014-10-05 19:03:48 -07001949 ['ab c', '', 'de fg', 'kl']
Nick Coghlane4936b82014-08-09 16:14:04 +10001950 >>> 'ab c\n\nde fg\rkl\r\n'.splitlines(keepends=True)
1951 ['ab c\n', '\n', 'de fg\r', 'kl\r\n']
Georg Brandl116aa622007-08-15 14:28:22 +00001952
R David Murray05c35a62012-08-06 16:08:09 -04001953 Unlike :meth:`~str.split` when a delimiter string *sep* is given, this
1954 method returns an empty list for the empty string, and a terminal line
Nick Coghlane4936b82014-08-09 16:14:04 +10001955 break does not result in an extra line::
1956
1957 >>> "".splitlines()
1958 []
1959 >>> "One line\n".splitlines()
1960 ['One line']
1961
1962 For comparison, ``split('\n')`` gives::
1963
1964 >>> ''.split('\n')
1965 ['']
1966 >>> 'Two lines\n'.split('\n')
1967 ['Two lines', '']
R David Murray05c35a62012-08-06 16:08:09 -04001968
Georg Brandl116aa622007-08-15 14:28:22 +00001969
1970.. method:: str.startswith(prefix[, start[, end]])
1971
1972 Return ``True`` if string starts with the *prefix*, otherwise return ``False``.
1973 *prefix* can also be a tuple of prefixes to look for. With optional *start*,
1974 test string beginning at that position. With optional *end*, stop comparing
1975 string at that position.
1976
Georg Brandl116aa622007-08-15 14:28:22 +00001977
1978.. method:: str.strip([chars])
1979
1980 Return a copy of the string with the leading and trailing characters removed.
1981 The *chars* argument is a string specifying the set of characters to be removed.
1982 If omitted or ``None``, the *chars* argument defaults to removing whitespace.
1983 The *chars* argument is not a prefix or suffix; rather, all combinations of its
Nick Coghlane4936b82014-08-09 16:14:04 +10001984 values are stripped::
Georg Brandl116aa622007-08-15 14:28:22 +00001985
1986 >>> ' spacious '.strip()
1987 'spacious'
1988 >>> 'www.example.com'.strip('cmowz.')
1989 'example'
1990
Raymond Hettinger19cfb572015-05-23 09:11:55 -07001991 The outermost leading and trailing *chars* argument values are stripped
1992 from the string. Characters are removed from the leading end until
1993 reaching a string character that is not contained in the set of
1994 characters in *chars*. A similar action takes place on the trailing end.
1995 For example::
1996
1997 >>> comment_string = '#....... Section 3.2.1 Issue #32 .......'
1998 >>> comment_string.strip('.#! ')
1999 'Section 3.2.1 Issue #32'
2000
Georg Brandl116aa622007-08-15 14:28:22 +00002001
2002.. method:: str.swapcase()
2003
2004 Return a copy of the string with uppercase characters converted to lowercase and
Benjamin Petersonb2bf01d2012-01-11 18:17:06 -05002005 vice versa. Note that it is not necessarily true that
2006 ``s.swapcase().swapcase() == s``.
Georg Brandl116aa622007-08-15 14:28:22 +00002007
Georg Brandl116aa622007-08-15 14:28:22 +00002008
2009.. method:: str.title()
2010
Raymond Hettingerb8b0ba12009-09-29 06:22:28 +00002011 Return a titlecased version of the string where words start with an uppercase
2012 character and the remaining characters are lowercase.
2013
Nick Coghlane4936b82014-08-09 16:14:04 +10002014 For example::
2015
2016 >>> 'Hello world'.title()
2017 'Hello World'
2018
Raymond Hettingerb8b0ba12009-09-29 06:22:28 +00002019 The algorithm uses a simple language-independent definition of a word as
2020 groups of consecutive letters. The definition works in many contexts but
2021 it means that apostrophes in contractions and possessives form word
2022 boundaries, which may not be the desired result::
2023
2024 >>> "they're bill's friends from the UK".title()
2025 "They'Re Bill'S Friends From The Uk"
2026
2027 A workaround for apostrophes can be constructed using regular expressions::
2028
2029 >>> import re
2030 >>> def titlecase(s):
Andrew Svetlov5c904362012-11-08 17:26:53 +02002031 ... return re.sub(r"[A-Za-z]+('[A-Za-z]+)?",
2032 ... lambda mo: mo.group(0)[0].upper() +
2033 ... mo.group(0)[1:].lower(),
2034 ... s)
2035 ...
Raymond Hettingerb8b0ba12009-09-29 06:22:28 +00002036 >>> titlecase("they're bill's friends.")
2037 "They're Bill's Friends."
Georg Brandl116aa622007-08-15 14:28:22 +00002038
Georg Brandl116aa622007-08-15 14:28:22 +00002039
Zachary Ware79b98df2015-08-05 23:54:15 -05002040.. method:: str.translate(table)
Georg Brandl116aa622007-08-15 14:28:22 +00002041
Zachary Ware79b98df2015-08-05 23:54:15 -05002042 Return a copy of the string in which each character has been mapped through
2043 the given translation table. The table must be an object that implements
2044 indexing via :meth:`__getitem__`, typically a :term:`mapping` or
2045 :term:`sequence`. When indexed by a Unicode ordinal (an integer), the
2046 table object can do any of the following: return a Unicode ordinal or a
2047 string, to map the character to one or more other characters; return
2048 ``None``, to delete the character from the return string; or raise a
2049 :exc:`LookupError` exception, to map the character to itself.
Georg Brandlceee0772007-11-27 23:48:05 +00002050
Georg Brandl454636f2008-12-27 23:33:20 +00002051 You can use :meth:`str.maketrans` to create a translation map from
2052 character-to-character mappings in different formats.
Christian Heimesfe337bf2008-03-23 21:54:12 +00002053
Zachary Ware79b98df2015-08-05 23:54:15 -05002054 See also the :mod:`codecs` module for a more flexible approach to custom
2055 character mappings.
Georg Brandl116aa622007-08-15 14:28:22 +00002056
2057
2058.. method:: str.upper()
2059
Ezio Melotti0656a562011-08-15 14:27:19 +03002060 Return a copy of the string with all the cased characters [4]_ converted to
Andrés Delfino4a6e7462018-06-25 07:34:22 -03002061 uppercase. Note that ``s.upper().isupper()`` might be ``False`` if ``s``
Ezio Melotti0656a562011-08-15 14:27:19 +03002062 contains uncased characters or if the Unicode category of the resulting
Benjamin Peterson94303542012-01-18 23:09:32 -05002063 character(s) is not "Lu" (Letter, uppercase), but e.g. "Lt" (Letter,
2064 titlecase).
2065
2066 The uppercasing algorithm used is described in section 3.13 of the Unicode
2067 Standard.
Georg Brandl116aa622007-08-15 14:28:22 +00002068
Georg Brandl116aa622007-08-15 14:28:22 +00002069
2070.. method:: str.zfill(width)
2071
Nick Coghlane4936b82014-08-09 16:14:04 +10002072 Return a copy of the string left filled with ASCII ``'0'`` digits to
Tim Golden42c235e2015-04-06 11:04:49 +01002073 make a string of length *width*. A leading sign prefix (``'+'``/``'-'``)
Nick Coghlane4936b82014-08-09 16:14:04 +10002074 is handled by inserting the padding *after* the sign character rather
2075 than before. The original string is returned if *width* is less than
2076 or equal to ``len(s)``.
2077
2078 For example::
2079
2080 >>> "42".zfill(5)
2081 '00042'
2082 >>> "-42".zfill(5)
2083 '-0042'
Christian Heimesb186d002008-03-18 15:15:01 +00002084
2085
Georg Brandl116aa622007-08-15 14:28:22 +00002086
Georg Brandl4b491312007-08-31 09:22:56 +00002087.. _old-string-formatting:
Georg Brandl116aa622007-08-15 14:28:22 +00002088
Nick Coghlan273069c2012-08-20 17:14:07 +10002089``printf``-style String Formatting
2090----------------------------------
Georg Brandl116aa622007-08-15 14:28:22 +00002091
2092.. index::
2093 single: formatting, string (%)
2094 single: interpolation, string (%)
Martin Panterbc1ee462016-02-13 00:41:37 +00002095 single: string; formatting, printf
2096 single: string; interpolation, printf
Georg Brandl116aa622007-08-15 14:28:22 +00002097 single: printf-style formatting
2098 single: sprintf-style formatting
2099 single: % formatting
2100 single: % interpolation
2101
Georg Brandl4b491312007-08-31 09:22:56 +00002102.. note::
2103
Nick Coghlan273069c2012-08-20 17:14:07 +10002104 The formatting operations described here exhibit a variety of quirks that
2105 lead to a number of common errors (such as failing to display tuples and
Barry Warsaw9f74deb2017-03-28 10:02:07 -04002106 dictionaries correctly). Using the newer :ref:`formatted string literals
2107 <f-strings>`, the :meth:`str.format` interface, or :ref:`template strings
2108 <template-strings>` may help avoid these errors. Each of these
2109 alternatives provides their own trade-offs and benefits of simplicity,
2110 flexibility, and/or extensibility.
Georg Brandl4b491312007-08-31 09:22:56 +00002111
2112String objects have one unique built-in operation: the ``%`` operator (modulo).
2113This is also known as the string *formatting* or *interpolation* operator.
2114Given ``format % values`` (where *format* is a string), ``%`` conversion
2115specifications in *format* are replaced with zero or more elements of *values*.
Nick Coghlan273069c2012-08-20 17:14:07 +10002116The effect is similar to using the :c:func:`sprintf` in the C language.
Georg Brandl116aa622007-08-15 14:28:22 +00002117
2118If *format* requires a single argument, *values* may be a single non-tuple
Ezio Melotti0656a562011-08-15 14:27:19 +03002119object. [5]_ Otherwise, *values* must be a tuple with exactly the number of
Georg Brandl116aa622007-08-15 14:28:22 +00002120items specified by the format string, or a single mapping object (for example, a
2121dictionary).
2122
2123A conversion specifier contains two or more characters and has the following
2124components, which must occur in this order:
2125
2126#. The ``'%'`` character, which marks the start of the specifier.
2127
2128#. Mapping key (optional), consisting of a parenthesised sequence of characters
2129 (for example, ``(somename)``).
2130
2131#. Conversion flags (optional), which affect the result of some conversion
2132 types.
2133
2134#. Minimum field width (optional). If specified as an ``'*'`` (asterisk), the
2135 actual width is read from the next element of the tuple in *values*, and the
2136 object to convert comes after the minimum field width and optional precision.
2137
2138#. Precision (optional), given as a ``'.'`` (dot) followed by the precision. If
Eli Benderskyef4902a2011-07-29 09:30:42 +03002139 specified as ``'*'`` (an asterisk), the actual precision is read from the next
Georg Brandl116aa622007-08-15 14:28:22 +00002140 element of the tuple in *values*, and the value to convert comes after the
2141 precision.
2142
2143#. Length modifier (optional).
2144
2145#. Conversion type.
2146
2147When the right argument is a dictionary (or other mapping type), then the
2148formats in the string *must* include a parenthesised mapping key into that
2149dictionary inserted immediately after the ``'%'`` character. The mapping key
Christian Heimesfe337bf2008-03-23 21:54:12 +00002150selects the value to be formatted from the mapping. For example:
Georg Brandl116aa622007-08-15 14:28:22 +00002151
Georg Brandledc9e7f2010-10-17 09:19:03 +00002152 >>> print('%(language)s has %(number)03d quote types.' %
2153 ... {'language': "Python", "number": 2})
Georg Brandl116aa622007-08-15 14:28:22 +00002154 Python has 002 quote types.
2155
2156In this case no ``*`` specifiers may occur in a format (since they require a
2157sequential parameter list).
2158
2159The conversion flag characters are:
2160
2161+---------+---------------------------------------------------------------------+
2162| Flag | Meaning |
2163+=========+=====================================================================+
2164| ``'#'`` | The value conversion will use the "alternate form" (where defined |
2165| | below). |
2166+---------+---------------------------------------------------------------------+
2167| ``'0'`` | The conversion will be zero padded for numeric values. |
2168+---------+---------------------------------------------------------------------+
2169| ``'-'`` | The converted value is left adjusted (overrides the ``'0'`` |
2170| | conversion if both are given). |
2171+---------+---------------------------------------------------------------------+
2172| ``' '`` | (a space) A blank should be left before a positive number (or empty |
2173| | string) produced by a signed conversion. |
2174+---------+---------------------------------------------------------------------+
2175| ``'+'`` | A sign character (``'+'`` or ``'-'``) will precede the conversion |
2176| | (overrides a "space" flag). |
2177+---------+---------------------------------------------------------------------+
2178
2179A length modifier (``h``, ``l``, or ``L``) may be present, but is ignored as it
Alexandre Vassalotti5f8ced22008-05-16 00:03:33 +00002180is not necessary for Python -- so e.g. ``%ld`` is identical to ``%d``.
Georg Brandl116aa622007-08-15 14:28:22 +00002181
2182The conversion types are:
2183
2184+------------+-----------------------------------------------------+-------+
2185| Conversion | Meaning | Notes |
2186+============+=====================================================+=======+
2187| ``'d'`` | Signed integer decimal. | |
2188+------------+-----------------------------------------------------+-------+
2189| ``'i'`` | Signed integer decimal. | |
2190+------------+-----------------------------------------------------+-------+
Alexandre Vassalotti5f8ced22008-05-16 00:03:33 +00002191| ``'o'`` | Signed octal value. | \(1) |
Georg Brandl116aa622007-08-15 14:28:22 +00002192+------------+-----------------------------------------------------+-------+
Berker Peksag7b440df2016-12-15 05:37:56 +03002193| ``'u'`` | Obsolete type -- it is identical to ``'d'``. | \(6) |
Georg Brandl116aa622007-08-15 14:28:22 +00002194+------------+-----------------------------------------------------+-------+
Alexandre Vassalotti5f8ced22008-05-16 00:03:33 +00002195| ``'x'`` | Signed hexadecimal (lowercase). | \(2) |
Georg Brandl116aa622007-08-15 14:28:22 +00002196+------------+-----------------------------------------------------+-------+
Alexandre Vassalotti5f8ced22008-05-16 00:03:33 +00002197| ``'X'`` | Signed hexadecimal (uppercase). | \(2) |
Georg Brandl116aa622007-08-15 14:28:22 +00002198+------------+-----------------------------------------------------+-------+
2199| ``'e'`` | Floating point exponential format (lowercase). | \(3) |
2200+------------+-----------------------------------------------------+-------+
2201| ``'E'`` | Floating point exponential format (uppercase). | \(3) |
2202+------------+-----------------------------------------------------+-------+
Eric Smith22b85b32008-07-17 19:18:29 +00002203| ``'f'`` | Floating point decimal format. | \(3) |
Georg Brandl116aa622007-08-15 14:28:22 +00002204+------------+-----------------------------------------------------+-------+
Eric Smith22b85b32008-07-17 19:18:29 +00002205| ``'F'`` | Floating point decimal format. | \(3) |
Georg Brandl116aa622007-08-15 14:28:22 +00002206+------------+-----------------------------------------------------+-------+
Christian Heimes8dc226f2008-05-06 23:45:46 +00002207| ``'g'`` | Floating point format. Uses lowercase exponential | \(4) |
2208| | format if exponent is less than -4 or not less than | |
2209| | precision, decimal format otherwise. | |
Georg Brandl116aa622007-08-15 14:28:22 +00002210+------------+-----------------------------------------------------+-------+
Christian Heimes8dc226f2008-05-06 23:45:46 +00002211| ``'G'`` | Floating point format. Uses uppercase exponential | \(4) |
2212| | format if exponent is less than -4 or not less than | |
2213| | precision, decimal format otherwise. | |
Georg Brandl116aa622007-08-15 14:28:22 +00002214+------------+-----------------------------------------------------+-------+
2215| ``'c'`` | Single character (accepts integer or single | |
2216| | character string). | |
2217+------------+-----------------------------------------------------+-------+
Ezio Melotti0639d5a2009-12-19 23:26:38 +00002218| ``'r'`` | String (converts any Python object using | \(5) |
Georg Brandl116aa622007-08-15 14:28:22 +00002219| | :func:`repr`). | |
2220+------------+-----------------------------------------------------+-------+
Eli Benderskyef4902a2011-07-29 09:30:42 +03002221| ``'s'`` | String (converts any Python object using | \(5) |
Georg Brandl116aa622007-08-15 14:28:22 +00002222| | :func:`str`). | |
2223+------------+-----------------------------------------------------+-------+
Eli Benderskyef4902a2011-07-29 09:30:42 +03002224| ``'a'`` | String (converts any Python object using | \(5) |
2225| | :func:`ascii`). | |
2226+------------+-----------------------------------------------------+-------+
Georg Brandl116aa622007-08-15 14:28:22 +00002227| ``'%'`` | No argument is converted, results in a ``'%'`` | |
2228| | character in the result. | |
2229+------------+-----------------------------------------------------+-------+
2230
2231Notes:
2232
2233(1)
Martin Panter41176ae2016-12-11 01:07:29 +00002234 The alternate form causes a leading octal specifier (``'0o'``) to be
2235 inserted before the first digit.
Georg Brandl116aa622007-08-15 14:28:22 +00002236
2237(2)
2238 The alternate form causes a leading ``'0x'`` or ``'0X'`` (depending on whether
Martin Panter41176ae2016-12-11 01:07:29 +00002239 the ``'x'`` or ``'X'`` format was used) to be inserted before the first digit.
Georg Brandl116aa622007-08-15 14:28:22 +00002240
2241(3)
2242 The alternate form causes the result to always contain a decimal point, even if
2243 no digits follow it.
2244
2245 The precision determines the number of digits after the decimal point and
2246 defaults to 6.
2247
2248(4)
2249 The alternate form causes the result to always contain a decimal point, and
2250 trailing zeroes are not removed as they would otherwise be.
2251
2252 The precision determines the number of significant digits before and after the
2253 decimal point and defaults to 6.
2254
2255(5)
Eli Benderskyef4902a2011-07-29 09:30:42 +03002256 If precision is ``N``, the output is truncated to ``N`` characters.
Georg Brandl116aa622007-08-15 14:28:22 +00002257
Berker Peksag7b440df2016-12-15 05:37:56 +03002258(6)
Alexandre Vassalotti5f8ced22008-05-16 00:03:33 +00002259 See :pep:`237`.
2260
Georg Brandl116aa622007-08-15 14:28:22 +00002261Since Python strings have an explicit length, ``%s`` conversions do not assume
2262that ``'\0'`` is the end of the string.
2263
Christian Heimes5b5e81c2007-12-31 16:14:33 +00002264.. XXX Examples?
2265
Mark Dickinson33841c32009-05-01 15:37:04 +00002266.. versionchanged:: 3.1
2267 ``%f`` conversions for numbers whose absolute value is over 1e50 are no
2268 longer replaced by ``%g`` conversions.
Georg Brandl116aa622007-08-15 14:28:22 +00002269
Georg Brandl116aa622007-08-15 14:28:22 +00002270
Chris Jerdonek5fae0e52012-11-20 17:45:51 -08002271.. index::
2272 single: buffer protocol; binary sequence types
2273
Nick Coghlan273069c2012-08-20 17:14:07 +10002274.. _binaryseq:
Georg Brandl116aa622007-08-15 14:28:22 +00002275
Nick Coghlan273069c2012-08-20 17:14:07 +10002276Binary Sequence Types --- :class:`bytes`, :class:`bytearray`, :class:`memoryview`
2277=================================================================================
Georg Brandl116aa622007-08-15 14:28:22 +00002278
2279.. index::
Nick Coghlan273069c2012-08-20 17:14:07 +10002280 object: bytes
Georg Brandl95414632007-11-22 11:00:28 +00002281 object: bytearray
Nick Coghlan273069c2012-08-20 17:14:07 +10002282 object: memoryview
2283 module: array
Georg Brandl116aa622007-08-15 14:28:22 +00002284
Nick Coghlan273069c2012-08-20 17:14:07 +10002285The core built-in types for manipulating binary data are :class:`bytes` and
2286:class:`bytearray`. They are supported by :class:`memoryview` which uses
Chris Jerdonek5fae0e52012-11-20 17:45:51 -08002287the :ref:`buffer protocol <bufferobjects>` to access the memory of other
2288binary objects without needing to make a copy.
Georg Brandl226878c2007-08-31 10:15:37 +00002289
Nick Coghlan273069c2012-08-20 17:14:07 +10002290The :mod:`array` module supports efficient storage of basic data types like
229132-bit integers and IEEE754 double-precision floating values.
Georg Brandl116aa622007-08-15 14:28:22 +00002292
Nick Coghlan273069c2012-08-20 17:14:07 +10002293.. _typebytes:
Senthil Kumaran7cafd262010-10-02 03:16:04 +00002294
csabellac6db4812017-04-26 01:47:01 -04002295Bytes Objects
2296-------------
Nick Coghlan273069c2012-08-20 17:14:07 +10002297
2298.. index:: object: bytes
2299
2300Bytes objects are immutable sequences of single bytes. Since many major
2301binary protocols are based on the ASCII text encoding, bytes objects offer
2302several methods that are only valid when working with ASCII compatible
2303data and are closely related to string objects in a variety of other ways.
2304
csabellac6db4812017-04-26 01:47:01 -04002305.. class:: bytes([source[, encoding[, errors]]])
Nick Coghlan273069c2012-08-20 17:14:07 +10002306
csabellac6db4812017-04-26 01:47:01 -04002307 Firstly, the syntax for bytes literals is largely the same as that for string
2308 literals, except that a ``b`` prefix is added:
Nick Coghlan273069c2012-08-20 17:14:07 +10002309
csabellac6db4812017-04-26 01:47:01 -04002310 * Single quotes: ``b'still allows embedded "double" quotes'``
2311 * Double quotes: ``b"still allows embedded 'single' quotes"``.
2312 * Triple quoted: ``b'''3 single quotes'''``, ``b"""3 double quotes"""``
Nick Coghlan273069c2012-08-20 17:14:07 +10002313
csabellac6db4812017-04-26 01:47:01 -04002314 Only ASCII characters are permitted in bytes literals (regardless of the
2315 declared source code encoding). Any binary values over 127 must be entered
2316 into bytes literals using the appropriate escape sequence.
Nick Coghlan273069c2012-08-20 17:14:07 +10002317
csabellac6db4812017-04-26 01:47:01 -04002318 As with string literals, bytes literals may also use a ``r`` prefix to disable
2319 processing of escape sequences. See :ref:`strings` for more about the various
2320 forms of bytes literal, including supported escape sequences.
Nick Coghlan273069c2012-08-20 17:14:07 +10002321
csabellac6db4812017-04-26 01:47:01 -04002322 While bytes literals and representations are based on ASCII text, bytes
2323 objects actually behave like immutable sequences of integers, with each
2324 value in the sequence restricted such that ``0 <= x < 256`` (attempts to
Andrés Delfino03dd0e72018-07-07 16:00:46 -03002325 violate this restriction will trigger :exc:`ValueError`). This is done
csabellac6db4812017-04-26 01:47:01 -04002326 deliberately to emphasise that while many binary formats include ASCII based
2327 elements and can be usefully manipulated with some text-oriented algorithms,
2328 this is not generally the case for arbitrary binary data (blindly applying
2329 text processing algorithms to binary data formats that are not ASCII
2330 compatible will usually lead to data corruption).
Nick Coghlan273069c2012-08-20 17:14:07 +10002331
csabellac6db4812017-04-26 01:47:01 -04002332 In addition to the literal forms, bytes objects can be created in a number of
2333 other ways:
Nick Coghlan273069c2012-08-20 17:14:07 +10002334
csabellac6db4812017-04-26 01:47:01 -04002335 * A zero-filled bytes object of a specified length: ``bytes(10)``
2336 * From an iterable of integers: ``bytes(range(20))``
2337 * Copying existing binary data via the buffer protocol: ``bytes(obj)``
Nick Coghlan83c0ae52012-08-21 17:42:52 +10002338
csabellac6db4812017-04-26 01:47:01 -04002339 Also see the :ref:`bytes <func-bytes>` built-in.
Nick Coghlane4936b82014-08-09 16:14:04 +10002340
csabellac6db4812017-04-26 01:47:01 -04002341 Since 2 hexadecimal digits correspond precisely to a single byte, hexadecimal
2342 numbers are a commonly used format for describing binary data. Accordingly,
2343 the bytes type has an additional class method to read data in that format:
Nick Coghlane4936b82014-08-09 16:14:04 +10002344
csabellac6db4812017-04-26 01:47:01 -04002345 .. classmethod:: fromhex(string)
Nick Coghlane4936b82014-08-09 16:14:04 +10002346
csabellac6db4812017-04-26 01:47:01 -04002347 This :class:`bytes` class method returns a bytes object, decoding the
2348 given string object. The string must contain two hexadecimal digits per
2349 byte, with ASCII whitespace being ignored.
Nick Coghlane4936b82014-08-09 16:14:04 +10002350
csabellac6db4812017-04-26 01:47:01 -04002351 >>> bytes.fromhex('2Ef0 F1f2 ')
2352 b'.\xf0\xf1\xf2'
Serhiy Storchakadd1da7f2016-12-19 18:51:37 +02002353
csabellac6db4812017-04-26 01:47:01 -04002354 .. versionchanged:: 3.7
2355 :meth:`bytes.fromhex` now skips all ASCII whitespace in the string,
2356 not just spaces.
Gregory P. Smith8cb65692015-04-25 23:22:26 +00002357
csabellac6db4812017-04-26 01:47:01 -04002358 A reverse conversion function exists to transform a bytes object into its
2359 hexadecimal representation.
Gregory P. Smith8cb65692015-04-25 23:22:26 +00002360
csabellac6db4812017-04-26 01:47:01 -04002361 .. method:: hex()
Gregory P. Smith8cb65692015-04-25 23:22:26 +00002362
csabellac6db4812017-04-26 01:47:01 -04002363 Return a string object containing two hexadecimal digits for each
2364 byte in the instance.
Gregory P. Smith8cb65692015-04-25 23:22:26 +00002365
csabellac6db4812017-04-26 01:47:01 -04002366 >>> b'\xf0\xf1\xf2'.hex()
2367 'f0f1f2'
2368
2369 .. versionadded:: 3.5
Gregory P. Smith8cb65692015-04-25 23:22:26 +00002370
Nick Coghlane4936b82014-08-09 16:14:04 +10002371Since bytes objects are sequences of integers (akin to a tuple), for a bytes
2372object *b*, ``b[0]`` will be an integer, while ``b[0:1]`` will be a bytes
2373object of length 1. (This contrasts with text strings, where both indexing
2374and slicing will produce a string of length 1)
Nick Coghlan273069c2012-08-20 17:14:07 +10002375
2376The representation of bytes objects uses the literal format (``b'...'``)
2377since it is often more useful than e.g. ``bytes([46, 46, 46])``. You can
2378always convert a bytes object into a list of integers using ``list(b)``.
Georg Brandl116aa622007-08-15 14:28:22 +00002379
Nick Coghlan273069c2012-08-20 17:14:07 +10002380.. note::
2381 For Python 2.x users: In the Python 2.x series, a variety of implicit
2382 conversions between 8-bit strings (the closest thing 2.x offers to a
2383 built-in binary data type) and Unicode strings were permitted. This was a
2384 backwards compatibility workaround to account for the fact that Python
2385 originally only supported 8-bit text, and Unicode text was a later
2386 addition. In Python 3.x, those implicit conversions are gone - conversions
2387 between 8-bit binary data and Unicode text must be explicit, and bytes and
2388 string objects will always compare unequal.
Raymond Hettingerc50846a2010-04-05 18:56:31 +00002389
Georg Brandl116aa622007-08-15 14:28:22 +00002390
Nick Coghlan273069c2012-08-20 17:14:07 +10002391.. _typebytearray:
Georg Brandl116aa622007-08-15 14:28:22 +00002392
Nick Coghlan273069c2012-08-20 17:14:07 +10002393Bytearray Objects
2394-----------------
Georg Brandl116aa622007-08-15 14:28:22 +00002395
Nick Coghlan273069c2012-08-20 17:14:07 +10002396.. index:: object: bytearray
Georg Brandl495f7b52009-10-27 15:28:25 +00002397
Nick Coghlan273069c2012-08-20 17:14:07 +10002398:class:`bytearray` objects are a mutable counterpart to :class:`bytes`
csabellac6db4812017-04-26 01:47:01 -04002399objects.
Georg Brandl116aa622007-08-15 14:28:22 +00002400
csabellac6db4812017-04-26 01:47:01 -04002401.. class:: bytearray([source[, encoding[, errors]]])
Eli Benderskycbbaa962011-02-25 05:47:53 +00002402
csabellac6db4812017-04-26 01:47:01 -04002403 There is no dedicated literal syntax for bytearray objects, instead
2404 they are always created by calling the constructor:
Georg Brandl116aa622007-08-15 14:28:22 +00002405
csabellac6db4812017-04-26 01:47:01 -04002406 * Creating an empty instance: ``bytearray()``
2407 * Creating a zero-filled instance with a given length: ``bytearray(10)``
2408 * From an iterable of integers: ``bytearray(range(20))``
2409 * Copying existing binary data via the buffer protocol: ``bytearray(b'Hi!')``
Nick Coghlan83c0ae52012-08-21 17:42:52 +10002410
csabellac6db4812017-04-26 01:47:01 -04002411 As bytearray objects are mutable, they support the
2412 :ref:`mutable <typesseq-mutable>` sequence operations in addition to the
2413 common bytes and bytearray operations described in :ref:`bytes-methods`.
Nick Coghlane4936b82014-08-09 16:14:04 +10002414
csabellac6db4812017-04-26 01:47:01 -04002415 Also see the :ref:`bytearray <func-bytearray>` built-in.
Nick Coghlane4936b82014-08-09 16:14:04 +10002416
csabellac6db4812017-04-26 01:47:01 -04002417 Since 2 hexadecimal digits correspond precisely to a single byte, hexadecimal
2418 numbers are a commonly used format for describing binary data. Accordingly,
2419 the bytearray type has an additional class method to read data in that format:
Nick Coghlane4936b82014-08-09 16:14:04 +10002420
csabellac6db4812017-04-26 01:47:01 -04002421 .. classmethod:: fromhex(string)
Nick Coghlane4936b82014-08-09 16:14:04 +10002422
csabellac6db4812017-04-26 01:47:01 -04002423 This :class:`bytearray` class method returns bytearray object, decoding
2424 the given string object. The string must contain two hexadecimal digits
2425 per byte, with ASCII whitespace being ignored.
Serhiy Storchakadd1da7f2016-12-19 18:51:37 +02002426
csabellac6db4812017-04-26 01:47:01 -04002427 >>> bytearray.fromhex('2Ef0 F1f2 ')
2428 bytearray(b'.\xf0\xf1\xf2')
Gregory P. Smith8cb65692015-04-25 23:22:26 +00002429
csabellac6db4812017-04-26 01:47:01 -04002430 .. versionchanged:: 3.7
2431 :meth:`bytearray.fromhex` now skips all ASCII whitespace in the string,
2432 not just spaces.
Gregory P. Smith8cb65692015-04-25 23:22:26 +00002433
csabellac6db4812017-04-26 01:47:01 -04002434 A reverse conversion function exists to transform a bytearray object into its
2435 hexadecimal representation.
Gregory P. Smith8cb65692015-04-25 23:22:26 +00002436
csabellac6db4812017-04-26 01:47:01 -04002437 .. method:: hex()
Gregory P. Smith8cb65692015-04-25 23:22:26 +00002438
csabellac6db4812017-04-26 01:47:01 -04002439 Return a string object containing two hexadecimal digits for each
2440 byte in the instance.
2441
2442 >>> bytearray(b'\xf0\xf1\xf2').hex()
2443 'f0f1f2'
2444
2445 .. versionadded:: 3.5
Gregory P. Smith8cb65692015-04-25 23:22:26 +00002446
Nick Coghlane4936b82014-08-09 16:14:04 +10002447Since bytearray objects are sequences of integers (akin to a list), for a
2448bytearray object *b*, ``b[0]`` will be an integer, while ``b[0:1]`` will be
2449a bytearray object of length 1. (This contrasts with text strings, where
2450both indexing and slicing will produce a string of length 1)
2451
2452The representation of bytearray objects uses the bytes literal format
2453(``bytearray(b'...')``) since it is often more useful than e.g.
2454``bytearray([46, 46, 46])``. You can always convert a bytearray object into
2455a list of integers using ``list(b)``.
2456
Georg Brandl495f7b52009-10-27 15:28:25 +00002457
Georg Brandl226878c2007-08-31 10:15:37 +00002458.. _bytes-methods:
2459
Nick Coghlan273069c2012-08-20 17:14:07 +10002460Bytes and Bytearray Operations
2461------------------------------
Georg Brandl226878c2007-08-31 10:15:37 +00002462
2463.. index:: pair: bytes; methods
Georg Brandl95414632007-11-22 11:00:28 +00002464 pair: bytearray; methods
Georg Brandl226878c2007-08-31 10:15:37 +00002465
Nick Coghlan273069c2012-08-20 17:14:07 +10002466Both bytes and bytearray objects support the :ref:`common <typesseq-common>`
2467sequence operations. They interoperate not just with operands of the same
Nick Coghlane4936b82014-08-09 16:14:04 +10002468type, but with any :term:`bytes-like object`. Due to this flexibility, they can be
Nick Coghlan273069c2012-08-20 17:14:07 +10002469freely mixed in operations without causing errors. However, the return type
2470of the result may depend on the order of operands.
Guido van Rossum98297ee2007-11-06 21:34:58 +00002471
Georg Brandl7c676132007-10-23 18:17:00 +00002472.. note::
Georg Brandl226878c2007-08-31 10:15:37 +00002473
Georg Brandl95414632007-11-22 11:00:28 +00002474 The methods on bytes and bytearray objects don't accept strings as their
Georg Brandl7c676132007-10-23 18:17:00 +00002475 arguments, just as the methods on strings don't accept bytes as their
Nick Coghlan273069c2012-08-20 17:14:07 +10002476 arguments. For example, you have to write::
Georg Brandl226878c2007-08-31 10:15:37 +00002477
Georg Brandl7c676132007-10-23 18:17:00 +00002478 a = "abc"
2479 b = a.replace("a", "f")
2480
Nick Coghlan273069c2012-08-20 17:14:07 +10002481 and::
Georg Brandl7c676132007-10-23 18:17:00 +00002482
2483 a = b"abc"
2484 b = a.replace(b"a", b"f")
Georg Brandl226878c2007-08-31 10:15:37 +00002485
Nick Coghlane4936b82014-08-09 16:14:04 +10002486Some bytes and bytearray operations assume the use of ASCII compatible
2487binary formats, and hence should be avoided when working with arbitrary
2488binary data. These restrictions are covered below.
Nick Coghlan273069c2012-08-20 17:14:07 +10002489
2490.. note::
Nick Coghlane4936b82014-08-09 16:14:04 +10002491 Using these ASCII based operations to manipulate binary data that is not
Nick Coghlan273069c2012-08-20 17:14:07 +10002492 stored in an ASCII based format may lead to data corruption.
2493
Nick Coghlane4936b82014-08-09 16:14:04 +10002494The following methods on bytes and bytearray objects can be used with
2495arbitrary binary data.
Nick Coghlan273069c2012-08-20 17:14:07 +10002496
Nick Coghlane4936b82014-08-09 16:14:04 +10002497.. method:: bytes.count(sub[, start[, end]])
2498 bytearray.count(sub[, start[, end]])
Nick Coghlan273069c2012-08-20 17:14:07 +10002499
Nick Coghlane4936b82014-08-09 16:14:04 +10002500 Return the number of non-overlapping occurrences of subsequence *sub* in
2501 the range [*start*, *end*]. Optional arguments *start* and *end* are
2502 interpreted as in slice notation.
Nick Coghlan273069c2012-08-20 17:14:07 +10002503
Nick Coghlane4936b82014-08-09 16:14:04 +10002504 The subsequence to search for may be any :term:`bytes-like object` or an
2505 integer in the range 0 to 255.
2506
2507 .. versionchanged:: 3.3
2508 Also accept an integer in the range 0 to 255 as the subsequence.
2509
Georg Brandl226878c2007-08-31 10:15:37 +00002510
Victor Stinnere14e2122010-11-07 18:41:46 +00002511.. method:: bytes.decode(encoding="utf-8", errors="strict")
2512 bytearray.decode(encoding="utf-8", errors="strict")
Georg Brandl4f5f98d2009-05-04 21:01:20 +00002513
Victor Stinnere14e2122010-11-07 18:41:46 +00002514 Return a string decoded from the given bytes. Default encoding is
2515 ``'utf-8'``. *errors* may be given to set a different
Georg Brandl4f5f98d2009-05-04 21:01:20 +00002516 error handling scheme. The default for *errors* is ``'strict'``, meaning
2517 that encoding errors raise a :exc:`UnicodeError`. Other possible values are
2518 ``'ignore'``, ``'replace'`` and any other name registered via
Nick Coghlanb9fdb7a2015-01-07 00:22:00 +10002519 :func:`codecs.register_error`, see section :ref:`error-handlers`. For a
Georg Brandl4f5f98d2009-05-04 21:01:20 +00002520 list of possible encodings, see section :ref:`standard-encodings`.
2521
Nick Coghlane4936b82014-08-09 16:14:04 +10002522 .. note::
2523
2524 Passing the *encoding* argument to :class:`str` allows decoding any
2525 :term:`bytes-like object` directly, without needing to make a temporary
2526 bytes or bytearray object.
2527
Benjamin Peterson308d6372009-09-18 21:42:35 +00002528 .. versionchanged:: 3.1
2529 Added support for keyword arguments.
2530
Georg Brandl226878c2007-08-31 10:15:37 +00002531
Nick Coghlane4936b82014-08-09 16:14:04 +10002532.. method:: bytes.endswith(suffix[, start[, end]])
2533 bytearray.endswith(suffix[, start[, end]])
Georg Brandl226878c2007-08-31 10:15:37 +00002534
Nick Coghlane4936b82014-08-09 16:14:04 +10002535 Return ``True`` if the binary data ends with the specified *suffix*,
2536 otherwise return ``False``. *suffix* can also be a tuple of suffixes to
2537 look for. With optional *start*, test beginning at that position. With
2538 optional *end*, stop comparing at that position.
Georg Brandl226878c2007-08-31 10:15:37 +00002539
Nick Coghlane4936b82014-08-09 16:14:04 +10002540 The suffix(es) to search for may be any :term:`bytes-like object`.
Georg Brandl226878c2007-08-31 10:15:37 +00002541
Georg Brandlabc38772009-04-12 15:51:51 +00002542
Nick Coghlane4936b82014-08-09 16:14:04 +10002543.. method:: bytes.find(sub[, start[, end]])
2544 bytearray.find(sub[, start[, end]])
2545
2546 Return the lowest index in the data where the subsequence *sub* is found,
2547 such that *sub* is contained in the slice ``s[start:end]``. Optional
2548 arguments *start* and *end* are interpreted as in slice notation. Return
2549 ``-1`` if *sub* is not found.
2550
2551 The subsequence to search for may be any :term:`bytes-like object` or an
2552 integer in the range 0 to 255.
2553
2554 .. note::
2555
2556 The :meth:`~bytes.find` method should be used only if you need to know the
2557 position of *sub*. To check if *sub* is a substring or not, use the
2558 :keyword:`in` operator::
2559
2560 >>> b'Py' in b'Python'
2561 True
2562
2563 .. versionchanged:: 3.3
2564 Also accept an integer in the range 0 to 255 as the subsequence.
2565
2566
2567.. method:: bytes.index(sub[, start[, end]])
2568 bytearray.index(sub[, start[, end]])
2569
2570 Like :meth:`~bytes.find`, but raise :exc:`ValueError` when the
2571 subsequence is not found.
2572
2573 The subsequence to search for may be any :term:`bytes-like object` or an
2574 integer in the range 0 to 255.
2575
2576 .. versionchanged:: 3.3
2577 Also accept an integer in the range 0 to 255 as the subsequence.
2578
2579
2580.. method:: bytes.join(iterable)
2581 bytearray.join(iterable)
2582
2583 Return a bytes or bytearray object which is the concatenation of the
Sanyam Khurana08e2f352017-05-27 11:14:41 +05302584 binary data sequences in *iterable*. A :exc:`TypeError` will be raised
2585 if there are any values in *iterable* that are not :term:`bytes-like
2586 objects <bytes-like object>`, including :class:`str` objects. The
2587 separator between elements is the contents of the bytes or
2588 bytearray object providing this method.
Nick Coghlane4936b82014-08-09 16:14:04 +10002589
2590
2591.. staticmethod:: bytes.maketrans(from, to)
2592 bytearray.maketrans(from, to)
2593
2594 This static method returns a translation table usable for
2595 :meth:`bytes.translate` that will map each character in *from* into the
2596 character at the same position in *to*; *from* and *to* must both be
2597 :term:`bytes-like objects <bytes-like object>` and have the same length.
2598
2599 .. versionadded:: 3.1
2600
2601
2602.. method:: bytes.partition(sep)
2603 bytearray.partition(sep)
2604
2605 Split the sequence at the first occurrence of *sep*, and return a 3-tuple
Serhiy Storchakaa2314282017-10-29 02:11:54 +03002606 containing the part before the separator, the separator itself or its
2607 bytearray copy, and the part after the separator.
2608 If the separator is not found, return a 3-tuple
Nick Coghlane4936b82014-08-09 16:14:04 +10002609 containing a copy of the original sequence, followed by two empty bytes or
2610 bytearray objects.
2611
2612 The separator to search for may be any :term:`bytes-like object`.
2613
2614
2615.. method:: bytes.replace(old, new[, count])
2616 bytearray.replace(old, new[, count])
2617
2618 Return a copy of the sequence with all occurrences of subsequence *old*
2619 replaced by *new*. If the optional argument *count* is given, only the
2620 first *count* occurrences are replaced.
2621
2622 The subsequence to search for and its replacement may be any
2623 :term:`bytes-like object`.
2624
2625 .. note::
2626
2627 The bytearray version of this method does *not* operate in place - it
2628 always produces a new object, even if no changes were made.
2629
2630
2631.. method:: bytes.rfind(sub[, start[, end]])
2632 bytearray.rfind(sub[, start[, end]])
2633
2634 Return the highest index in the sequence where the subsequence *sub* is
2635 found, such that *sub* is contained within ``s[start:end]``. Optional
2636 arguments *start* and *end* are interpreted as in slice notation. Return
2637 ``-1`` on failure.
2638
2639 The subsequence to search for may be any :term:`bytes-like object` or an
2640 integer in the range 0 to 255.
2641
2642 .. versionchanged:: 3.3
2643 Also accept an integer in the range 0 to 255 as the subsequence.
2644
2645
2646.. method:: bytes.rindex(sub[, start[, end]])
2647 bytearray.rindex(sub[, start[, end]])
2648
2649 Like :meth:`~bytes.rfind` but raises :exc:`ValueError` when the
2650 subsequence *sub* is not found.
2651
2652 The subsequence to search for may be any :term:`bytes-like object` or an
2653 integer in the range 0 to 255.
2654
2655 .. versionchanged:: 3.3
2656 Also accept an integer in the range 0 to 255 as the subsequence.
2657
2658
2659.. method:: bytes.rpartition(sep)
2660 bytearray.rpartition(sep)
2661
2662 Split the sequence at the last occurrence of *sep*, and return a 3-tuple
Serhiy Storchakaa2314282017-10-29 02:11:54 +03002663 containing the part before the separator, the separator itself or its
2664 bytearray copy, and the part after the separator.
2665 If the separator is not found, return a 3-tuple
Nick Coghlane4936b82014-08-09 16:14:04 +10002666 containing a copy of the original sequence, followed by two empty bytes or
2667 bytearray objects.
2668
2669 The separator to search for may be any :term:`bytes-like object`.
2670
2671
2672.. method:: bytes.startswith(prefix[, start[, end]])
2673 bytearray.startswith(prefix[, start[, end]])
2674
2675 Return ``True`` if the binary data starts with the specified *prefix*,
2676 otherwise return ``False``. *prefix* can also be a tuple of prefixes to
2677 look for. With optional *start*, test beginning at that position. With
2678 optional *end*, stop comparing at that position.
2679
2680 The prefix(es) to search for may be any :term:`bytes-like object`.
2681
Georg Brandl48310cd2009-01-03 21:18:54 +00002682
Martin Panter1b6c6da2016-08-27 08:35:02 +00002683.. method:: bytes.translate(table, delete=b'')
2684 bytearray.translate(table, delete=b'')
Georg Brandl226878c2007-08-31 10:15:37 +00002685
Georg Brandl454636f2008-12-27 23:33:20 +00002686 Return a copy of the bytes or bytearray object where all bytes occurring in
Nick Coghlane4936b82014-08-09 16:14:04 +10002687 the optional argument *delete* are removed, and the remaining bytes have
2688 been mapped through the given translation table, which must be a bytes
2689 object of length 256.
Georg Brandl226878c2007-08-31 10:15:37 +00002690
Nick Coghlane4936b82014-08-09 16:14:04 +10002691 You can use the :func:`bytes.maketrans` method to create a translation
2692 table.
Georg Brandl226878c2007-08-31 10:15:37 +00002693
Georg Brandl454636f2008-12-27 23:33:20 +00002694 Set the *table* argument to ``None`` for translations that only delete
2695 characters::
Georg Brandl226878c2007-08-31 10:15:37 +00002696
Georg Brandl454636f2008-12-27 23:33:20 +00002697 >>> b'read this short text'.translate(None, b'aeiou')
2698 b'rd ths shrt txt'
Georg Brandl226878c2007-08-31 10:15:37 +00002699
Martin Panter1b6c6da2016-08-27 08:35:02 +00002700 .. versionchanged:: 3.6
2701 *delete* is now supported as a keyword argument.
2702
Georg Brandl226878c2007-08-31 10:15:37 +00002703
Nick Coghlane4936b82014-08-09 16:14:04 +10002704The following methods on bytes and bytearray objects have default behaviours
2705that assume the use of ASCII compatible binary formats, but can still be used
2706with arbitrary binary data by passing appropriate arguments. Note that all of
2707the bytearray methods in this section do *not* operate in place, and instead
2708produce new objects.
Georg Brandlabc38772009-04-12 15:51:51 +00002709
Nick Coghlane4936b82014-08-09 16:14:04 +10002710.. method:: bytes.center(width[, fillbyte])
2711 bytearray.center(width[, fillbyte])
Georg Brandlabc38772009-04-12 15:51:51 +00002712
Nick Coghlane4936b82014-08-09 16:14:04 +10002713 Return a copy of the object centered in a sequence of length *width*.
2714 Padding is done using the specified *fillbyte* (default is an ASCII
2715 space). For :class:`bytes` objects, the original sequence is returned if
2716 *width* is less than or equal to ``len(s)``.
2717
2718 .. note::
2719
2720 The bytearray version of this method does *not* operate in place -
2721 it always produces a new object, even if no changes were made.
2722
2723
2724.. method:: bytes.ljust(width[, fillbyte])
2725 bytearray.ljust(width[, fillbyte])
2726
2727 Return a copy of the object left justified in a sequence of length *width*.
2728 Padding is done using the specified *fillbyte* (default is an ASCII
2729 space). For :class:`bytes` objects, the original sequence is returned if
2730 *width* is less than or equal to ``len(s)``.
2731
2732 .. note::
2733
2734 The bytearray version of this method does *not* operate in place -
2735 it always produces a new object, even if no changes were made.
2736
2737
2738.. method:: bytes.lstrip([chars])
2739 bytearray.lstrip([chars])
2740
2741 Return a copy of the sequence with specified leading bytes removed. The
2742 *chars* argument is a binary sequence specifying the set of byte values to
2743 be removed - the name refers to the fact this method is usually used with
2744 ASCII characters. If omitted or ``None``, the *chars* argument defaults
2745 to removing ASCII whitespace. The *chars* argument is not a prefix;
2746 rather, all combinations of its values are stripped::
2747
2748 >>> b' spacious '.lstrip()
2749 b'spacious '
2750 >>> b'www.example.com'.lstrip(b'cmowz.')
2751 b'example.com'
2752
2753 The binary sequence of byte values to remove may be any
2754 :term:`bytes-like object`.
2755
2756 .. note::
2757
2758 The bytearray version of this method does *not* operate in place -
2759 it always produces a new object, even if no changes were made.
2760
2761
2762.. method:: bytes.rjust(width[, fillbyte])
2763 bytearray.rjust(width[, fillbyte])
2764
2765 Return a copy of the object right justified in a sequence of length *width*.
2766 Padding is done using the specified *fillbyte* (default is an ASCII
2767 space). For :class:`bytes` objects, the original sequence is returned if
2768 *width* is less than or equal to ``len(s)``.
2769
2770 .. note::
2771
2772 The bytearray version of this method does *not* operate in place -
2773 it always produces a new object, even if no changes were made.
2774
2775
2776.. method:: bytes.rsplit(sep=None, maxsplit=-1)
2777 bytearray.rsplit(sep=None, maxsplit=-1)
2778
2779 Split the binary sequence into subsequences of the same type, using *sep*
2780 as the delimiter string. If *maxsplit* is given, at most *maxsplit* splits
2781 are done, the *rightmost* ones. If *sep* is not specified or ``None``,
2782 any subsequence consisting solely of ASCII whitespace is a separator.
2783 Except for splitting from the right, :meth:`rsplit` behaves like
2784 :meth:`split` which is described in detail below.
2785
2786
2787.. method:: bytes.rstrip([chars])
2788 bytearray.rstrip([chars])
2789
2790 Return a copy of the sequence with specified trailing bytes removed. The
2791 *chars* argument is a binary sequence specifying the set of byte values to
2792 be removed - the name refers to the fact this method is usually used with
2793 ASCII characters. If omitted or ``None``, the *chars* argument defaults to
2794 removing ASCII whitespace. The *chars* argument is not a suffix; rather,
2795 all combinations of its values are stripped::
2796
2797 >>> b' spacious '.rstrip()
2798 b' spacious'
2799 >>> b'mississippi'.rstrip(b'ipz')
2800 b'mississ'
2801
2802 The binary sequence of byte values to remove may be any
2803 :term:`bytes-like object`.
2804
2805 .. note::
2806
2807 The bytearray version of this method does *not* operate in place -
2808 it always produces a new object, even if no changes were made.
2809
2810
2811.. method:: bytes.split(sep=None, maxsplit=-1)
2812 bytearray.split(sep=None, maxsplit=-1)
2813
2814 Split the binary sequence into subsequences of the same type, using *sep*
2815 as the delimiter string. If *maxsplit* is given and non-negative, at most
2816 *maxsplit* splits are done (thus, the list will have at most ``maxsplit+1``
2817 elements). If *maxsplit* is not specified or is ``-1``, then there is no
2818 limit on the number of splits (all possible splits are made).
2819
2820 If *sep* is given, consecutive delimiters are not grouped together and are
2821 deemed to delimit empty subsequences (for example, ``b'1,,2'.split(b',')``
2822 returns ``[b'1', b'', b'2']``). The *sep* argument may consist of a
2823 multibyte sequence (for example, ``b'1<>2<>3'.split(b'<>')`` returns
2824 ``[b'1', b'2', b'3']``). Splitting an empty sequence with a specified
2825 separator returns ``[b'']`` or ``[bytearray(b'')]`` depending on the type
2826 of object being split. The *sep* argument may be any
2827 :term:`bytes-like object`.
2828
2829 For example::
2830
2831 >>> b'1,2,3'.split(b',')
2832 [b'1', b'2', b'3']
2833 >>> b'1,2,3'.split(b',', maxsplit=1)
Benjamin Petersoneb83ffe2014-09-22 22:43:50 -04002834 [b'1', b'2,3']
Nick Coghlane4936b82014-08-09 16:14:04 +10002835 >>> b'1,2,,3,'.split(b',')
2836 [b'1', b'2', b'', b'3', b'']
2837
2838 If *sep* is not specified or is ``None``, a different splitting algorithm
2839 is applied: runs of consecutive ASCII whitespace are regarded as a single
2840 separator, and the result will contain no empty strings at the start or
2841 end if the sequence has leading or trailing whitespace. Consequently,
2842 splitting an empty sequence or a sequence consisting solely of ASCII
2843 whitespace without a specified separator returns ``[]``.
2844
2845 For example::
2846
2847
2848 >>> b'1 2 3'.split()
2849 [b'1', b'2', b'3']
2850 >>> b'1 2 3'.split(maxsplit=1)
2851 [b'1', b'2 3']
2852 >>> b' 1 2 3 '.split()
2853 [b'1', b'2', b'3']
2854
2855
2856.. method:: bytes.strip([chars])
2857 bytearray.strip([chars])
2858
2859 Return a copy of the sequence with specified leading and trailing bytes
2860 removed. The *chars* argument is a binary sequence specifying the set of
2861 byte values to be removed - the name refers to the fact this method is
2862 usually used with ASCII characters. If omitted or ``None``, the *chars*
2863 argument defaults to removing ASCII whitespace. The *chars* argument is
2864 not a prefix or suffix; rather, all combinations of its values are
2865 stripped::
2866
2867 >>> b' spacious '.strip()
2868 b'spacious'
2869 >>> b'www.example.com'.strip(b'cmowz.')
2870 b'example'
2871
2872 The binary sequence of byte values to remove may be any
2873 :term:`bytes-like object`.
2874
2875 .. note::
2876
2877 The bytearray version of this method does *not* operate in place -
2878 it always produces a new object, even if no changes were made.
2879
2880
2881The following methods on bytes and bytearray objects assume the use of ASCII
2882compatible binary formats and should not be applied to arbitrary binary data.
2883Note that all of the bytearray methods in this section do *not* operate in
2884place, and instead produce new objects.
2885
2886.. method:: bytes.capitalize()
2887 bytearray.capitalize()
2888
2889 Return a copy of the sequence with each byte interpreted as an ASCII
2890 character, and the first byte capitalized and the rest lowercased.
2891 Non-ASCII byte values are passed through unchanged.
2892
2893 .. note::
2894
2895 The bytearray version of this method does *not* operate in place - it
2896 always produces a new object, even if no changes were made.
2897
2898
2899.. method:: bytes.expandtabs(tabsize=8)
2900 bytearray.expandtabs(tabsize=8)
2901
2902 Return a copy of the sequence where all ASCII tab characters are replaced
2903 by one or more ASCII spaces, depending on the current column and the given
2904 tab size. Tab positions occur every *tabsize* bytes (default is 8,
2905 giving tab positions at columns 0, 8, 16 and so on). To expand the
2906 sequence, the current column is set to zero and the sequence is examined
2907 byte by byte. If the byte is an ASCII tab character (``b'\t'``), one or
2908 more space characters are inserted in the result until the current column
2909 is equal to the next tab position. (The tab character itself is not
2910 copied.) If the current byte is an ASCII newline (``b'\n'``) or
2911 carriage return (``b'\r'``), it is copied and the current column is reset
2912 to zero. Any other byte value is copied unchanged and the current column
2913 is incremented by one regardless of how the byte value is represented when
2914 printed::
2915
2916 >>> b'01\t012\t0123\t01234'.expandtabs()
2917 b'01 012 0123 01234'
2918 >>> b'01\t012\t0123\t01234'.expandtabs(4)
2919 b'01 012 0123 01234'
2920
2921 .. note::
2922
2923 The bytearray version of this method does *not* operate in place - it
2924 always produces a new object, even if no changes were made.
2925
2926
2927.. method:: bytes.isalnum()
2928 bytearray.isalnum()
2929
2930 Return true if all bytes in the sequence are alphabetical ASCII characters
2931 or ASCII decimal digits and the sequence is not empty, false otherwise.
2932 Alphabetic ASCII characters are those byte values in the sequence
2933 ``b'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'``. ASCII decimal
2934 digits are those byte values in the sequence ``b'0123456789'``.
2935
2936 For example::
2937
2938 >>> b'ABCabc1'.isalnum()
2939 True
2940 >>> b'ABC abc1'.isalnum()
2941 False
2942
2943
2944.. method:: bytes.isalpha()
2945 bytearray.isalpha()
2946
2947 Return true if all bytes in the sequence are alphabetic ASCII characters
2948 and the sequence is not empty, false otherwise. Alphabetic ASCII
2949 characters are those byte values in the sequence
2950 ``b'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'``.
2951
2952 For example::
2953
2954 >>> b'ABCabc'.isalpha()
2955 True
2956 >>> b'ABCabc1'.isalpha()
2957 False
2958
2959
INADA Naokia49ac992018-01-27 14:06:21 +09002960.. method:: bytes.isascii()
2961 bytearray.isascii()
2962
2963 Return true if the sequence is empty or all bytes in the sequence are ASCII,
2964 false otherwise.
2965 ASCII bytes are in the range 0-0x7F.
2966
2967 .. versionadded:: 3.7
2968
2969
Nick Coghlane4936b82014-08-09 16:14:04 +10002970.. method:: bytes.isdigit()
2971 bytearray.isdigit()
2972
2973 Return true if all bytes in the sequence are ASCII decimal digits
2974 and the sequence is not empty, false otherwise. ASCII decimal digits are
2975 those byte values in the sequence ``b'0123456789'``.
2976
2977 For example::
2978
2979 >>> b'1234'.isdigit()
2980 True
2981 >>> b'1.23'.isdigit()
2982 False
2983
2984
2985.. method:: bytes.islower()
2986 bytearray.islower()
2987
2988 Return true if there is at least one lowercase ASCII character
2989 in the sequence and no uppercase ASCII characters, false otherwise.
2990
2991 For example::
2992
2993 >>> b'hello world'.islower()
2994 True
2995 >>> b'Hello world'.islower()
2996 False
2997
2998 Lowercase ASCII characters are those byte values in the sequence
2999 ``b'abcdefghijklmnopqrstuvwxyz'``. Uppercase ASCII characters
3000 are those byte values in the sequence ``b'ABCDEFGHIJKLMNOPQRSTUVWXYZ'``.
3001
3002
3003.. method:: bytes.isspace()
3004 bytearray.isspace()
3005
3006 Return true if all bytes in the sequence are ASCII whitespace and the
3007 sequence is not empty, false otherwise. ASCII whitespace characters are
Serhiy Storchakabf7b9ed2015-11-23 16:43:05 +02003008 those byte values in the sequence ``b' \t\n\r\x0b\f'`` (space, tab, newline,
Nick Coghlane4936b82014-08-09 16:14:04 +10003009 carriage return, vertical tab, form feed).
3010
3011
3012.. method:: bytes.istitle()
3013 bytearray.istitle()
3014
3015 Return true if the sequence is ASCII titlecase and the sequence is not
3016 empty, false otherwise. See :meth:`bytes.title` for more details on the
3017 definition of "titlecase".
3018
3019 For example::
3020
3021 >>> b'Hello World'.istitle()
3022 True
3023 >>> b'Hello world'.istitle()
3024 False
3025
3026
3027.. method:: bytes.isupper()
3028 bytearray.isupper()
3029
Zachary Ware0b496372015-02-27 01:40:22 -06003030 Return true if there is at least one uppercase alphabetic ASCII character
3031 in the sequence and no lowercase ASCII characters, false otherwise.
Nick Coghlane4936b82014-08-09 16:14:04 +10003032
3033 For example::
3034
3035 >>> b'HELLO WORLD'.isupper()
3036 True
3037 >>> b'Hello world'.isupper()
3038 False
3039
3040 Lowercase ASCII characters are those byte values in the sequence
3041 ``b'abcdefghijklmnopqrstuvwxyz'``. Uppercase ASCII characters
3042 are those byte values in the sequence ``b'ABCDEFGHIJKLMNOPQRSTUVWXYZ'``.
3043
3044
3045.. method:: bytes.lower()
3046 bytearray.lower()
3047
3048 Return a copy of the sequence with all the uppercase ASCII characters
3049 converted to their corresponding lowercase counterpart.
3050
3051 For example::
3052
3053 >>> b'Hello World'.lower()
3054 b'hello world'
3055
3056 Lowercase ASCII characters are those byte values in the sequence
3057 ``b'abcdefghijklmnopqrstuvwxyz'``. Uppercase ASCII characters
3058 are those byte values in the sequence ``b'ABCDEFGHIJKLMNOPQRSTUVWXYZ'``.
3059
3060 .. note::
3061
3062 The bytearray version of this method does *not* operate in place - it
3063 always produces a new object, even if no changes were made.
3064
3065
3066.. index::
3067 single: universal newlines; bytes.splitlines method
3068 single: universal newlines; bytearray.splitlines method
3069
3070.. method:: bytes.splitlines(keepends=False)
3071 bytearray.splitlines(keepends=False)
3072
3073 Return a list of the lines in the binary sequence, breaking at ASCII
3074 line boundaries. This method uses the :term:`universal newlines` approach
3075 to splitting lines. Line breaks are not included in the resulting list
3076 unless *keepends* is given and true.
3077
3078 For example::
3079
3080 >>> b'ab c\n\nde fg\rkl\r\n'.splitlines()
Larry Hastingsc6256e52014-10-05 19:03:48 -07003081 [b'ab c', b'', b'de fg', b'kl']
Nick Coghlane4936b82014-08-09 16:14:04 +10003082 >>> b'ab c\n\nde fg\rkl\r\n'.splitlines(keepends=True)
3083 [b'ab c\n', b'\n', b'de fg\r', b'kl\r\n']
3084
3085 Unlike :meth:`~bytes.split` when a delimiter string *sep* is given, this
3086 method returns an empty list for the empty string, and a terminal line
3087 break does not result in an extra line::
3088
3089 >>> b"".split(b'\n'), b"Two lines\n".split(b'\n')
3090 ([b''], [b'Two lines', b''])
3091 >>> b"".splitlines(), b"One line\n".splitlines()
3092 ([], [b'One line'])
3093
3094
3095.. method:: bytes.swapcase()
3096 bytearray.swapcase()
3097
3098 Return a copy of the sequence with all the lowercase ASCII characters
3099 converted to their corresponding uppercase counterpart and vice-versa.
3100
3101 For example::
3102
3103 >>> b'Hello World'.swapcase()
3104 b'hELLO wORLD'
3105
3106 Lowercase ASCII characters are those byte values in the sequence
3107 ``b'abcdefghijklmnopqrstuvwxyz'``. Uppercase ASCII characters
3108 are those byte values in the sequence ``b'ABCDEFGHIJKLMNOPQRSTUVWXYZ'``.
3109
3110 Unlike :func:`str.swapcase()`, it is always the case that
3111 ``bin.swapcase().swapcase() == bin`` for the binary versions. Case
3112 conversions are symmetrical in ASCII, even though that is not generally
3113 true for arbitrary Unicode code points.
3114
3115 .. note::
3116
3117 The bytearray version of this method does *not* operate in place - it
3118 always produces a new object, even if no changes were made.
3119
3120
3121.. method:: bytes.title()
3122 bytearray.title()
3123
3124 Return a titlecased version of the binary sequence where words start with
3125 an uppercase ASCII character and the remaining characters are lowercase.
3126 Uncased byte values are left unmodified.
3127
3128 For example::
3129
3130 >>> b'Hello world'.title()
3131 b'Hello World'
3132
3133 Lowercase ASCII characters are those byte values in the sequence
3134 ``b'abcdefghijklmnopqrstuvwxyz'``. Uppercase ASCII characters
3135 are those byte values in the sequence ``b'ABCDEFGHIJKLMNOPQRSTUVWXYZ'``.
3136 All other byte values are uncased.
3137
3138 The algorithm uses a simple language-independent definition of a word as
3139 groups of consecutive letters. The definition works in many contexts but
3140 it means that apostrophes in contractions and possessives form word
3141 boundaries, which may not be the desired result::
3142
3143 >>> b"they're bill's friends from the UK".title()
3144 b"They'Re Bill'S Friends From The Uk"
3145
3146 A workaround for apostrophes can be constructed using regular expressions::
3147
3148 >>> import re
3149 >>> def titlecase(s):
3150 ... return re.sub(rb"[A-Za-z]+('[A-Za-z]+)?",
3151 ... lambda mo: mo.group(0)[0:1].upper() +
3152 ... mo.group(0)[1:].lower(),
3153 ... s)
3154 ...
3155 >>> titlecase(b"they're bill's friends.")
3156 b"They're Bill's Friends."
3157
3158 .. note::
3159
3160 The bytearray version of this method does *not* operate in place - it
3161 always produces a new object, even if no changes were made.
3162
3163
3164.. method:: bytes.upper()
3165 bytearray.upper()
3166
3167 Return a copy of the sequence with all the lowercase ASCII characters
3168 converted to their corresponding uppercase counterpart.
3169
3170 For example::
3171
3172 >>> b'Hello World'.upper()
3173 b'HELLO WORLD'
3174
3175 Lowercase ASCII characters are those byte values in the sequence
3176 ``b'abcdefghijklmnopqrstuvwxyz'``. Uppercase ASCII characters
3177 are those byte values in the sequence ``b'ABCDEFGHIJKLMNOPQRSTUVWXYZ'``.
3178
3179 .. note::
3180
3181 The bytearray version of this method does *not* operate in place - it
3182 always produces a new object, even if no changes were made.
3183
3184
3185.. method:: bytes.zfill(width)
3186 bytearray.zfill(width)
3187
3188 Return a copy of the sequence left filled with ASCII ``b'0'`` digits to
3189 make a sequence of length *width*. A leading sign prefix (``b'+'``/
3190 ``b'-'`` is handled by inserting the padding *after* the sign character
3191 rather than before. For :class:`bytes` objects, the original sequence is
3192 returned if *width* is less than or equal to ``len(seq)``.
3193
3194 For example::
3195
3196 >>> b"42".zfill(5)
3197 b'00042'
3198 >>> b"-42".zfill(5)
3199 b'-0042'
3200
3201 .. note::
3202
3203 The bytearray version of this method does *not* operate in place - it
3204 always produces a new object, even if no changes were made.
Georg Brandlabc38772009-04-12 15:51:51 +00003205
3206
Ethan Furmanb95b5612015-01-23 20:05:18 -08003207.. _bytes-formatting:
3208
3209``printf``-style Bytes Formatting
3210----------------------------------
3211
3212.. index::
3213 single: formatting, bytes (%)
3214 single: formatting, bytearray (%)
3215 single: interpolation, bytes (%)
3216 single: interpolation, bytearray (%)
3217 single: bytes; formatting
3218 single: bytearray; formatting
3219 single: bytes; interpolation
3220 single: bytearray; interpolation
3221 single: printf-style formatting
3222 single: sprintf-style formatting
3223 single: % formatting
3224 single: % interpolation
3225
3226.. note::
3227
3228 The formatting operations described here exhibit a variety of quirks that
3229 lead to a number of common errors (such as failing to display tuples and
3230 dictionaries correctly). If the value being printed may be a tuple or
3231 dictionary, wrap it in a tuple.
3232
3233Bytes objects (``bytes``/``bytearray``) have one unique built-in operation:
3234the ``%`` operator (modulo).
3235This is also known as the bytes *formatting* or *interpolation* operator.
3236Given ``format % values`` (where *format* is a bytes object), ``%`` conversion
3237specifications in *format* are replaced with zero or more elements of *values*.
3238The effect is similar to using the :c:func:`sprintf` in the C language.
3239
3240If *format* requires a single argument, *values* may be a single non-tuple
3241object. [5]_ Otherwise, *values* must be a tuple with exactly the number of
3242items specified by the format bytes object, or a single mapping object (for
3243example, a dictionary).
3244
3245A conversion specifier contains two or more characters and has the following
3246components, which must occur in this order:
3247
3248#. The ``'%'`` character, which marks the start of the specifier.
3249
3250#. Mapping key (optional), consisting of a parenthesised sequence of characters
3251 (for example, ``(somename)``).
3252
3253#. Conversion flags (optional), which affect the result of some conversion
3254 types.
3255
3256#. Minimum field width (optional). If specified as an ``'*'`` (asterisk), the
3257 actual width is read from the next element of the tuple in *values*, and the
3258 object to convert comes after the minimum field width and optional precision.
3259
3260#. Precision (optional), given as a ``'.'`` (dot) followed by the precision. If
3261 specified as ``'*'`` (an asterisk), the actual precision is read from the next
3262 element of the tuple in *values*, and the value to convert comes after the
3263 precision.
3264
3265#. Length modifier (optional).
3266
3267#. Conversion type.
3268
3269When the right argument is a dictionary (or other mapping type), then the
3270formats in the bytes object *must* include a parenthesised mapping key into that
3271dictionary inserted immediately after the ``'%'`` character. The mapping key
3272selects the value to be formatted from the mapping. For example:
3273
3274 >>> print(b'%(language)s has %(number)03d quote types.' %
3275 ... {b'language': b"Python", b"number": 2})
3276 b'Python has 002 quote types.'
3277
3278In this case no ``*`` specifiers may occur in a format (since they require a
3279sequential parameter list).
3280
3281The conversion flag characters are:
3282
3283+---------+---------------------------------------------------------------------+
3284| Flag | Meaning |
3285+=========+=====================================================================+
3286| ``'#'`` | The value conversion will use the "alternate form" (where defined |
3287| | below). |
3288+---------+---------------------------------------------------------------------+
3289| ``'0'`` | The conversion will be zero padded for numeric values. |
3290+---------+---------------------------------------------------------------------+
3291| ``'-'`` | The converted value is left adjusted (overrides the ``'0'`` |
3292| | conversion if both are given). |
3293+---------+---------------------------------------------------------------------+
3294| ``' '`` | (a space) A blank should be left before a positive number (or empty |
3295| | string) produced by a signed conversion. |
3296+---------+---------------------------------------------------------------------+
3297| ``'+'`` | A sign character (``'+'`` or ``'-'``) will precede the conversion |
3298| | (overrides a "space" flag). |
3299+---------+---------------------------------------------------------------------+
3300
3301A length modifier (``h``, ``l``, or ``L``) may be present, but is ignored as it
3302is not necessary for Python -- so e.g. ``%ld`` is identical to ``%d``.
3303
3304The conversion types are:
3305
3306+------------+-----------------------------------------------------+-------+
3307| Conversion | Meaning | Notes |
3308+============+=====================================================+=======+
3309| ``'d'`` | Signed integer decimal. | |
3310+------------+-----------------------------------------------------+-------+
3311| ``'i'`` | Signed integer decimal. | |
3312+------------+-----------------------------------------------------+-------+
3313| ``'o'`` | Signed octal value. | \(1) |
3314+------------+-----------------------------------------------------+-------+
Ethan Furman62e977f2015-03-11 08:17:00 -07003315| ``'u'`` | Obsolete type -- it is identical to ``'d'``. | \(8) |
Ethan Furmanb95b5612015-01-23 20:05:18 -08003316+------------+-----------------------------------------------------+-------+
3317| ``'x'`` | Signed hexadecimal (lowercase). | \(2) |
3318+------------+-----------------------------------------------------+-------+
3319| ``'X'`` | Signed hexadecimal (uppercase). | \(2) |
3320+------------+-----------------------------------------------------+-------+
3321| ``'e'`` | Floating point exponential format (lowercase). | \(3) |
3322+------------+-----------------------------------------------------+-------+
3323| ``'E'`` | Floating point exponential format (uppercase). | \(3) |
3324+------------+-----------------------------------------------------+-------+
3325| ``'f'`` | Floating point decimal format. | \(3) |
3326+------------+-----------------------------------------------------+-------+
3327| ``'F'`` | Floating point decimal format. | \(3) |
3328+------------+-----------------------------------------------------+-------+
3329| ``'g'`` | Floating point format. Uses lowercase exponential | \(4) |
3330| | format if exponent is less than -4 or not less than | |
3331| | precision, decimal format otherwise. | |
3332+------------+-----------------------------------------------------+-------+
3333| ``'G'`` | Floating point format. Uses uppercase exponential | \(4) |
3334| | format if exponent is less than -4 or not less than | |
3335| | precision, decimal format otherwise. | |
3336+------------+-----------------------------------------------------+-------+
3337| ``'c'`` | Single byte (accepts integer or single | |
3338| | byte objects). | |
3339+------------+-----------------------------------------------------+-------+
3340| ``'b'`` | Bytes (any object that follows the | \(5) |
3341| | :ref:`buffer protocol <bufferobjects>` or has | |
3342| | :meth:`__bytes__`). | |
3343+------------+-----------------------------------------------------+-------+
3344| ``'s'`` | ``'s'`` is an alias for ``'b'`` and should only | \(6) |
3345| | be used for Python2/3 code bases. | |
3346+------------+-----------------------------------------------------+-------+
3347| ``'a'`` | Bytes (converts any Python object using | \(5) |
3348| | ``repr(obj).encode('ascii','backslashreplace)``). | |
3349+------------+-----------------------------------------------------+-------+
Ethan Furman62e977f2015-03-11 08:17:00 -07003350| ``'r'`` | ``'r'`` is an alias for ``'a'`` and should only | \(7) |
3351| | be used for Python2/3 code bases. | |
3352+------------+-----------------------------------------------------+-------+
Ethan Furmanb95b5612015-01-23 20:05:18 -08003353| ``'%'`` | No argument is converted, results in a ``'%'`` | |
3354| | character in the result. | |
3355+------------+-----------------------------------------------------+-------+
3356
3357Notes:
3358
3359(1)
Martin Panter41176ae2016-12-11 01:07:29 +00003360 The alternate form causes a leading octal specifier (``'0o'``) to be
3361 inserted before the first digit.
Ethan Furmanb95b5612015-01-23 20:05:18 -08003362
3363(2)
3364 The alternate form causes a leading ``'0x'`` or ``'0X'`` (depending on whether
Martin Panter41176ae2016-12-11 01:07:29 +00003365 the ``'x'`` or ``'X'`` format was used) to be inserted before the first digit.
Ethan Furmanb95b5612015-01-23 20:05:18 -08003366
3367(3)
3368 The alternate form causes the result to always contain a decimal point, even if
3369 no digits follow it.
3370
3371 The precision determines the number of digits after the decimal point and
3372 defaults to 6.
3373
3374(4)
3375 The alternate form causes the result to always contain a decimal point, and
3376 trailing zeroes are not removed as they would otherwise be.
3377
3378 The precision determines the number of significant digits before and after the
3379 decimal point and defaults to 6.
3380
3381(5)
3382 If precision is ``N``, the output is truncated to ``N`` characters.
3383
3384(6)
3385 ``b'%s'`` is deprecated, but will not be removed during the 3.x series.
3386
3387(7)
Ethan Furman62e977f2015-03-11 08:17:00 -07003388 ``b'%r'`` is deprecated, but will not be removed during the 3.x series.
3389
3390(8)
Ethan Furmanb95b5612015-01-23 20:05:18 -08003391 See :pep:`237`.
3392
3393.. note::
3394
3395 The bytearray version of this method does *not* operate in place - it
3396 always produces a new object, even if no changes were made.
3397
Andrés Delfinoa9d0b342018-06-15 16:42:20 -03003398.. seealso::
3399
3400 :pep:`461` - Adding % formatting to bytes and bytearray
3401
Ethan Furmanb95b5612015-01-23 20:05:18 -08003402.. versionadded:: 3.5
3403
Nick Coghlan273069c2012-08-20 17:14:07 +10003404.. _typememoryview:
3405
3406Memory Views
3407------------
3408
3409:class:`memoryview` objects allow Python code to access the internal data
3410of an object that supports the :ref:`buffer protocol <bufferobjects>` without
3411copying.
3412
3413.. class:: memoryview(obj)
3414
3415 Create a :class:`memoryview` that references *obj*. *obj* must support the
3416 buffer protocol. Built-in objects that support the buffer protocol include
3417 :class:`bytes` and :class:`bytearray`.
3418
3419 A :class:`memoryview` has the notion of an *element*, which is the
3420 atomic memory unit handled by the originating object *obj*. For many
3421 simple types such as :class:`bytes` and :class:`bytearray`, an element
3422 is a single byte, but other types such as :class:`array.array` may have
3423 bigger elements.
3424
3425 ``len(view)`` is equal to the length of :class:`~memoryview.tolist`.
3426 If ``view.ndim = 0``, the length is 1. If ``view.ndim = 1``, the length
3427 is equal to the number of elements in the view. For higher dimensions,
3428 the length is equal to the length of the nested list representation of
3429 the view. The :class:`~memoryview.itemsize` attribute will give you the
3430 number of bytes in a single element.
3431
Antoine Pitrou31084ba2015-03-19 23:29:36 +01003432 A :class:`memoryview` supports slicing and indexing to expose its data.
3433 One-dimensional slicing will result in a subview::
Nick Coghlan273069c2012-08-20 17:14:07 +10003434
3435 >>> v = memoryview(b'abcefg')
3436 >>> v[1]
3437 98
3438 >>> v[-1]
3439 103
3440 >>> v[1:4]
3441 <memory at 0x7f3ddc9f4350>
3442 >>> bytes(v[1:4])
3443 b'bce'
3444
Antoine Pitrou31084ba2015-03-19 23:29:36 +01003445 If :class:`~memoryview.format` is one of the native format specifiers
3446 from the :mod:`struct` module, indexing with an integer or a tuple of
3447 integers is also supported and returns a single *element* with
3448 the correct type. One-dimensional memoryviews can be indexed
3449 with an integer or a one-integer tuple. Multi-dimensional memoryviews
3450 can be indexed with tuples of exactly *ndim* integers where *ndim* is
3451 the number of dimensions. Zero-dimensional memoryviews can be indexed
3452 with the empty tuple.
3453
3454 Here is an example with a non-byte format::
Nick Coghlan273069c2012-08-20 17:14:07 +10003455
3456 >>> import array
3457 >>> a = array.array('l', [-11111111, 22222222, -33333333, 44444444])
Antoine Pitrou31084ba2015-03-19 23:29:36 +01003458 >>> m = memoryview(a)
3459 >>> m[0]
Nick Coghlan273069c2012-08-20 17:14:07 +10003460 -11111111
Antoine Pitrou31084ba2015-03-19 23:29:36 +01003461 >>> m[-1]
Nick Coghlan273069c2012-08-20 17:14:07 +10003462 44444444
Antoine Pitrou31084ba2015-03-19 23:29:36 +01003463 >>> m[::2].tolist()
Nick Coghlan273069c2012-08-20 17:14:07 +10003464 [-11111111, -33333333]
Nick Coghlan273069c2012-08-20 17:14:07 +10003465
Antoine Pitrou31084ba2015-03-19 23:29:36 +01003466 If the underlying object is writable, the memoryview supports
3467 one-dimensional slice assignment. Resizing is not allowed::
Nick Coghlan273069c2012-08-20 17:14:07 +10003468
3469 >>> data = bytearray(b'abcefg')
3470 >>> v = memoryview(data)
3471 >>> v.readonly
3472 False
3473 >>> v[0] = ord(b'z')
3474 >>> data
3475 bytearray(b'zbcefg')
3476 >>> v[1:4] = b'123'
3477 >>> data
3478 bytearray(b'z123fg')
3479 >>> v[2:3] = b'spam'
3480 Traceback (most recent call last):
3481 File "<stdin>", line 1, in <module>
3482 ValueError: memoryview assignment: lvalue and rvalue have different structures
3483 >>> v[2:6] = b'spam'
3484 >>> data
3485 bytearray(b'z1spam')
3486
Stefan Kraha3b84fb2012-09-02 14:50:56 +02003487 One-dimensional memoryviews of hashable (read-only) types with formats
3488 'B', 'b' or 'c' are also hashable. The hash is defined as
3489 ``hash(m) == hash(m.tobytes())``::
Nick Coghlan273069c2012-08-20 17:14:07 +10003490
3491 >>> v = memoryview(b'abcefg')
3492 >>> hash(v) == hash(b'abcefg')
3493 True
3494 >>> hash(v[2:4]) == hash(b'ce')
3495 True
3496 >>> hash(v[::-2]) == hash(b'abcefg'[::-2])
3497 True
3498
Nick Coghlan273069c2012-08-20 17:14:07 +10003499 .. versionchanged:: 3.3
Antoine Pitrou31084ba2015-03-19 23:29:36 +01003500 One-dimensional memoryviews can now be sliced.
Stefan Kraha3b84fb2012-09-02 14:50:56 +02003501 One-dimensional memoryviews with formats 'B', 'b' or 'c' are now hashable.
Nick Coghlan273069c2012-08-20 17:14:07 +10003502
Nick Coghlan45163cc2013-10-02 22:31:47 +10003503 .. versionchanged:: 3.4
3504 memoryview is now registered automatically with
3505 :class:`collections.abc.Sequence`
3506
Antoine Pitrou31084ba2015-03-19 23:29:36 +01003507 .. versionchanged:: 3.5
3508 memoryviews can now be indexed with tuple of integers.
3509
Nick Coghlan273069c2012-08-20 17:14:07 +10003510 :class:`memoryview` has several methods:
3511
Nick Coghlan06e1ab02012-08-25 17:59:50 +10003512 .. method:: __eq__(exporter)
3513
3514 A memoryview and a :pep:`3118` exporter are equal if their shapes are
3515 equivalent and if all corresponding values are equal when the operands'
3516 respective format codes are interpreted using :mod:`struct` syntax.
3517
3518 For the subset of :mod:`struct` format strings currently supported by
3519 :meth:`tolist`, ``v`` and ``w`` are equal if ``v.tolist() == w.tolist()``::
3520
3521 >>> import array
3522 >>> a = array.array('I', [1, 2, 3, 4, 5])
3523 >>> b = array.array('d', [1.0, 2.0, 3.0, 4.0, 5.0])
3524 >>> c = array.array('b', [5, 3, 1])
3525 >>> x = memoryview(a)
3526 >>> y = memoryview(b)
3527 >>> x == a == y == b
3528 True
3529 >>> x.tolist() == a.tolist() == y.tolist() == b.tolist()
3530 True
3531 >>> z = y[::-2]
3532 >>> z == c
3533 True
3534 >>> z.tolist() == c.tolist()
3535 True
3536
3537 If either format string is not supported by the :mod:`struct` module,
3538 then the objects will always compare as unequal (even if the format
3539 strings and buffer contents are identical)::
3540
3541 >>> from ctypes import BigEndianStructure, c_long
3542 >>> class BEPoint(BigEndianStructure):
3543 ... _fields_ = [("x", c_long), ("y", c_long)]
3544 ...
3545 >>> point = BEPoint(100, 200)
3546 >>> a = memoryview(point)
3547 >>> b = memoryview(point)
3548 >>> a == point
3549 False
3550 >>> a == b
3551 False
3552
3553 Note that, as with floating point numbers, ``v is w`` does *not* imply
3554 ``v == w`` for memoryview objects.
3555
3556 .. versionchanged:: 3.3
Stefan Krahab0c3c72012-08-30 12:09:09 +02003557 Previous versions compared the raw memory disregarding the item format
3558 and the logical array structure.
Nick Coghlan06e1ab02012-08-25 17:59:50 +10003559
Nick Coghlan273069c2012-08-20 17:14:07 +10003560 .. method:: tobytes()
3561
3562 Return the data in the buffer as a bytestring. This is equivalent to
3563 calling the :class:`bytes` constructor on the memoryview. ::
3564
3565 >>> m = memoryview(b"abc")
3566 >>> m.tobytes()
3567 b'abc'
3568 >>> bytes(m)
3569 b'abc'
3570
3571 For non-contiguous arrays the result is equal to the flattened list
Nick Coghlan06e1ab02012-08-25 17:59:50 +10003572 representation with all elements converted to bytes. :meth:`tobytes`
3573 supports all format strings, including those that are not in
3574 :mod:`struct` module syntax.
Nick Coghlan273069c2012-08-20 17:14:07 +10003575
Gregory P. Smith8cb65692015-04-25 23:22:26 +00003576 .. method:: hex()
3577
3578 Return a string object containing two hexadecimal digits for each
3579 byte in the buffer. ::
3580
3581 >>> m = memoryview(b"abc")
3582 >>> m.hex()
3583 '616263'
3584
3585 .. versionadded:: 3.5
3586
Nick Coghlan273069c2012-08-20 17:14:07 +10003587 .. method:: tolist()
3588
3589 Return the data in the buffer as a list of elements. ::
3590
3591 >>> memoryview(b'abc').tolist()
3592 [97, 98, 99]
3593 >>> import array
3594 >>> a = array.array('d', [1.1, 2.2, 3.3])
3595 >>> m = memoryview(a)
3596 >>> m.tolist()
3597 [1.1, 2.2, 3.3]
3598
Stefan Krahab0c3c72012-08-30 12:09:09 +02003599 .. versionchanged:: 3.3
3600 :meth:`tolist` now supports all single character native formats in
3601 :mod:`struct` module syntax as well as multi-dimensional
3602 representations.
Nick Coghlan06e1ab02012-08-25 17:59:50 +10003603
Antoine Pitrou480ab052018-04-14 19:49:21 +02003604 .. method:: toreadonly()
3605
3606 Return a readonly version of the memoryview object. The original
3607 memoryview object is unchanged. ::
3608
3609 >>> m = memoryview(bytearray(b'abc'))
3610 >>> mm = m.toreadonly()
3611 >>> mm.tolist()
3612 [89, 98, 99]
3613 >>> mm[0] = 42
3614 Traceback (most recent call last):
3615 File "<stdin>", line 1, in <module>
3616 TypeError: cannot modify read-only memory
3617 >>> m[0] = 43
3618 >>> mm.tolist()
3619 [43, 98, 99]
3620
3621 .. versionadded:: 3.8
3622
Nick Coghlan273069c2012-08-20 17:14:07 +10003623 .. method:: release()
3624
3625 Release the underlying buffer exposed by the memoryview object. Many
3626 objects take special actions when a view is held on them (for example,
3627 a :class:`bytearray` would temporarily forbid resizing); therefore,
3628 calling release() is handy to remove these restrictions (and free any
3629 dangling resources) as soon as possible.
3630
3631 After this method has been called, any further operation on the view
3632 raises a :class:`ValueError` (except :meth:`release()` itself which can
3633 be called multiple times)::
3634
3635 >>> m = memoryview(b'abc')
3636 >>> m.release()
3637 >>> m[0]
3638 Traceback (most recent call last):
3639 File "<stdin>", line 1, in <module>
3640 ValueError: operation forbidden on released memoryview object
3641
3642 The context management protocol can be used for a similar effect,
3643 using the ``with`` statement::
3644
3645 >>> with memoryview(b'abc') as m:
3646 ... m[0]
3647 ...
3648 97
3649 >>> m[0]
3650 Traceback (most recent call last):
3651 File "<stdin>", line 1, in <module>
3652 ValueError: operation forbidden on released memoryview object
3653
3654 .. versionadded:: 3.2
3655
3656 .. method:: cast(format[, shape])
3657
3658 Cast a memoryview to a new format or shape. *shape* defaults to
3659 ``[byte_length//new_itemsize]``, which means that the result view
3660 will be one-dimensional. The return value is a new memoryview, but
Stefan Krah70e543b2015-08-08 14:33:28 +02003661 the buffer itself is not copied. Supported casts are 1D -> C-:term:`contiguous`
Nick Coghlan06e1ab02012-08-25 17:59:50 +10003662 and C-contiguous -> 1D.
3663
Stefan Krah0c515952015-08-08 13:38:10 +02003664 The destination format is restricted to a single element native format in
Nick Coghlan06e1ab02012-08-25 17:59:50 +10003665 :mod:`struct` syntax. One of the formats must be a byte format
Nick Coghlan273069c2012-08-20 17:14:07 +10003666 ('B', 'b' or 'c'). The byte length of the result must be the same
3667 as the original length.
3668
3669 Cast 1D/long to 1D/unsigned bytes::
3670
3671 >>> import array
3672 >>> a = array.array('l', [1,2,3])
3673 >>> x = memoryview(a)
3674 >>> x.format
3675 'l'
3676 >>> x.itemsize
3677 8
3678 >>> len(x)
3679 3
3680 >>> x.nbytes
3681 24
3682 >>> y = x.cast('B')
3683 >>> y.format
3684 'B'
3685 >>> y.itemsize
3686 1
3687 >>> len(y)
3688 24
3689 >>> y.nbytes
3690 24
3691
3692 Cast 1D/unsigned bytes to 1D/char::
3693
3694 >>> b = bytearray(b'zyz')
3695 >>> x = memoryview(b)
3696 >>> x[0] = b'a'
3697 Traceback (most recent call last):
3698 File "<stdin>", line 1, in <module>
3699 ValueError: memoryview: invalid value for format "B"
3700 >>> y = x.cast('c')
3701 >>> y[0] = b'a'
3702 >>> b
3703 bytearray(b'ayz')
3704
3705 Cast 1D/bytes to 3D/ints to 1D/signed char::
3706
3707 >>> import struct
3708 >>> buf = struct.pack("i"*12, *list(range(12)))
3709 >>> x = memoryview(buf)
3710 >>> y = x.cast('i', shape=[2,2,3])
3711 >>> y.tolist()
3712 [[[0, 1, 2], [3, 4, 5]], [[6, 7, 8], [9, 10, 11]]]
3713 >>> y.format
3714 'i'
3715 >>> y.itemsize
3716 4
3717 >>> len(y)
3718 2
3719 >>> y.nbytes
3720 48
3721 >>> z = y.cast('b')
3722 >>> z.format
3723 'b'
3724 >>> z.itemsize
3725 1
3726 >>> len(z)
3727 48
3728 >>> z.nbytes
3729 48
3730
Terry Jan Reedy0f847642013-03-11 18:34:00 -04003731 Cast 1D/unsigned char to 2D/unsigned long::
Nick Coghlan273069c2012-08-20 17:14:07 +10003732
3733 >>> buf = struct.pack("L"*6, *list(range(6)))
3734 >>> x = memoryview(buf)
3735 >>> y = x.cast('L', shape=[2,3])
3736 >>> len(y)
3737 2
3738 >>> y.nbytes
3739 48
3740 >>> y.tolist()
3741 [[0, 1, 2], [3, 4, 5]]
3742
3743 .. versionadded:: 3.3
3744
Stefan Krah0c515952015-08-08 13:38:10 +02003745 .. versionchanged:: 3.5
3746 The source format is no longer restricted when casting to a byte view.
3747
Nick Coghlan273069c2012-08-20 17:14:07 +10003748 There are also several readonly attributes available:
3749
3750 .. attribute:: obj
3751
3752 The underlying object of the memoryview::
3753
3754 >>> b = bytearray(b'xyz')
3755 >>> m = memoryview(b)
3756 >>> m.obj is b
3757 True
3758
3759 .. versionadded:: 3.3
3760
3761 .. attribute:: nbytes
3762
3763 ``nbytes == product(shape) * itemsize == len(m.tobytes())``. This is
3764 the amount of space in bytes that the array would use in a contiguous
3765 representation. It is not necessarily equal to len(m)::
3766
3767 >>> import array
3768 >>> a = array.array('i', [1,2,3,4,5])
3769 >>> m = memoryview(a)
3770 >>> len(m)
3771 5
3772 >>> m.nbytes
3773 20
3774 >>> y = m[::2]
3775 >>> len(y)
3776 3
3777 >>> y.nbytes
3778 12
3779 >>> len(y.tobytes())
3780 12
3781
3782 Multi-dimensional arrays::
3783
3784 >>> import struct
3785 >>> buf = struct.pack("d"*12, *[1.5*x for x in range(12)])
3786 >>> x = memoryview(buf)
3787 >>> y = x.cast('d', shape=[3,4])
3788 >>> y.tolist()
3789 [[0.0, 1.5, 3.0, 4.5], [6.0, 7.5, 9.0, 10.5], [12.0, 13.5, 15.0, 16.5]]
3790 >>> len(y)
3791 3
3792 >>> y.nbytes
3793 96
3794
3795 .. versionadded:: 3.3
3796
3797 .. attribute:: readonly
3798
3799 A bool indicating whether the memory is read only.
3800
3801 .. attribute:: format
3802
3803 A string containing the format (in :mod:`struct` module style) for each
3804 element in the view. A memoryview can be created from exporters with
3805 arbitrary format strings, but some methods (e.g. :meth:`tolist`) are
Nick Coghlan06e1ab02012-08-25 17:59:50 +10003806 restricted to native single element formats.
Nick Coghlan273069c2012-08-20 17:14:07 +10003807
Stefan Krahab0c3c72012-08-30 12:09:09 +02003808 .. versionchanged:: 3.3
3809 format ``'B'`` is now handled according to the struct module syntax.
3810 This means that ``memoryview(b'abc')[0] == b'abc'[0] == 97``.
3811
Nick Coghlan273069c2012-08-20 17:14:07 +10003812 .. attribute:: itemsize
3813
3814 The size in bytes of each element of the memoryview::
3815
3816 >>> import array, struct
3817 >>> m = memoryview(array.array('H', [32000, 32001, 32002]))
3818 >>> m.itemsize
3819 2
3820 >>> m[0]
3821 32000
3822 >>> struct.calcsize('H') == m.itemsize
3823 True
3824
3825 .. attribute:: ndim
3826
3827 An integer indicating how many dimensions of a multi-dimensional array the
3828 memory represents.
3829
3830 .. attribute:: shape
3831
3832 A tuple of integers the length of :attr:`ndim` giving the shape of the
Alexander Belopolskye8677c02012-09-03 17:29:22 -04003833 memory as an N-dimensional array.
3834
3835 .. versionchanged:: 3.3
Serhiy Storchakaecf41da2016-10-19 16:29:26 +03003836 An empty tuple instead of ``None`` when ndim = 0.
Nick Coghlan273069c2012-08-20 17:14:07 +10003837
3838 .. attribute:: strides
3839
3840 A tuple of integers the length of :attr:`ndim` giving the size in bytes to
3841 access each element for each dimension of the array.
3842
Alexander Belopolskye8677c02012-09-03 17:29:22 -04003843 .. versionchanged:: 3.3
Serhiy Storchakaecf41da2016-10-19 16:29:26 +03003844 An empty tuple instead of ``None`` when ndim = 0.
Alexander Belopolskye8677c02012-09-03 17:29:22 -04003845
Nick Coghlan273069c2012-08-20 17:14:07 +10003846 .. attribute:: suboffsets
3847
3848 Used internally for PIL-style arrays. The value is informational only.
3849
3850 .. attribute:: c_contiguous
3851
Stefan Krah70e543b2015-08-08 14:33:28 +02003852 A bool indicating whether the memory is C-:term:`contiguous`.
Nick Coghlan273069c2012-08-20 17:14:07 +10003853
3854 .. versionadded:: 3.3
3855
3856 .. attribute:: f_contiguous
3857
Stefan Krah70e543b2015-08-08 14:33:28 +02003858 A bool indicating whether the memory is Fortran :term:`contiguous`.
Nick Coghlan273069c2012-08-20 17:14:07 +10003859
3860 .. versionadded:: 3.3
3861
3862 .. attribute:: contiguous
3863
Stefan Krah70e543b2015-08-08 14:33:28 +02003864 A bool indicating whether the memory is :term:`contiguous`.
Nick Coghlan273069c2012-08-20 17:14:07 +10003865
3866 .. versionadded:: 3.3
3867
3868
Georg Brandl116aa622007-08-15 14:28:22 +00003869.. _types-set:
3870
3871Set Types --- :class:`set`, :class:`frozenset`
3872==============================================
3873
3874.. index:: object: set
3875
Guido van Rossum2cc30da2007-11-02 23:46:40 +00003876A :dfn:`set` object is an unordered collection of distinct :term:`hashable` objects.
Georg Brandl116aa622007-08-15 14:28:22 +00003877Common uses include membership testing, removing duplicates from a sequence, and
3878computing mathematical operations such as intersection, union, difference, and
3879symmetric difference.
Nick Coghlan83c0ae52012-08-21 17:42:52 +10003880(For other containers see the built-in :class:`dict`, :class:`list`,
Georg Brandl116aa622007-08-15 14:28:22 +00003881and :class:`tuple` classes, and the :mod:`collections` module.)
3882
Georg Brandl116aa622007-08-15 14:28:22 +00003883Like other collections, sets support ``x in set``, ``len(set)``, and ``for x in
3884set``. Being an unordered collection, sets do not record element position or
3885order of insertion. Accordingly, sets do not support indexing, slicing, or
3886other sequence-like behavior.
3887
Georg Brandl22b34312009-07-26 14:54:51 +00003888There are currently two built-in set types, :class:`set` and :class:`frozenset`.
Georg Brandl116aa622007-08-15 14:28:22 +00003889The :class:`set` type is mutable --- the contents can be changed using methods
Serhiy Storchaka0d196ed2013-10-09 14:02:31 +03003890like :meth:`~set.add` and :meth:`~set.remove`. Since it is mutable, it has no
3891hash value and cannot be used as either a dictionary key or as an element of
3892another set. The :class:`frozenset` type is immutable and :term:`hashable` ---
3893its contents cannot be altered after it is created; it can therefore be used as
3894a dictionary key or as an element of another set.
Georg Brandl116aa622007-08-15 14:28:22 +00003895
Georg Brandl99cd9572010-03-21 09:10:32 +00003896Non-empty sets (not frozensets) can be created by placing a comma-separated list
Georg Brandl53b95e72010-03-21 11:53:50 +00003897of elements within braces, for example: ``{'jack', 'sjoerd'}``, in addition to the
3898:class:`set` constructor.
Georg Brandl99cd9572010-03-21 09:10:32 +00003899
Georg Brandl116aa622007-08-15 14:28:22 +00003900The constructors for both classes work the same:
3901
3902.. class:: set([iterable])
3903 frozenset([iterable])
3904
3905 Return a new set or frozenset object whose elements are taken from
Andrew Svetlov9a411ce2013-04-05 16:21:50 +03003906 *iterable*. The elements of a set must be :term:`hashable`. To
3907 represent sets of sets, the inner sets must be :class:`frozenset`
3908 objects. If *iterable* is not specified, a new empty set is
3909 returned.
Georg Brandl116aa622007-08-15 14:28:22 +00003910
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00003911 Instances of :class:`set` and :class:`frozenset` provide the following
3912 operations:
Georg Brandl116aa622007-08-15 14:28:22 +00003913
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00003914 .. describe:: len(s)
Georg Brandl116aa622007-08-15 14:28:22 +00003915
Gregory P. Smithe27403b2016-02-08 09:58:40 -08003916 Return the number of elements in set *s* (cardinality of *s*).
Georg Brandl116aa622007-08-15 14:28:22 +00003917
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00003918 .. describe:: x in s
Georg Brandl116aa622007-08-15 14:28:22 +00003919
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00003920 Test *x* for membership in *s*.
Georg Brandl116aa622007-08-15 14:28:22 +00003921
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00003922 .. describe:: x not in s
Georg Brandl116aa622007-08-15 14:28:22 +00003923
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00003924 Test *x* for non-membership in *s*.
Georg Brandl116aa622007-08-15 14:28:22 +00003925
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00003926 .. method:: isdisjoint(other)
Guido van Rossum58da9312007-11-10 23:39:45 +00003927
Serhiy Storchakafbc1c262013-11-29 12:17:13 +02003928 Return ``True`` if the set has no elements in common with *other*. Sets are
Georg Brandl2ee470f2008-07-16 12:55:28 +00003929 disjoint if and only if their intersection is the empty set.
Guido van Rossum58da9312007-11-10 23:39:45 +00003930
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00003931 .. method:: issubset(other)
3932 set <= other
Georg Brandl116aa622007-08-15 14:28:22 +00003933
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00003934 Test whether every element in the set is in *other*.
Georg Brandl116aa622007-08-15 14:28:22 +00003935
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00003936 .. method:: set < other
Georg Brandla6f52782007-09-01 15:49:30 +00003937
Andrew Svetlov5bb42072012-11-01 21:47:54 +02003938 Test whether the set is a proper subset of *other*, that is,
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00003939 ``set <= other and set != other``.
Georg Brandla6f52782007-09-01 15:49:30 +00003940
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00003941 .. method:: issuperset(other)
3942 set >= other
Georg Brandl116aa622007-08-15 14:28:22 +00003943
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00003944 Test whether every element in *other* is in the set.
Georg Brandl116aa622007-08-15 14:28:22 +00003945
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00003946 .. method:: set > other
Georg Brandla6f52782007-09-01 15:49:30 +00003947
Andrew Svetlov5bb42072012-11-01 21:47:54 +02003948 Test whether the set is a proper superset of *other*, that is, ``set >=
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00003949 other and set != other``.
Georg Brandla6f52782007-09-01 15:49:30 +00003950
Raymond Hettingera33e9f72016-09-12 23:38:50 -07003951 .. method:: union(*others)
Georg Brandlc28e1fa2008-06-10 19:20:26 +00003952 set | other | ...
Georg Brandl116aa622007-08-15 14:28:22 +00003953
Benjamin Petersonb58dda72009-01-18 22:27:04 +00003954 Return a new set with elements from the set and all others.
Georg Brandl116aa622007-08-15 14:28:22 +00003955
Raymond Hettingera33e9f72016-09-12 23:38:50 -07003956 .. method:: intersection(*others)
Georg Brandlc28e1fa2008-06-10 19:20:26 +00003957 set & other & ...
Georg Brandl116aa622007-08-15 14:28:22 +00003958
Benjamin Petersonb58dda72009-01-18 22:27:04 +00003959 Return a new set with elements common to the set and all others.
Georg Brandl116aa622007-08-15 14:28:22 +00003960
Raymond Hettingera33e9f72016-09-12 23:38:50 -07003961 .. method:: difference(*others)
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00003962 set - other - ...
Georg Brandlc28e1fa2008-06-10 19:20:26 +00003963
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00003964 Return a new set with elements in the set that are not in the others.
Georg Brandl116aa622007-08-15 14:28:22 +00003965
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00003966 .. method:: symmetric_difference(other)
3967 set ^ other
Georg Brandl116aa622007-08-15 14:28:22 +00003968
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00003969 Return a new set with elements in either the set or *other* but not both.
Georg Brandl116aa622007-08-15 14:28:22 +00003970
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00003971 .. method:: copy()
Georg Brandl116aa622007-08-15 14:28:22 +00003972
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00003973 Return a new set with a shallow copy of *s*.
Georg Brandl116aa622007-08-15 14:28:22 +00003974
3975
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00003976 Note, the non-operator versions of :meth:`union`, :meth:`intersection`,
3977 :meth:`difference`, and :meth:`symmetric_difference`, :meth:`issubset`, and
3978 :meth:`issuperset` methods will accept any iterable as an argument. In
3979 contrast, their operator based counterparts require their arguments to be
3980 sets. This precludes error-prone constructions like ``set('abc') & 'cbs'``
3981 in favor of the more readable ``set('abc').intersection('cbs')``.
Georg Brandl116aa622007-08-15 14:28:22 +00003982
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00003983 Both :class:`set` and :class:`frozenset` support set to set comparisons. Two
3984 sets are equal if and only if every element of each set is contained in the
3985 other (each is a subset of the other). A set is less than another set if and
3986 only if the first set is a proper subset of the second set (is a subset, but
3987 is not equal). A set is greater than another set if and only if the first set
3988 is a proper superset of the second set (is a superset, but is not equal).
Georg Brandl116aa622007-08-15 14:28:22 +00003989
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00003990 Instances of :class:`set` are compared to instances of :class:`frozenset`
3991 based on their members. For example, ``set('abc') == frozenset('abc')``
3992 returns ``True`` and so does ``set('abc') in set([frozenset('abc')])``.
Georg Brandl116aa622007-08-15 14:28:22 +00003993
Raymond Hettinger12f588a2013-05-06 18:22:43 -07003994 The subset and equality comparisons do not generalize to a total ordering
3995 function. For example, any two nonempty disjoint sets are not equal and are not
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00003996 subsets of each other, so *all* of the following return ``False``: ``a<b``,
Georg Brandl05f5ab72008-09-24 09:11:47 +00003997 ``a==b``, or ``a>b``.
Georg Brandl116aa622007-08-15 14:28:22 +00003998
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00003999 Since sets only define partial ordering (subset relationships), the output of
4000 the :meth:`list.sort` method is undefined for lists of sets.
Georg Brandl116aa622007-08-15 14:28:22 +00004001
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00004002 Set elements, like dictionary keys, must be :term:`hashable`.
Georg Brandl116aa622007-08-15 14:28:22 +00004003
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00004004 Binary operations that mix :class:`set` instances with :class:`frozenset`
4005 return the type of the first operand. For example: ``frozenset('ab') |
4006 set('bc')`` returns an instance of :class:`frozenset`.
Georg Brandl116aa622007-08-15 14:28:22 +00004007
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00004008 The following table lists operations available for :class:`set` that do not
4009 apply to immutable instances of :class:`frozenset`:
Georg Brandl116aa622007-08-15 14:28:22 +00004010
Raymond Hettingera33e9f72016-09-12 23:38:50 -07004011 .. method:: update(*others)
Georg Brandlc28e1fa2008-06-10 19:20:26 +00004012 set |= other | ...
Georg Brandl116aa622007-08-15 14:28:22 +00004013
Georg Brandla6053b42009-09-01 08:11:14 +00004014 Update the set, adding elements from all others.
Georg Brandl116aa622007-08-15 14:28:22 +00004015
Raymond Hettingera33e9f72016-09-12 23:38:50 -07004016 .. method:: intersection_update(*others)
Georg Brandlc28e1fa2008-06-10 19:20:26 +00004017 set &= other & ...
Georg Brandl116aa622007-08-15 14:28:22 +00004018
Georg Brandla6053b42009-09-01 08:11:14 +00004019 Update the set, keeping only elements found in it and all others.
Georg Brandl116aa622007-08-15 14:28:22 +00004020
Raymond Hettingera33e9f72016-09-12 23:38:50 -07004021 .. method:: difference_update(*others)
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00004022 set -= other | ...
Georg Brandl116aa622007-08-15 14:28:22 +00004023
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00004024 Update the set, removing elements found in others.
4025
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00004026 .. method:: symmetric_difference_update(other)
4027 set ^= other
Georg Brandl116aa622007-08-15 14:28:22 +00004028
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00004029 Update the set, keeping only elements found in either set, but not in both.
Georg Brandl116aa622007-08-15 14:28:22 +00004030
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00004031 .. method:: add(elem)
Georg Brandl116aa622007-08-15 14:28:22 +00004032
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00004033 Add element *elem* to the set.
Georg Brandl116aa622007-08-15 14:28:22 +00004034
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00004035 .. method:: remove(elem)
Georg Brandl116aa622007-08-15 14:28:22 +00004036
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00004037 Remove element *elem* from the set. Raises :exc:`KeyError` if *elem* is
4038 not contained in the set.
Georg Brandl116aa622007-08-15 14:28:22 +00004039
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00004040 .. method:: discard(elem)
Georg Brandl116aa622007-08-15 14:28:22 +00004041
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00004042 Remove element *elem* from the set if it is present.
Georg Brandl116aa622007-08-15 14:28:22 +00004043
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00004044 .. method:: pop()
Georg Brandl116aa622007-08-15 14:28:22 +00004045
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00004046 Remove and return an arbitrary element from the set. Raises
4047 :exc:`KeyError` if the set is empty.
Georg Brandl116aa622007-08-15 14:28:22 +00004048
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00004049 .. method:: clear()
Georg Brandl116aa622007-08-15 14:28:22 +00004050
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00004051 Remove all elements from the set.
Georg Brandl116aa622007-08-15 14:28:22 +00004052
4053
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00004054 Note, the non-operator versions of the :meth:`update`,
4055 :meth:`intersection_update`, :meth:`difference_update`, and
4056 :meth:`symmetric_difference_update` methods will accept any iterable as an
4057 argument.
Georg Brandl116aa622007-08-15 14:28:22 +00004058
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00004059 Note, the *elem* argument to the :meth:`__contains__`, :meth:`remove`, and
4060 :meth:`discard` methods may be a set. To support searching for an equivalent
Julien0737ee22017-06-01 16:02:21 +02004061 frozenset, a temporary one is created from *elem*.
Benjamin Peterson699adb92008-05-08 22:27:58 +00004062
Georg Brandl116aa622007-08-15 14:28:22 +00004063
4064.. _typesmapping:
4065
4066Mapping Types --- :class:`dict`
4067===============================
4068
4069.. index::
4070 object: mapping
4071 object: dictionary
4072 triple: operations on; mapping; types
4073 triple: operations on; dictionary; type
4074 statement: del
4075 builtin: len
4076
Chris Jerdonek11f3f172012-11-03 12:05:55 -07004077A :term:`mapping` object maps :term:`hashable` values to arbitrary objects.
Guido van Rossum2cc30da2007-11-02 23:46:40 +00004078Mappings are mutable objects. There is currently only one standard mapping
Nick Coghlan83c0ae52012-08-21 17:42:52 +10004079type, the :dfn:`dictionary`. (For other containers see the built-in
Guido van Rossum2cc30da2007-11-02 23:46:40 +00004080:class:`list`, :class:`set`, and :class:`tuple` classes, and the
4081:mod:`collections` module.)
Georg Brandl116aa622007-08-15 14:28:22 +00004082
Guido van Rossum2cc30da2007-11-02 23:46:40 +00004083A dictionary's keys are *almost* arbitrary values. Values that are not
4084:term:`hashable`, that is, values containing lists, dictionaries or other
4085mutable types (that are compared by value rather than by object identity) may
4086not be used as keys. Numeric types used for keys obey the normal rules for
4087numeric comparison: if two numbers compare equal (such as ``1`` and ``1.0``)
4088then they can be used interchangeably to index the same dictionary entry. (Note
4089however, that since computers store floating-point numbers as approximations it
4090is usually unwise to use them as dictionary keys.)
Georg Brandl116aa622007-08-15 14:28:22 +00004091
4092Dictionaries can be created by placing a comma-separated list of ``key: value``
4093pairs within braces, for example: ``{'jack': 4098, 'sjoerd': 4127}`` or ``{4098:
4094'jack', 4127: 'sjoerd'}``, or by the :class:`dict` constructor.
4095
Chris Jerdonekf3413172012-10-13 03:22:33 -07004096.. class:: dict(**kwarg)
4097 dict(mapping, **kwarg)
4098 dict(iterable, **kwarg)
Georg Brandl116aa622007-08-15 14:28:22 +00004099
Chris Jerdonekf3413172012-10-13 03:22:33 -07004100 Return a new dictionary initialized from an optional positional argument
4101 and a possibly empty set of keyword arguments.
4102
4103 If no positional argument is given, an empty dictionary is created.
4104 If a positional argument is given and it is a mapping object, a dictionary
4105 is created with the same key-value pairs as the mapping object. Otherwise,
Terry Jan Reedyb52f8762014-06-02 20:42:56 -04004106 the positional argument must be an :term:`iterable` object. Each item in
4107 the iterable must itself be an iterable with exactly two objects. The
Chris Jerdonekf3413172012-10-13 03:22:33 -07004108 first object of each item becomes a key in the new dictionary, and the
4109 second object the corresponding value. If a key occurs more than once, the
4110 last value for that key becomes the corresponding value in the new
Georg Brandld22a8152007-09-04 17:43:37 +00004111 dictionary.
Georg Brandl116aa622007-08-15 14:28:22 +00004112
Chris Jerdonekf3413172012-10-13 03:22:33 -07004113 If keyword arguments are given, the keyword arguments and their values are
4114 added to the dictionary created from the positional argument. If a key
4115 being added is already present, the value from the keyword argument
4116 replaces the value from the positional argument.
Georg Brandl116aa622007-08-15 14:28:22 +00004117
Chris Jerdonekf3413172012-10-13 03:22:33 -07004118 To illustrate, the following examples all return a dictionary equal to
Ezio Melottia20879f2012-10-26 19:14:16 +03004119 ``{"one": 1, "two": 2, "three": 3}``::
Georg Brandl116aa622007-08-15 14:28:22 +00004120
Ezio Melottia20879f2012-10-26 19:14:16 +03004121 >>> a = dict(one=1, two=2, three=3)
4122 >>> b = {'one': 1, 'two': 2, 'three': 3}
4123 >>> c = dict(zip(['one', 'two', 'three'], [1, 2, 3]))
4124 >>> d = dict([('two', 2), ('one', 1), ('three', 3)])
4125 >>> e = dict({'three': 3, 'one': 1, 'two': 2})
Chris Jerdonekf3413172012-10-13 03:22:33 -07004126 >>> a == b == c == d == e
4127 True
4128
4129 Providing keyword arguments as in the first example only works for keys that
4130 are valid Python identifiers. Otherwise, any valid keys can be used.
Georg Brandl116aa622007-08-15 14:28:22 +00004131
Georg Brandl116aa622007-08-15 14:28:22 +00004132
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00004133 These are the operations that dictionaries support (and therefore, custom
4134 mapping types should support too):
Georg Brandl116aa622007-08-15 14:28:22 +00004135
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00004136 .. describe:: len(d)
Georg Brandl116aa622007-08-15 14:28:22 +00004137
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00004138 Return the number of items in the dictionary *d*.
Georg Brandl116aa622007-08-15 14:28:22 +00004139
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00004140 .. describe:: d[key]
Georg Brandl116aa622007-08-15 14:28:22 +00004141
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00004142 Return the item of *d* with key *key*. Raises a :exc:`KeyError` if *key* is
4143 not in the map.
Georg Brandl48310cd2009-01-03 21:18:54 +00004144
Terry Jan Reedy06c62182014-12-10 18:48:23 -05004145 .. index:: __missing__()
Terry Jan Reedye40031d2014-12-10 18:49:58 -05004146
Terry Jan Reedyb67f6e22014-12-10 18:38:19 -05004147 If a subclass of dict defines a method :meth:`__missing__` and *key*
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00004148 is not present, the ``d[key]`` operation calls that method with the key *key*
4149 as argument. The ``d[key]`` operation then returns or raises whatever is
Terry Jan Reedyb67f6e22014-12-10 18:38:19 -05004150 returned or raised by the ``__missing__(key)`` call.
4151 No other operations or methods invoke :meth:`__missing__`. If
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00004152 :meth:`__missing__` is not defined, :exc:`KeyError` is raised.
Raymond Hettinger5254e972011-01-08 09:35:38 +00004153 :meth:`__missing__` must be a method; it cannot be an instance variable::
4154
4155 >>> class Counter(dict):
4156 ... def __missing__(self, key):
4157 ... return 0
4158 >>> c = Counter()
4159 >>> c['red']
4160 0
4161 >>> c['red'] += 1
4162 >>> c['red']
4163 1
4164
Terry Jan Reedyb67f6e22014-12-10 18:38:19 -05004165 The example above shows part of the implementation of
4166 :class:`collections.Counter`. A different ``__missing__`` method is used
4167 by :class:`collections.defaultdict`.
Georg Brandl116aa622007-08-15 14:28:22 +00004168
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00004169 .. describe:: d[key] = value
Georg Brandl116aa622007-08-15 14:28:22 +00004170
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00004171 Set ``d[key]`` to *value*.
Georg Brandl116aa622007-08-15 14:28:22 +00004172
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00004173 .. describe:: del d[key]
Georg Brandl116aa622007-08-15 14:28:22 +00004174
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00004175 Remove ``d[key]`` from *d*. Raises a :exc:`KeyError` if *key* is not in the
4176 map.
Georg Brandl116aa622007-08-15 14:28:22 +00004177
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00004178 .. describe:: key in d
Georg Brandl116aa622007-08-15 14:28:22 +00004179
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00004180 Return ``True`` if *d* has a key *key*, else ``False``.
Georg Brandl116aa622007-08-15 14:28:22 +00004181
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00004182 .. describe:: key not in d
Georg Brandl116aa622007-08-15 14:28:22 +00004183
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00004184 Equivalent to ``not key in d``.
Georg Brandl116aa622007-08-15 14:28:22 +00004185
Benjamin Petersond23f8222009-04-05 19:13:16 +00004186 .. describe:: iter(d)
4187
4188 Return an iterator over the keys of the dictionary. This is a shortcut
Georg Brandlede6c2a2010-01-05 10:22:04 +00004189 for ``iter(d.keys())``.
Benjamin Petersond23f8222009-04-05 19:13:16 +00004190
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00004191 .. method:: clear()
Georg Brandl116aa622007-08-15 14:28:22 +00004192
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00004193 Remove all items from the dictionary.
Georg Brandl116aa622007-08-15 14:28:22 +00004194
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00004195 .. method:: copy()
Georg Brandl116aa622007-08-15 14:28:22 +00004196
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00004197 Return a shallow copy of the dictionary.
Georg Brandl116aa622007-08-15 14:28:22 +00004198
Georg Brandlabc38772009-04-12 15:51:51 +00004199 .. classmethod:: fromkeys(seq[, value])
Georg Brandl116aa622007-08-15 14:28:22 +00004200
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00004201 Create a new dictionary with keys from *seq* and values set to *value*.
Georg Brandl116aa622007-08-15 14:28:22 +00004202
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00004203 :meth:`fromkeys` is a class method that returns a new dictionary. *value*
4204 defaults to ``None``.
Georg Brandl116aa622007-08-15 14:28:22 +00004205
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00004206 .. method:: get(key[, default])
Georg Brandl116aa622007-08-15 14:28:22 +00004207
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00004208 Return the value for *key* if *key* is in the dictionary, else *default*.
4209 If *default* is not given, it defaults to ``None``, so that this method
4210 never raises a :exc:`KeyError`.
Georg Brandl116aa622007-08-15 14:28:22 +00004211
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00004212 .. method:: items()
Georg Brandl116aa622007-08-15 14:28:22 +00004213
Victor Stinner0db176f2012-04-16 00:16:30 +02004214 Return a new view of the dictionary's items (``(key, value)`` pairs).
4215 See the :ref:`documentation of view objects <dict-views>`.
Georg Brandl116aa622007-08-15 14:28:22 +00004216
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00004217 .. method:: keys()
Georg Brandl116aa622007-08-15 14:28:22 +00004218
Victor Stinner0db176f2012-04-16 00:16:30 +02004219 Return a new view of the dictionary's keys. See the :ref:`documentation
4220 of view objects <dict-views>`.
Georg Brandl116aa622007-08-15 14:28:22 +00004221
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00004222 .. method:: pop(key[, default])
Georg Brandl116aa622007-08-15 14:28:22 +00004223
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00004224 If *key* is in the dictionary, remove it and return its value, else return
4225 *default*. If *default* is not given and *key* is not in the dictionary,
4226 a :exc:`KeyError` is raised.
Georg Brandl116aa622007-08-15 14:28:22 +00004227
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00004228 .. method:: popitem()
Georg Brandl116aa622007-08-15 14:28:22 +00004229
Raymond Hettinger01b7d582018-07-16 17:20:15 -07004230 Remove and return a ``(key, value)`` pair from the dictionary.
4231 Pairs are returned in :abbr:`LIFO (last-in, first-out)` order.
Georg Brandl116aa622007-08-15 14:28:22 +00004232
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00004233 :meth:`popitem` is useful to destructively iterate over a dictionary, as
4234 often used in set algorithms. If the dictionary is empty, calling
4235 :meth:`popitem` raises a :exc:`KeyError`.
Georg Brandl116aa622007-08-15 14:28:22 +00004236
Raymond Hettinger01b7d582018-07-16 17:20:15 -07004237 .. versionchanged:: 3.7
Andrés Delfinocb9c2992018-07-21 19:14:56 -03004238 LIFO order is now guaranteed. In prior versions, :meth:`popitem` would
4239 return an arbitrary key/value pair.
Raymond Hettinger01b7d582018-07-16 17:20:15 -07004240
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00004241 .. method:: setdefault(key[, default])
Georg Brandl116aa622007-08-15 14:28:22 +00004242
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00004243 If *key* is in the dictionary, return its value. If not, insert *key*
4244 with a value of *default* and return *default*. *default* defaults to
4245 ``None``.
Georg Brandl116aa622007-08-15 14:28:22 +00004246
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00004247 .. method:: update([other])
Georg Brandl116aa622007-08-15 14:28:22 +00004248
Éric Araujo0fc86b82010-08-18 22:29:54 +00004249 Update the dictionary with the key/value pairs from *other*, overwriting
4250 existing keys. Return ``None``.
Georg Brandl116aa622007-08-15 14:28:22 +00004251
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00004252 :meth:`update` accepts either another dictionary object or an iterable of
Georg Brandlfda21062010-09-25 16:56:36 +00004253 key/value pairs (as tuples or other iterables of length two). If keyword
Benjamin Peterson8719ad52009-09-11 22:24:02 +00004254 arguments are specified, the dictionary is then updated with those
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00004255 key/value pairs: ``d.update(red=1, blue=2)``.
Georg Brandl116aa622007-08-15 14:28:22 +00004256
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00004257 .. method:: values()
Georg Brandl116aa622007-08-15 14:28:22 +00004258
Victor Stinner0db176f2012-04-16 00:16:30 +02004259 Return a new view of the dictionary's values. See the
4260 :ref:`documentation of view objects <dict-views>`.
4261
Terry Jan Reedyfe63c9a2015-06-12 16:38:57 -04004262 Dictionaries compare equal if and only if they have the same ``(key,
4263 value)`` pairs. Order comparisons ('<', '<=', '>=', '>') raise
4264 :exc:`TypeError`.
Terry Jan Reedy6ac5cc12015-06-12 16:47:44 -04004265
Neil Schemenauerd3ed67d2018-06-07 14:46:04 -07004266 Dictionaries preserve insertion order. Note that updating a key does not
4267 affect the order. Keys added after deletion are inserted at the end. ::
INADA Naokif8225492018-06-05 07:09:22 +09004268
4269 >>> d = {"one": 1, "two": 2, "three": 3, "four": 4}
4270 >>> d
4271 {'one': 1, 'two': 2, 'three': 3, 'four': 4}
4272 >>> list(d)
4273 ['one', 'two', 'three', 'four']
4274 >>> list(d.values())
4275 [1, 2, 3, 4]
4276 >>> d["one"] = 42
4277 >>> d
4278 {'one': 42, 'two': 2, 'three': 3, 'four': 4}
4279 >>> del d["two"]
4280 >>> d["two"] = None
4281 >>> d
4282 {'one': 42, 'three': 3, 'four': 4, 'two': None}
4283
4284 .. versionchanged:: 3.7
Neil Schemenauerd3ed67d2018-06-07 14:46:04 -07004285 Dictionary order is guaranteed to be insertion order. This behavior was
INADA Naokif8225492018-06-05 07:09:22 +09004286 implementation detail of CPython from 3.6.
4287
Victor Stinner0db176f2012-04-16 00:16:30 +02004288.. seealso::
4289 :class:`types.MappingProxyType` can be used to create a read-only view
4290 of a :class:`dict`.
Georg Brandld22a8152007-09-04 17:43:37 +00004291
4292
Benjamin Peterson44309e62008-11-22 00:41:45 +00004293.. _dict-views:
4294
Georg Brandld22a8152007-09-04 17:43:37 +00004295Dictionary view objects
4296-----------------------
4297
4298The objects returned by :meth:`dict.keys`, :meth:`dict.values` and
4299:meth:`dict.items` are *view objects*. They provide a dynamic view on the
4300dictionary's entries, which means that when the dictionary changes, the view
Benjamin Petersonce0506c2008-11-17 21:47:41 +00004301reflects these changes.
Georg Brandld22a8152007-09-04 17:43:37 +00004302
4303Dictionary views can be iterated over to yield their respective data, and
4304support membership tests:
4305
4306.. describe:: len(dictview)
4307
4308 Return the number of entries in the dictionary.
4309
4310.. describe:: iter(dictview)
4311
4312 Return an iterator over the keys, values or items (represented as tuples of
4313 ``(key, value)``) in the dictionary.
4314
hui shangdfbbbf12018-04-04 12:55:05 +08004315 Keys and values are iterated over in insertion order.
4316 This allows the creation of ``(value, key)`` pairs
Georg Brandld22a8152007-09-04 17:43:37 +00004317 using :func:`zip`: ``pairs = zip(d.values(), d.keys())``. Another way to
4318 create the same list is ``pairs = [(v, k) for (k, v) in d.items()]``.
4319
Georg Brandl81269142009-05-17 08:31:29 +00004320 Iterating views while adding or deleting entries in the dictionary may raise
4321 a :exc:`RuntimeError` or fail to iterate over all entries.
Benjamin Petersond23f8222009-04-05 19:13:16 +00004322
hui shangdfbbbf12018-04-04 12:55:05 +08004323 .. versionchanged:: 3.7
Neil Schemenauerd3ed67d2018-06-07 14:46:04 -07004324 Dictionary order is guaranteed to be insertion order.
hui shangdfbbbf12018-04-04 12:55:05 +08004325
Georg Brandld22a8152007-09-04 17:43:37 +00004326.. describe:: x in dictview
4327
4328 Return ``True`` if *x* is in the underlying dictionary's keys, values or
4329 items (in the latter case, *x* should be a ``(key, value)`` tuple).
4330
4331
Benjamin Petersonce0506c2008-11-17 21:47:41 +00004332Keys views are set-like since their entries are unique and hashable. If all
Georg Brandlf74cf772010-10-15 16:03:02 +00004333values are hashable, so that ``(key, value)`` pairs are unique and hashable,
4334then the items view is also set-like. (Values views are not treated as set-like
4335since the entries are generally not unique.) For set-like views, all of the
Nick Coghlan273069c2012-08-20 17:14:07 +10004336operations defined for the abstract base class :class:`collections.abc.Set` are
Georg Brandlf74cf772010-10-15 16:03:02 +00004337available (for example, ``==``, ``<``, or ``^``).
Georg Brandl116aa622007-08-15 14:28:22 +00004338
Georg Brandlc53c9662007-09-04 17:58:02 +00004339An example of dictionary view usage::
4340
4341 >>> dishes = {'eggs': 2, 'sausage': 1, 'bacon': 1, 'spam': 500}
4342 >>> keys = dishes.keys()
4343 >>> values = dishes.values()
4344
4345 >>> # iteration
4346 >>> n = 0
4347 >>> for val in values:
4348 ... n += val
4349 >>> print(n)
4350 504
4351
hui shangdfbbbf12018-04-04 12:55:05 +08004352 >>> # keys and values are iterated over in the same order (insertion order)
Georg Brandlc53c9662007-09-04 17:58:02 +00004353 >>> list(keys)
hui shangdfbbbf12018-04-04 12:55:05 +08004354 ['eggs', 'sausage', 'bacon', 'spam']
Georg Brandlc53c9662007-09-04 17:58:02 +00004355 >>> list(values)
4356 [2, 1, 1, 500]
4357
4358 >>> # view objects are dynamic and reflect dict changes
4359 >>> del dishes['eggs']
4360 >>> del dishes['sausage']
4361 >>> list(keys)
hui shangdfbbbf12018-04-04 12:55:05 +08004362 ['bacon', 'spam']
Georg Brandlc53c9662007-09-04 17:58:02 +00004363
4364 >>> # set operations
4365 >>> keys & {'eggs', 'bacon', 'salad'}
Gregory P. Smithe8388122008-09-04 04:18:09 +00004366 {'bacon'}
Georg Brandlf74cf772010-10-15 16:03:02 +00004367 >>> keys ^ {'sausage', 'juice'}
Sandro Tosi2a8d1952011-08-02 18:42:04 +02004368 {'juice', 'sausage', 'bacon', 'spam'}
Georg Brandlc53c9662007-09-04 17:58:02 +00004369
4370
Georg Brandl116aa622007-08-15 14:28:22 +00004371.. _typecontextmanager:
4372
4373Context Manager Types
4374=====================
4375
Georg Brandl116aa622007-08-15 14:28:22 +00004376.. index::
4377 single: context manager
4378 single: context management protocol
4379 single: protocol; context management
4380
4381Python's :keyword:`with` statement supports the concept of a runtime context
Antoine Pitroua6540902010-12-12 20:09:18 +00004382defined by a context manager. This is implemented using a pair of methods
Georg Brandl116aa622007-08-15 14:28:22 +00004383that allow user-defined classes to define a runtime context that is entered
Antoine Pitroua6540902010-12-12 20:09:18 +00004384before the statement body is executed and exited when the statement ends:
Georg Brandl116aa622007-08-15 14:28:22 +00004385
4386
4387.. method:: contextmanager.__enter__()
4388
4389 Enter the runtime context and return either this object or another object
4390 related to the runtime context. The value returned by this method is bound to
4391 the identifier in the :keyword:`as` clause of :keyword:`with` statements using
4392 this context manager.
4393
Antoine Pitrou11cb9612010-09-15 11:11:28 +00004394 An example of a context manager that returns itself is a :term:`file object`.
4395 File objects return themselves from __enter__() to allow :func:`open` to be
4396 used as the context expression in a :keyword:`with` statement.
Georg Brandl116aa622007-08-15 14:28:22 +00004397
4398 An example of a context manager that returns a related object is the one
Christian Heimesfaf2f632008-01-06 16:59:19 +00004399 returned by :func:`decimal.localcontext`. These managers set the active
Georg Brandl116aa622007-08-15 14:28:22 +00004400 decimal context to a copy of the original decimal context and then return the
4401 copy. This allows changes to be made to the current decimal context in the body
4402 of the :keyword:`with` statement without affecting code outside the
4403 :keyword:`with` statement.
4404
4405
4406.. method:: contextmanager.__exit__(exc_type, exc_val, exc_tb)
4407
Georg Brandl9afde1c2007-11-01 20:32:30 +00004408 Exit the runtime context and return a Boolean flag indicating if any exception
Georg Brandl116aa622007-08-15 14:28:22 +00004409 that occurred should be suppressed. If an exception occurred while executing the
4410 body of the :keyword:`with` statement, the arguments contain the exception type,
4411 value and traceback information. Otherwise, all three arguments are ``None``.
4412
4413 Returning a true value from this method will cause the :keyword:`with` statement
4414 to suppress the exception and continue execution with the statement immediately
4415 following the :keyword:`with` statement. Otherwise the exception continues
4416 propagating after this method has finished executing. Exceptions that occur
4417 during execution of this method will replace any exception that occurred in the
4418 body of the :keyword:`with` statement.
4419
4420 The exception passed in should never be reraised explicitly - instead, this
4421 method should return a false value to indicate that the method completed
4422 successfully and does not want to suppress the raised exception. This allows
Georg Brandle4196d32014-10-31 09:41:46 +01004423 context management code to easily detect whether or not an :meth:`__exit__`
4424 method has actually failed.
Georg Brandl116aa622007-08-15 14:28:22 +00004425
4426Python defines several context managers to support easy thread synchronisation,
4427prompt closure of files or other objects, and simpler manipulation of the active
4428decimal arithmetic context. The specific types are not treated specially beyond
4429their implementation of the context management protocol. See the
4430:mod:`contextlib` module for some examples.
4431
Antoine Pitroua6540902010-12-12 20:09:18 +00004432Python's :term:`generator`\s and the :class:`contextlib.contextmanager` decorator
Christian Heimesd8654cf2007-12-02 15:22:16 +00004433provide a convenient way to implement these protocols. If a generator function is
Antoine Pitroua6540902010-12-12 20:09:18 +00004434decorated with the :class:`contextlib.contextmanager` decorator, it will return a
Georg Brandl116aa622007-08-15 14:28:22 +00004435context manager implementing the necessary :meth:`__enter__` and
4436:meth:`__exit__` methods, rather than the iterator produced by an undecorated
4437generator function.
4438
4439Note that there is no specific slot for any of these methods in the type
4440structure for Python objects in the Python/C API. Extension types wanting to
4441define these methods must provide them as a normal Python accessible method.
4442Compared to the overhead of setting up the runtime context, the overhead of a
4443single class dictionary lookup is negligible.
4444
4445
4446.. _typesother:
4447
4448Other Built-in Types
4449====================
4450
4451The interpreter supports several other kinds of objects. Most of these support
4452only one or two operations.
4453
4454
4455.. _typesmodules:
4456
4457Modules
4458-------
4459
4460The only special operation on a module is attribute access: ``m.name``, where
4461*m* is a module and *name* accesses a name defined in *m*'s symbol table.
4462Module attributes can be assigned to. (Note that the :keyword:`import`
4463statement is not, strictly speaking, an operation on a module object; ``import
4464foo`` does not require a module object named *foo* to exist, rather it requires
4465an (external) *definition* for a module named *foo* somewhere.)
4466
Serhiy Storchaka0d196ed2013-10-09 14:02:31 +03004467A special attribute of every module is :attr:`~object.__dict__`. This is the
4468dictionary containing the module's symbol table. Modifying this dictionary will
4469actually change the module's symbol table, but direct assignment to the
Martin Panterbae5d812016-06-18 03:57:31 +00004470:attr:`~object.__dict__` attribute is not possible (you can write
Serhiy Storchaka0d196ed2013-10-09 14:02:31 +03004471``m.__dict__['a'] = 1``, which defines ``m.a`` to be ``1``, but you can't write
Martin Panterbae5d812016-06-18 03:57:31 +00004472``m.__dict__ = {}``). Modifying :attr:`~object.__dict__` directly is
4473not recommended.
Georg Brandl116aa622007-08-15 14:28:22 +00004474
4475Modules built into the interpreter are written like this: ``<module 'sys'
4476(built-in)>``. If loaded from a file, they are written as ``<module 'os' from
4477'/usr/local/lib/pythonX.Y/os.pyc'>``.
4478
4479
4480.. _typesobjects:
4481
4482Classes and Class Instances
4483---------------------------
4484
4485See :ref:`objects` and :ref:`class` for these.
4486
4487
4488.. _typesfunctions:
4489
4490Functions
4491---------
4492
4493Function objects are created by function definitions. The only operation on a
4494function object is to call it: ``func(argument-list)``.
4495
4496There are really two flavors of function objects: built-in functions and
4497user-defined functions. Both support the same operation (to call the function),
4498but the implementation is different, hence the different object types.
4499
4500See :ref:`function` for more information.
4501
4502
4503.. _typesmethods:
4504
4505Methods
4506-------
4507
4508.. index:: object: method
4509
4510Methods are functions that are called using the attribute notation. There are
4511two flavors: built-in methods (such as :meth:`append` on lists) and class
4512instance methods. Built-in methods are described with the types that support
4513them.
4514
Georg Brandl2e0b7552007-11-27 12:43:08 +00004515If you access a method (a function defined in a class namespace) through an
4516instance, you get a special object: a :dfn:`bound method` (also called
4517:dfn:`instance method`) object. When called, it will add the ``self`` argument
4518to the argument list. Bound methods have two special read-only attributes:
4519``m.__self__`` is the object on which the method operates, and ``m.__func__`` is
4520the function implementing the method. Calling ``m(arg-1, arg-2, ..., arg-n)``
4521is completely equivalent to calling ``m.__func__(m.__self__, arg-1, arg-2, ...,
4522arg-n)``.
Georg Brandl116aa622007-08-15 14:28:22 +00004523
Georg Brandl2e0b7552007-11-27 12:43:08 +00004524Like function objects, bound method objects support getting arbitrary
4525attributes. However, since method attributes are actually stored on the
4526underlying function object (``meth.__func__``), setting method attributes on
Ezio Melotti8b6b1762012-11-09 01:08:25 +02004527bound methods is disallowed. Attempting to set an attribute on a method
4528results in an :exc:`AttributeError` being raised. In order to set a method
4529attribute, you need to explicitly set it on the underlying function object::
Georg Brandl116aa622007-08-15 14:28:22 +00004530
Ezio Melotti8b6b1762012-11-09 01:08:25 +02004531 >>> class C:
4532 ... def method(self):
4533 ... pass
4534 ...
4535 >>> c = C()
4536 >>> c.method.whoami = 'my name is method' # can't set on the method
4537 Traceback (most recent call last):
4538 File "<stdin>", line 1, in <module>
4539 AttributeError: 'method' object has no attribute 'whoami'
4540 >>> c.method.__func__.whoami = 'my name is method'
4541 >>> c.method.whoami
4542 'my name is method'
Georg Brandl116aa622007-08-15 14:28:22 +00004543
4544See :ref:`types` for more information.
4545
4546
Tommy Beadlee9b84032016-06-02 19:26:51 -04004547.. index:: object; code, code object
4548
Georg Brandl116aa622007-08-15 14:28:22 +00004549.. _bltin-code-objects:
4550
4551Code Objects
4552------------
4553
Georg Brandl116aa622007-08-15 14:28:22 +00004554.. index::
4555 builtin: compile
4556 single: __code__ (function object attribute)
4557
4558Code objects are used by the implementation to represent "pseudo-compiled"
4559executable Python code such as a function body. They differ from function
4560objects because they don't contain a reference to their global execution
4561environment. Code objects are returned by the built-in :func:`compile` function
4562and can be extracted from function objects through their :attr:`__code__`
4563attribute. See also the :mod:`code` module.
4564
4565.. index::
4566 builtin: exec
4567 builtin: eval
4568
4569A code object can be executed or evaluated by passing it (instead of a source
4570string) to the :func:`exec` or :func:`eval` built-in functions.
4571
4572See :ref:`types` for more information.
4573
4574
4575.. _bltin-type-objects:
4576
4577Type Objects
4578------------
4579
4580.. index::
4581 builtin: type
4582 module: types
4583
4584Type objects represent the various object types. An object's type is accessed
4585by the built-in function :func:`type`. There are no special operations on
4586types. The standard module :mod:`types` defines names for all standard built-in
4587types.
4588
Martin v. Löwis250ad612008-04-07 05:43:42 +00004589Types are written like this: ``<class 'int'>``.
Georg Brandl116aa622007-08-15 14:28:22 +00004590
4591
4592.. _bltin-null-object:
4593
4594The Null Object
4595---------------
4596
4597This object is returned by functions that don't explicitly return a value. It
4598supports no special operations. There is exactly one null object, named
Benjamin Peterson98f2b9b2011-07-30 12:26:27 -05004599``None`` (a built-in name). ``type(None)()`` produces the same singleton.
Georg Brandl116aa622007-08-15 14:28:22 +00004600
4601It is written as ``None``.
4602
4603
4604.. _bltin-ellipsis-object:
4605
4606The Ellipsis Object
4607-------------------
4608
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004609This object is commonly used by slicing (see :ref:`slicings`). It supports no
4610special operations. There is exactly one ellipsis object, named
4611:const:`Ellipsis` (a built-in name). ``type(Ellipsis)()`` produces the
4612:const:`Ellipsis` singleton.
Georg Brandl116aa622007-08-15 14:28:22 +00004613
4614It is written as ``Ellipsis`` or ``...``.
4615
4616
Éric Araujo18ddf822011-09-01 23:10:36 +02004617.. _bltin-notimplemented-object:
4618
Benjamin Peterson50211fa2011-07-30 09:57:24 -05004619The NotImplemented Object
4620-------------------------
4621
4622This object is returned from comparisons and binary operations when they are
4623asked to operate on types they don't support. See :ref:`comparisons` for more
Benjamin Peterson98f2b9b2011-07-30 12:26:27 -05004624information. There is exactly one ``NotImplemented`` object.
4625``type(NotImplemented)()`` produces the singleton instance.
Benjamin Peterson50211fa2011-07-30 09:57:24 -05004626
4627It is written as ``NotImplemented``.
4628
Georg Brandl116aa622007-08-15 14:28:22 +00004629
Éric Araujo18ddf822011-09-01 23:10:36 +02004630.. _bltin-boolean-values:
4631
Georg Brandl116aa622007-08-15 14:28:22 +00004632Boolean Values
4633--------------
4634
4635Boolean values are the two constant objects ``False`` and ``True``. They are
4636used to represent truth values (although other values can also be considered
4637false or true). In numeric contexts (for example when used as the argument to
4638an arithmetic operator), they behave like the integers 0 and 1, respectively.
Ezio Melottic1f26f62011-12-02 19:47:24 +02004639The built-in function :func:`bool` can be used to convert any value to a
4640Boolean, if the value can be interpreted as a truth value (see section
4641:ref:`truth` above).
Georg Brandl116aa622007-08-15 14:28:22 +00004642
4643.. index::
4644 single: False
4645 single: True
4646 pair: Boolean; values
4647
4648They are written as ``False`` and ``True``, respectively.
4649
4650
4651.. _typesinternal:
4652
4653Internal Objects
4654----------------
4655
4656See :ref:`types` for this information. It describes stack frame objects,
4657traceback objects, and slice objects.
4658
4659
4660.. _specialattrs:
4661
4662Special Attributes
4663==================
4664
4665The implementation adds a few special read-only attributes to several object
4666types, where they are relevant. Some of these are not reported by the
4667:func:`dir` built-in function.
4668
4669
4670.. attribute:: object.__dict__
4671
4672 A dictionary or other mapping object used to store an object's (writable)
4673 attributes.
4674
4675
4676.. attribute:: instance.__class__
4677
4678 The class to which a class instance belongs.
4679
4680
4681.. attribute:: class.__bases__
4682
Benjamin Peterson1baf4652009-12-31 03:11:23 +00004683 The tuple of base classes of a class object.
Georg Brandl116aa622007-08-15 14:28:22 +00004684
4685
Martin Panterbae5d812016-06-18 03:57:31 +00004686.. attribute:: definition.__name__
Georg Brandl116aa622007-08-15 14:28:22 +00004687
Martin Panterbae5d812016-06-18 03:57:31 +00004688 The name of the class, function, method, descriptor, or
4689 generator instance.
Georg Brandl116aa622007-08-15 14:28:22 +00004690
Georg Brandl7a51e582009-03-28 19:13:21 +00004691
Martin Panterbae5d812016-06-18 03:57:31 +00004692.. attribute:: definition.__qualname__
Antoine Pitrou86a36b52011-11-25 18:56:07 +01004693
Martin Panterbae5d812016-06-18 03:57:31 +00004694 The :term:`qualified name` of the class, function, method, descriptor,
4695 or generator instance.
Antoine Pitrou86a36b52011-11-25 18:56:07 +01004696
4697 .. versionadded:: 3.3
4698
4699
Benjamin Petersond23f8222009-04-05 19:13:16 +00004700.. attribute:: class.__mro__
4701
4702 This attribute is a tuple of classes that are considered when looking for
4703 base classes during method resolution.
4704
4705
4706.. method:: class.mro()
4707
4708 This method can be overridden by a metaclass to customize the method
4709 resolution order for its instances. It is called at class instantiation, and
Serhiy Storchaka0d196ed2013-10-09 14:02:31 +03004710 its result is stored in :attr:`~class.__mro__`.
Benjamin Petersond23f8222009-04-05 19:13:16 +00004711
4712
Georg Brandl7a51e582009-03-28 19:13:21 +00004713.. method:: class.__subclasses__
4714
Florent Xicluna74e64952011-10-28 11:21:19 +02004715 Each class keeps a list of weak references to its immediate subclasses. This
4716 method returns a list of all those references still alive.
Benjamin Petersond23f8222009-04-05 19:13:16 +00004717 Example::
Georg Brandl7a51e582009-03-28 19:13:21 +00004718
4719 >>> int.__subclasses__()
Florent Xicluna74e64952011-10-28 11:21:19 +02004720 [<class 'bool'>]
Georg Brandl7a51e582009-03-28 19:13:21 +00004721
4722
Georg Brandl116aa622007-08-15 14:28:22 +00004723.. rubric:: Footnotes
4724
Ezio Melotti0656a562011-08-15 14:27:19 +03004725.. [1] Additional information on these special methods may be found in the Python
Georg Brandl116aa622007-08-15 14:28:22 +00004726 Reference Manual (:ref:`customization`).
4727
Ezio Melotti0656a562011-08-15 14:27:19 +03004728.. [2] As a consequence, the list ``[1, 2]`` is considered equal to ``[1.0, 2.0]``, and
Georg Brandl116aa622007-08-15 14:28:22 +00004729 similarly for tuples.
4730
Ezio Melotti0656a562011-08-15 14:27:19 +03004731.. [3] They must have since the parser can't tell the type of the operands.
Georg Brandl116aa622007-08-15 14:28:22 +00004732
Ezio Melotti0656a562011-08-15 14:27:19 +03004733.. [4] Cased characters are those with general category property being one of
4734 "Lu" (Letter, uppercase), "Ll" (Letter, lowercase), or "Lt" (Letter, titlecase).
4735
4736.. [5] To format only a tuple you should therefore provide a singleton tuple whose only
Georg Brandl116aa622007-08-15 14:28:22 +00004737 element is the tuple to be formatted.
Neil Schemenauerd3ed67d2018-06-07 14:46:04 -07004738