1 .. XXX: reference/datamodel and this have quite a few overlaps!
10 The following sections describe the standard types that are built into the
13 .. index:: pair: built-in; types
15 The principal built-in types are numerics, sequences, mappings, classes,
16 instances and exceptions.
18 Some collection classes are mutable. The methods that add, subtract, or
19 rearrange their members in place, and don't return a specific item, never return
20 the collection instance itself but ``None``.
22 Some operations are supported by several object types; in particular,
23 practically all objects can be compared for equality, tested for truth
24 value, and converted to a string (with the :func:`repr` function or the
25 slightly different :func:`str` function). The latter function is implicitly
26 used when an object is written by the :func:`print` function.
38 pair: Boolean; operations
41 Any object can be tested for truth value, for use in an :keyword:`if` or
42 :keyword:`while` condition or as operand of the Boolean operations below.
44 .. index:: single: true
46 By 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
48 returns zero, when called with the object. [1]_ Here are most of the built-in
49 objects considered false:
52 single: None (Built-in object)
53 single: False (Built-in object)
55 * constants defined to be false: ``None`` and ``False``.
57 * zero of any numeric type: ``0``, ``0.0``, ``0j``, ``Decimal(0)``,
60 * empty sequences and collections: ``''``, ``()``, ``[]``, ``{}``, ``set()``,
69 Operations and built-in functions that have a Boolean result always return ``0``
70 or ``False`` for false and ``1`` or ``True`` for true, unless otherwise stated.
71 (Important exception: the Boolean operations ``or`` and ``and`` always return
72 one of their operands.)
77 Boolean Operations --- :keyword:`!and`, :keyword:`!or`, :keyword:`!not`
78 =======================================================================
80 .. index:: pair: Boolean; operations
82 These are the Boolean operations, ordered by ascending priority:
84 +-------------+---------------------------------+-------+
85 | Operation | Result | Notes |
86 +=============+=================================+=======+
87 | ``x or y`` | if *x* is false, then *y*, else | \(1) |
89 +-------------+---------------------------------+-------+
90 | ``x and y`` | if *x* is false, then *x*, else | \(2) |
92 +-------------+---------------------------------+-------+
93 | ``not x`` | if *x* is false, then ``True``, | \(3) |
94 | | else ``False`` | |
95 +-------------+---------------------------------+-------+
105 This is a short-circuit operator, so it only evaluates the second
106 argument if the first one is false.
109 This is a short-circuit operator, so it only evaluates the second
110 argument if the first one is true.
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.
123 pair: chaining; comparisons
124 pair: operator; comparison
128 operator: > (greater)
134 There are eight comparison operations in Python. They all have the same
135 priority (which is higher than that of the Boolean operations). Comparisons can
136 be chained arbitrarily; for example, ``x < y <= z`` is equivalent to ``x < y and
137 y <= z``, except that *y* is evaluated only once (but in both cases *z* is not
138 evaluated at all when ``x < y`` is found to be false).
140 This table summarizes the comparison operations:
142 +------------+-------------------------+
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 +------------+-------------------------+
154 +------------+-------------------------+
155 | ``!=`` | not equal |
156 +------------+-------------------------+
157 | ``is`` | object identity |
158 +------------+-------------------------+
159 | ``is not`` | negated object identity |
160 +------------+-------------------------+
163 pair: object; numeric
164 pair: objects; comparing
166 Objects of different types, except different numeric types, never compare equal.
167 The ``==`` operator is always defined but for some object types (for example,
168 class objects) is equivalent to :keyword:`is`. The ``<``, ``<=``, ``>`` and ``>=``
169 operators are only defined where they make sense; for example, they raise a
170 :exc:`TypeError` exception when one of the arguments is a complex number.
173 single: __eq__() (instance method)
174 single: __ne__() (instance method)
175 single: __lt__() (instance method)
176 single: __le__() (instance method)
177 single: __gt__() (instance method)
178 single: __ge__() (instance method)
180 Non-identical instances of a class normally compare as non-equal unless the
181 class defines the :meth:`__eq__` method.
183 Instances of a class cannot be ordered with respect to other instances of the
184 same class, or other types of object, unless the class defines enough of the
185 methods :meth:`__lt__`, :meth:`__le__`, :meth:`__gt__`, and :meth:`__ge__` (in
186 general, :meth:`__lt__` and :meth:`__eq__` are sufficient, if you want the
187 conventional meanings of the comparison operators).
189 The behavior of the :keyword:`is` and :keyword:`is not` operators cannot be
190 customized; also they can be applied to any two objects and never raise an
197 Two more operations with the same syntactic priority, :keyword:`in` and
198 :keyword:`not in`, are supported by types that are :term:`iterable` or
199 implement the :meth:`__contains__` method.
203 Numeric Types --- :class:`int`, :class:`float`, :class:`complex`
204 ================================================================
210 object: floating point
211 object: complex number
214 There are three distinct numeric types: :dfn:`integers`, :dfn:`floating
215 point numbers`, and :dfn:`complex numbers`. In addition, Booleans are a
216 subtype of integers. Integers have unlimited precision. Floating point
217 numbers are usually implemented using :c:type:`double` in C; information
218 about the precision and internal representation of floating point
219 numbers for the machine on which your program is running is available
220 in :data:`sys.float_info`. Complex numbers have a real and imaginary
221 part, which are each a floating point number. To extract these parts
222 from a complex number *z*, use ``z.real`` and ``z.imag``. (The standard
223 library includes the additional numeric types :mod:`fractions.Fraction`, for
224 rationals, and :mod:`decimal.Decimal`, for floating-point numbers with
225 user-definable precision.)
228 pair: numeric; literals
229 pair: integer; literals
230 pair: floating point; literals
231 pair: complex number; literals
232 pair: hexadecimal; literals
233 pair: octal; literals
234 pair: binary; literals
236 Numbers are created by numeric literals or as the result of built-in functions
237 and operators. Unadorned integer literals (including hex, octal and binary
238 numbers) yield integers. Numeric literals containing a decimal point or an
239 exponent sign yield floating point numbers. Appending ``'j'`` or ``'J'`` to a
240 numeric literal yields an imaginary number (a complex number with a zero real
241 part) which you can add to an integer or float to get a complex number with real
249 single: operator; + (plus)
250 single: + (plus); unary operator
251 single: + (plus); binary operator
252 single: operator; - (minus)
253 single: - (minus); unary operator
254 single: - (minus); binary operator
255 operator: * (asterisk)
258 operator: % (percent)
261 Python fully supports mixed arithmetic: when a binary arithmetic operator has
262 operands of different numeric types, the operand with the "narrower" type is
263 widened to that of the other, where integer is narrower than floating point,
264 which is narrower than complex. Comparisons between numbers of mixed type use
265 the same rule. [2]_ The constructors :func:`int`, :func:`float`, and
266 :func:`complex` can be used to produce numbers of a specific type.
268 All numeric types (except complex) support the following operations (for priorities of
269 the operations, see :ref:`operator-summary`):
271 +---------------------+---------------------------------+---------+--------------------+
272 | Operation | Result | Notes | Full documentation |
273 +=====================+=================================+=========+====================+
274 | ``x + y`` | sum of *x* and *y* | | |
275 +---------------------+---------------------------------+---------+--------------------+
276 | ``x - y`` | difference of *x* and *y* | | |
277 +---------------------+---------------------------------+---------+--------------------+
278 | ``x * y`` | product of *x* and *y* | | |
279 +---------------------+---------------------------------+---------+--------------------+
280 | ``x / y`` | quotient of *x* and *y* | | |
281 +---------------------+---------------------------------+---------+--------------------+
282 | ``x // y`` | floored quotient of *x* and | \(1) | |
284 +---------------------+---------------------------------+---------+--------------------+
285 | ``x % y`` | remainder of ``x / y`` | \(2) | |
286 +---------------------+---------------------------------+---------+--------------------+
287 | ``-x`` | *x* negated | | |
288 +---------------------+---------------------------------+---------+--------------------+
289 | ``+x`` | *x* unchanged | | |
290 +---------------------+---------------------------------+---------+--------------------+
291 | ``abs(x)`` | absolute value or magnitude of | | :func:`abs` |
293 +---------------------+---------------------------------+---------+--------------------+
294 | ``int(x)`` | *x* converted to integer | \(3)\(6)| :func:`int` |
295 +---------------------+---------------------------------+---------+--------------------+
296 | ``float(x)`` | *x* converted to floating point | \(4)\(6)| :func:`float` |
297 +---------------------+---------------------------------+---------+--------------------+
298 | ``complex(re, im)`` | a complex number with real part | \(6) | :func:`complex` |
299 | | *re*, imaginary part *im*. | | |
300 | | *im* defaults to zero. | | |
301 +---------------------+---------------------------------+---------+--------------------+
302 | ``c.conjugate()`` | conjugate of the complex number | | |
304 +---------------------+---------------------------------+---------+--------------------+
305 | ``divmod(x, y)`` | the pair ``(x // y, x % y)`` | \(2) | :func:`divmod` |
306 +---------------------+---------------------------------+---------+--------------------+
307 | ``pow(x, y)`` | *x* to the power *y* | \(5) | :func:`pow` |
308 +---------------------+---------------------------------+---------+--------------------+
309 | ``x ** y`` | *x* to the power *y* | \(5) | |
310 +---------------------+---------------------------------+---------+--------------------+
313 triple: operations on; numeric; types
314 single: conjugate() (complex number method)
319 Also referred to as integer division. The resultant value is a whole
320 integer, though the result's type is not necessarily int. The result is
321 always rounded towards minus infinity: ``1//2`` is ``0``, ``(-1)//2`` is
322 ``-1``, ``1//(-2)`` is ``-1``, and ``(-1)//(-2)`` is ``0``.
325 Not for complex numbers. Instead convert to floats using :func:`abs` if
331 single: floor() (in module math)
332 single: ceil() (in module math)
333 single: trunc() (in module math)
334 pair: numeric; conversions
337 Conversion from floating point to integer may round or truncate
338 as in C; see functions :func:`math.floor` and :func:`math.ceil` for
339 well-defined conversions.
342 float also accepts the strings "nan" and "inf" with an optional prefix "+"
343 or "-" for Not a Number (NaN) and positive or negative infinity.
346 Python defines ``pow(0, 0)`` and ``0 ** 0`` to be ``1``, as is common for
347 programming languages.
350 The numeric literals accepted include the digits ``0`` to ``9`` or any
351 Unicode equivalent (code points with the ``Nd`` property).
353 See http://www.unicode.org/Public/|UNIDATA_VERSION|/ucd/extracted/DerivedNumericType.txt
354 for a complete list of code points with the ``Nd`` property.
357 All :class:`numbers.Real` types (:class:`int` and :class:`float`) also include
358 the following operations:
360 +--------------------+---------------------------------------------+
361 | Operation | Result |
362 +====================+=============================================+
363 | :func:`math.trunc(\| *x* truncated to :class:`~numbers.Integral` |
364 | x) <math.trunc>` | |
365 +--------------------+---------------------------------------------+
366 | :func:`round(x[, | *x* rounded to *n* digits, |
367 | n]) <round>` | rounding half to even. If *n* is |
368 | | omitted, it defaults to 0. |
369 +--------------------+---------------------------------------------+
370 | :func:`math.floor(\| the greatest :class:`~numbers.Integral` |
371 | x) <math.floor>` | <= *x* |
372 +--------------------+---------------------------------------------+
373 | :func:`math.ceil(x)| the least :class:`~numbers.Integral` >= *x* |
375 +--------------------+---------------------------------------------+
377 For additional numeric operations see the :mod:`math` and :mod:`cmath`
380 .. XXXJH exceptions: overflow (when? what operations?) zerodivision
385 Bitwise Operations on Integer Types
386 -----------------------------------
389 triple: operations on; integer; types
390 pair: bitwise; operations
391 pair: shifting; operations
392 pair: masking; operations
393 operator: | (vertical bar)
395 operator: & (ampersand)
400 Bitwise operations only make sense for integers. The result of bitwise
401 operations is calculated as though carried out in two's complement with an
402 infinite number of sign bits.
404 The priorities of the binary bitwise operations are all lower than the numeric
405 operations and higher than the comparisons; the unary operation ``~`` has the
406 same priority as the other unary numeric operations (``+`` and ``-``).
408 This table lists the bitwise operations sorted in ascending priority:
410 +------------+--------------------------------+----------+
411 | Operation | Result | Notes |
412 +============+================================+==========+
413 | ``x | y`` | bitwise :dfn:`or` of *x* and | \(4) |
415 +------------+--------------------------------+----------+
416 | ``x ^ y`` | bitwise :dfn:`exclusive or` of | \(4) |
418 +------------+--------------------------------+----------+
419 | ``x & y`` | bitwise :dfn:`and` of *x* and | \(4) |
421 +------------+--------------------------------+----------+
422 | ``x << n`` | *x* shifted left by *n* bits | (1)(2) |
423 +------------+--------------------------------+----------+
424 | ``x >> n`` | *x* shifted right by *n* bits | (1)(3) |
425 +------------+--------------------------------+----------+
426 | ``~x`` | the bits of *x* inverted | |
427 +------------+--------------------------------+----------+
432 Negative shift counts are illegal and cause a :exc:`ValueError` to be raised.
435 A left shift by *n* bits is equivalent to multiplication by ``pow(2, n)``
436 without overflow check.
439 A right shift by *n* bits is equivalent to division by ``pow(2, n)`` without
443 Performing these calculations with at least one extra sign extension bit in
444 a finite two's complement representation (a working bit-width of
445 ``1 + max(x.bit_length(), y.bit_length())`` or more) is sufficient to get the
446 same result as if there were an infinite number of sign bits.
449 Additional Methods on Integer Types
450 -----------------------------------
452 The int type implements the :class:`numbers.Integral` :term:`abstract base
453 class`. In addition, it provides a few more methods:
455 .. method:: int.bit_length()
457 Return the number of bits necessary to represent an integer in binary,
458 excluding the sign and leading zeros::
466 More precisely, if ``x`` is nonzero, then ``x.bit_length()`` is the
467 unique positive integer ``k`` such that ``2**(k-1) <= abs(x) < 2**k``.
468 Equivalently, when ``abs(x)`` is small enough to have a correctly
469 rounded logarithm, then ``k = 1 + int(log(abs(x), 2))``.
470 If ``x`` is zero, then ``x.bit_length()`` returns ``0``.
474 def bit_length(self):
475 s = bin(self) # binary representation: bin(-37) --> '-0b100101'
476 s = s.lstrip('-0b') # remove leading zeros and minus sign
477 return len(s) # len('100101') --> 6
479 .. versionadded:: 3.1
481 .. method:: int.to_bytes(length, byteorder, \*, signed=False)
483 Return an array of bytes representing an integer.
485 >>> (1024).to_bytes(2, byteorder='big')
487 >>> (1024).to_bytes(10, byteorder='big')
488 b'\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00'
489 >>> (-1024).to_bytes(10, byteorder='big', signed=True)
490 b'\xff\xff\xff\xff\xff\xff\xff\xff\xfc\x00'
492 >>> x.to_bytes((x.bit_length() + 7) // 8, byteorder='little')
495 The integer is represented using *length* bytes. An :exc:`OverflowError`
496 is raised if the integer is not representable with the given number of
499 The *byteorder* argument determines the byte order used to represent the
500 integer. If *byteorder* is ``"big"``, the most significant byte is at the
501 beginning of the byte array. If *byteorder* is ``"little"``, the most
502 significant byte is at the end of the byte array. To request the native
503 byte order of the host system, use :data:`sys.byteorder` as the byte order
506 The *signed* argument determines whether two's complement is used to
507 represent the integer. If *signed* is ``False`` and a negative integer is
508 given, an :exc:`OverflowError` is raised. The default value for *signed*
511 .. versionadded:: 3.2
513 .. classmethod:: int.from_bytes(bytes, byteorder, \*, signed=False)
515 Return the integer represented by the given array of bytes.
517 >>> int.from_bytes(b'\x00\x10', byteorder='big')
519 >>> int.from_bytes(b'\x00\x10', byteorder='little')
521 >>> int.from_bytes(b'\xfc\x00', byteorder='big', signed=True)
523 >>> int.from_bytes(b'\xfc\x00', byteorder='big', signed=False)
525 >>> int.from_bytes([255, 0, 0], byteorder='big')
528 The argument *bytes* must either be a :term:`bytes-like object` or an
529 iterable producing bytes.
531 The *byteorder* argument determines the byte order used to represent the
532 integer. If *byteorder* is ``"big"``, the most significant byte is at the
533 beginning of the byte array. If *byteorder* is ``"little"``, the most
534 significant byte is at the end of the byte array. To request the native
535 byte order of the host system, use :data:`sys.byteorder` as the byte order
538 The *signed* argument indicates whether two's complement is used to
539 represent the integer.
541 .. versionadded:: 3.2
543 .. method:: int.as_integer_ratio()
545 Return a pair of integers whose ratio is exactly equal to the original
546 integer and with a positive denominator. The integer ratio of integers
547 (whole numbers) is always the integer as the numerator and ``1`` as the
550 .. versionadded:: 3.8
552 Additional Methods on Float
553 ---------------------------
555 The float type implements the :class:`numbers.Real` :term:`abstract base
556 class`. float also has the following additional methods.
558 .. method:: float.as_integer_ratio()
560 Return a pair of integers whose ratio is exactly equal to the
561 original float and with a positive denominator. Raises
562 :exc:`OverflowError` on infinities and a :exc:`ValueError` on
565 .. method:: float.is_integer()
567 Return ``True`` if the float instance is finite with integral
568 value, and ``False`` otherwise::
570 >>> (-2.0).is_integer()
572 >>> (3.2).is_integer()
575 Two methods support conversion to
576 and from hexadecimal strings. Since Python's floats are stored
577 internally as binary numbers, converting a float to or from a
578 *decimal* string usually involves a small rounding error. In
579 contrast, hexadecimal strings allow exact representation and
580 specification of floating-point numbers. This can be useful when
581 debugging, and in numerical work.
584 .. method:: float.hex()
586 Return a representation of a floating-point number as a hexadecimal
587 string. For finite floating-point numbers, this representation
588 will always include a leading ``0x`` and a trailing ``p`` and
592 .. classmethod:: float.fromhex(s)
594 Class method to return the float represented by a hexadecimal
595 string *s*. The string *s* may have leading and trailing
599 Note that :meth:`float.hex` is an instance method, while
600 :meth:`float.fromhex` is a class method.
602 A hexadecimal string takes the form::
604 [sign] ['0x'] integer ['.' fraction] ['p' exponent]
606 where the optional ``sign`` may by either ``+`` or ``-``, ``integer``
607 and ``fraction`` are strings of hexadecimal digits, and ``exponent``
608 is a decimal integer with an optional leading sign. Case is not
609 significant, and there must be at least one hexadecimal digit in
610 either the integer or the fraction. This syntax is similar to the
611 syntax specified in section 6.4.4.2 of the C99 standard, and also to
612 the syntax used in Java 1.5 onwards. In particular, the output of
613 :meth:`float.hex` is usable as a hexadecimal floating-point literal in
614 C or Java code, and hexadecimal strings produced by C's ``%a`` format
615 character or Java's ``Double.toHexString`` are accepted by
616 :meth:`float.fromhex`.
619 Note that the exponent is written in decimal rather than hexadecimal,
620 and that it gives the power of 2 by which to multiply the coefficient.
621 For example, the hexadecimal string ``0x3.a7p10`` represents the
622 floating-point number ``(3 + 10./16 + 7./16**2) * 2.0**10``, or
625 >>> float.fromhex('0x3.a7p10')
629 Applying the reverse conversion to ``3740.0`` gives a different
630 hexadecimal string representing the same number::
632 >>> float.hex(3740.0)
633 '0x1.d380000000000p+11'
638 Hashing of numeric types
639 ------------------------
641 For numbers ``x`` and ``y``, possibly of different types, it's a requirement
642 that ``hash(x) == hash(y)`` whenever ``x == y`` (see the :meth:`__hash__`
643 method documentation for more details). For ease of implementation and
644 efficiency across a variety of numeric types (including :class:`int`,
645 :class:`float`, :class:`decimal.Decimal` and :class:`fractions.Fraction`)
646 Python's hash for numeric types is based on a single mathematical function
647 that's defined for any rational number, and hence applies to all instances of
648 :class:`int` and :class:`fractions.Fraction`, and all finite instances of
649 :class:`float` and :class:`decimal.Decimal`. Essentially, this function is
650 given by reduction modulo ``P`` for a fixed prime ``P``. The value of ``P`` is
651 made available to Python as the :attr:`modulus` attribute of
652 :data:`sys.hash_info`.
656 Currently, the prime used is ``P = 2**31 - 1`` on machines with 32-bit C
657 longs and ``P = 2**61 - 1`` on machines with 64-bit C longs.
659 Here are the rules in detail:
661 - If ``x = m / n`` is a nonnegative rational number and ``n`` is not divisible
662 by ``P``, define ``hash(x)`` as ``m * invmod(n, P) % P``, where ``invmod(n,
663 P)`` gives the inverse of ``n`` modulo ``P``.
665 - If ``x = m / n`` is a nonnegative rational number and ``n`` is
666 divisible by ``P`` (but ``m`` is not) then ``n`` has no inverse
667 modulo ``P`` and the rule above doesn't apply; in this case define
668 ``hash(x)`` to be the constant value ``sys.hash_info.inf``.
670 - If ``x = m / n`` is a negative rational number define ``hash(x)``
671 as ``-hash(-x)``. If the resulting hash is ``-1``, replace it with
674 - The particular values ``sys.hash_info.inf``, ``-sys.hash_info.inf``
675 and ``sys.hash_info.nan`` are used as hash values for positive
676 infinity, negative infinity, or nans (respectively). (All hashable
677 nans have the same hash value.)
679 - For a :class:`complex` number ``z``, the hash values of the real
680 and imaginary parts are combined by computing ``hash(z.real) +
681 sys.hash_info.imag * hash(z.imag)``, reduced modulo
682 ``2**sys.hash_info.width`` so that it lies in
683 ``range(-2**(sys.hash_info.width - 1), 2**(sys.hash_info.width -
684 1))``. Again, if the result is ``-1``, it's replaced with ``-2``.
687 To clarify the above rules, here's some example Python code,
688 equivalent to the built-in hash, for computing the hash of a rational
689 number, :class:`float`, or :class:`complex`::
694 def hash_fraction(m, n):
695 """Compute the hash of a rational number m / n.
697 Assumes m and n are integers, with n positive.
698 Equivalent to hash(fractions.Fraction(m, n)).
701 P = sys.hash_info.modulus
702 # Remove common factors of P. (Unnecessary if m and n already coprime.)
703 while m % P == n % P == 0:
704 m, n = m // P, n // P
707 hash_value = sys.hash_info.inf
709 # Fermat's Little Theorem: pow(n, P-1, P) is 1, so
710 # pow(n, P-2, P) gives the inverse of n modulo P.
711 hash_value = (abs(m) % P) * pow(n, P - 2, P) % P
713 hash_value = -hash_value
719 """Compute the hash of a float x."""
722 return sys.hash_info.nan
724 return sys.hash_info.inf if x > 0 else -sys.hash_info.inf
726 return hash_fraction(*x.as_integer_ratio())
729 """Compute the hash of a complex number z."""
731 hash_value = hash_float(z.real) + sys.hash_info.imag * hash_float(z.imag)
732 # do a signed reduction modulo 2**sys.hash_info.width
733 M = 2**(sys.hash_info.width - 1)
734 hash_value = (hash_value & (M - 1)) - (hash_value & M)
745 single: iterator protocol
746 single: protocol; iterator
747 single: sequence; iteration
748 single: container; iteration over
750 Python supports a concept of iteration over containers. This is implemented
751 using two distinct methods; these are used to allow user-defined classes to
752 support iteration. Sequences, described below in more detail, always support
753 the iteration methods.
755 One method needs to be defined for container objects to provide iteration
758 .. XXX duplicated in reference/datamodel!
760 .. method:: container.__iter__()
762 Return an iterator object. The object is required to support the iterator
763 protocol described below. If a container supports different types of
764 iteration, additional methods can be provided to specifically request
765 iterators for those iteration types. (An example of an object supporting
766 multiple forms of iteration would be a tree structure which supports both
767 breadth-first and depth-first traversal.) This method corresponds to the
768 :c:member:`~PyTypeObject.tp_iter` slot of the type structure for Python objects in the Python/C
771 The iterator objects themselves are required to support the following two
772 methods, which together form the :dfn:`iterator protocol`:
775 .. method:: iterator.__iter__()
777 Return the iterator object itself. This is required to allow both containers
778 and iterators to be used with the :keyword:`for` and :keyword:`in` statements.
779 This method corresponds to the :c:member:`~PyTypeObject.tp_iter` slot of the type structure for
780 Python objects in the Python/C API.
783 .. method:: iterator.__next__()
785 Return the next item from the container. If there are no further items, raise
786 the :exc:`StopIteration` exception. This method corresponds to the
787 :c:member:`~PyTypeObject.tp_iternext` slot of the type structure for Python objects in the
790 Python defines several iterator objects to support iteration over general and
791 specific sequence types, dictionaries, and other more specialized forms. The
792 specific types are not important beyond their implementation of the iterator
795 Once an iterator's :meth:`~iterator.__next__` method raises
796 :exc:`StopIteration`, it must continue to do so on subsequent calls.
797 Implementations that do not obey this property are deemed broken.
805 Python's :term:`generator`\s provide a convenient way to implement the iterator
806 protocol. If a container object's :meth:`__iter__` method is implemented as a
807 generator, it will automatically return an iterator object (technically, a
808 generator object) supplying the :meth:`__iter__` and :meth:`~generator.__next__`
810 More information about generators can be found in :ref:`the documentation for
811 the yield expression <yieldexpr>`.
816 Sequence Types --- :class:`list`, :class:`tuple`, :class:`range`
817 ================================================================
819 There are three basic sequence types: lists, tuples, and range objects.
820 Additional sequence types tailored for processing of
821 :ref:`binary data <binaryseq>` and :ref:`text strings <textseq>` are
822 described in dedicated sections.
827 Common Sequence Operations
828 --------------------------
830 .. index:: object: sequence
832 The operations in the following table are supported by most sequence types,
833 both mutable and immutable. The :class:`collections.abc.Sequence` ABC is
834 provided to make it easier to correctly implement these operations on
835 custom sequence types.
837 This table lists the sequence operations sorted in ascending priority. In the
838 table, *s* and *t* are sequences of the same type, *n*, *i*, *j* and *k* are
839 integers and *x* is an arbitrary object that meets any type and value
840 restrictions imposed by *s*.
842 The ``in`` and ``not in`` operations have the same priorities as the
843 comparison operations. The ``+`` (concatenation) and ``*`` (repetition)
844 operations have the same priority as the corresponding numeric operations. [3]_
847 triple: operations on; sequence; types
851 pair: concatenation; operation
852 pair: repetition; operation
853 pair: subscript; operation
854 pair: slice; operation
857 single: count() (sequence method)
858 single: index() (sequence method)
860 +--------------------------+--------------------------------+----------+
861 | Operation | Result | Notes |
862 +==========================+================================+==========+
863 | ``x in s`` | ``True`` if an item of *s* is | \(1) |
864 | | equal to *x*, else ``False`` | |
865 +--------------------------+--------------------------------+----------+
866 | ``x not in s`` | ``False`` if an item of *s* is | \(1) |
867 | | equal to *x*, else ``True`` | |
868 +--------------------------+--------------------------------+----------+
869 | ``s + t`` | the concatenation of *s* and | (6)(7) |
871 +--------------------------+--------------------------------+----------+
872 | ``s * n`` or | equivalent to adding *s* to | (2)(7) |
873 | ``n * s`` | itself *n* times | |
874 +--------------------------+--------------------------------+----------+
875 | ``s[i]`` | *i*\ th item of *s*, origin 0 | \(3) |
876 +--------------------------+--------------------------------+----------+
877 | ``s[i:j]`` | slice of *s* from *i* to *j* | (3)(4) |
878 +--------------------------+--------------------------------+----------+
879 | ``s[i:j:k]`` | slice of *s* from *i* to *j* | (3)(5) |
880 | | with step *k* | |
881 +--------------------------+--------------------------------+----------+
882 | ``len(s)`` | length of *s* | |
883 +--------------------------+--------------------------------+----------+
884 | ``min(s)`` | smallest item of *s* | |
885 +--------------------------+--------------------------------+----------+
886 | ``max(s)`` | largest item of *s* | |
887 +--------------------------+--------------------------------+----------+
888 | ``s.index(x[, i[, j]])`` | index of the first occurrence | \(8) |
889 | | of *x* in *s* (at or after | |
890 | | index *i* and before index *j*)| |
891 +--------------------------+--------------------------------+----------+
892 | ``s.count(x)`` | total number of occurrences of | |
894 +--------------------------+--------------------------------+----------+
896 Sequences of the same type also support comparisons. In particular, tuples
897 and lists are compared lexicographically by comparing corresponding elements.
898 This means that to compare equal, every element must compare equal and the
899 two sequences must be of the same type and have the same length. (For full
900 details see :ref:`comparisons` in the language reference.)
905 While the ``in`` and ``not in`` operations are used only for simple
906 containment testing in the general case, some specialised sequences
907 (such as :class:`str`, :class:`bytes` and :class:`bytearray`) also use
908 them for subsequence testing::
914 Values of *n* less than ``0`` are treated as ``0`` (which yields an empty
915 sequence of the same type as *s*). Note that items in the sequence *s*
916 are not copied; they are referenced multiple times. This often haunts
917 new Python programmers; consider::
922 >>> lists[0].append(3)
926 What has happened is that ``[[]]`` is a one-element list containing an empty
927 list, so all three elements of ``[[]] * 3`` are references to this single empty
928 list. Modifying any of the elements of ``lists`` modifies this single list.
929 You can create a list of different lists this way::
931 >>> lists = [[] for i in range(3)]
932 >>> lists[0].append(3)
933 >>> lists[1].append(5)
934 >>> lists[2].append(7)
938 Further explanation is available in the FAQ entry
939 :ref:`faq-multidimensional-list`.
942 If *i* or *j* is negative, the index is relative to the end of sequence *s*:
943 ``len(s) + i`` or ``len(s) + j`` is substituted. But note that ``-0`` is
947 The slice of *s* from *i* to *j* is defined as the sequence of items with index
948 *k* such that ``i <= k < j``. If *i* or *j* is greater than ``len(s)``, use
949 ``len(s)``. If *i* is omitted or ``None``, use ``0``. If *j* is omitted or
950 ``None``, use ``len(s)``. If *i* is greater than or equal to *j*, the slice is
954 The slice of *s* from *i* to *j* with step *k* is defined as the sequence of
955 items with index ``x = i + n*k`` such that ``0 <= n < (j-i)/k``. In other words,
956 the indices are ``i``, ``i+k``, ``i+2*k``, ``i+3*k`` and so on, stopping when
957 *j* is reached (but never including *j*). When *k* is positive,
958 *i* and *j* are reduced to ``len(s)`` if they are greater.
959 When *k* is negative, *i* and *j* are reduced to ``len(s) - 1`` if
960 they are greater. If *i* or *j* are omitted or ``None``, they become
961 "end" values (which end depends on the sign of *k*). Note, *k* cannot be zero.
962 If *k* is ``None``, it is treated like ``1``.
965 Concatenating immutable sequences always results in a new object. This
966 means that building up a sequence by repeated concatenation will have a
967 quadratic runtime cost in the total sequence length. To get a linear
968 runtime cost, you must switch to one of the alternatives below:
970 * if concatenating :class:`str` objects, you can build a list and use
971 :meth:`str.join` at the end or else write to an :class:`io.StringIO`
972 instance and retrieve its value when complete
974 * if concatenating :class:`bytes` objects, you can similarly use
975 :meth:`bytes.join` or :class:`io.BytesIO`, or you can do in-place
976 concatenation with a :class:`bytearray` object. :class:`bytearray`
977 objects are mutable and have an efficient overallocation mechanism
979 * if concatenating :class:`tuple` objects, extend a :class:`list` instead
981 * for other types, investigate the relevant class documentation
985 Some sequence types (such as :class:`range`) only support item sequences
986 that follow specific patterns, and hence don't support sequence
987 concatenation or repetition.
990 ``index`` raises :exc:`ValueError` when *x* is not found in *s*.
991 Not all implementations support passing the additional arguments *i* and *j*.
992 These arguments allow efficient searching of subsections of the sequence. Passing
993 the extra arguments is roughly equivalent to using ``s[i:j].index(x)``, only
994 without copying any data and with the returned index being relative to
995 the start of the sequence rather than the start of the slice.
998 .. _typesseq-immutable:
1000 Immutable Sequence Types
1001 ------------------------
1004 triple: immutable; sequence; types
1008 The only operation that immutable sequence types generally implement that is
1009 not also implemented by mutable sequence types is support for the :func:`hash`
1012 This support allows immutable sequences, such as :class:`tuple` instances, to
1013 be used as :class:`dict` keys and stored in :class:`set` and :class:`frozenset`
1016 Attempting to hash an immutable sequence that contains unhashable values will
1017 result in :exc:`TypeError`.
1020 .. _typesseq-mutable:
1022 Mutable Sequence Types
1023 ----------------------
1026 triple: mutable; sequence; types
1030 The operations in the following table are defined on mutable sequence types.
1031 The :class:`collections.abc.MutableSequence` ABC is provided to make it
1032 easier to correctly implement these operations on custom sequence types.
1034 In the table *s* is an instance of a mutable sequence type, *t* is any
1035 iterable object and *x* is an arbitrary object that meets any type
1036 and value restrictions imposed by *s* (for example, :class:`bytearray` only
1037 accepts integers that meet the value restriction ``0 <= x <= 255``).
1041 triple: operations on; sequence; types
1042 triple: operations on; list; type
1043 pair: subscript; assignment
1044 pair: slice; assignment
1046 single: append() (sequence method)
1047 single: clear() (sequence method)
1048 single: copy() (sequence method)
1049 single: extend() (sequence method)
1050 single: insert() (sequence method)
1051 single: pop() (sequence method)
1052 single: remove() (sequence method)
1053 single: reverse() (sequence method)
1055 +------------------------------+--------------------------------+---------------------+
1056 | Operation | Result | Notes |
1057 +==============================+================================+=====================+
1058 | ``s[i] = x`` | item *i* of *s* is replaced by | |
1060 +------------------------------+--------------------------------+---------------------+
1061 | ``s[i:j] = t`` | slice of *s* from *i* to *j* | |
1062 | | is replaced by the contents of | |
1063 | | the iterable *t* | |
1064 +------------------------------+--------------------------------+---------------------+
1065 | ``del s[i:j]`` | same as ``s[i:j] = []`` | |
1066 +------------------------------+--------------------------------+---------------------+
1067 | ``s[i:j:k] = t`` | the elements of ``s[i:j:k]`` | \(1) |
1068 | | are replaced by those of *t* | |
1069 +------------------------------+--------------------------------+---------------------+
1070 | ``del s[i:j:k]`` | removes the elements of | |
1071 | | ``s[i:j:k]`` from the list | |
1072 +------------------------------+--------------------------------+---------------------+
1073 | ``s.append(x)`` | appends *x* to the end of the | |
1074 | | sequence (same as | |
1075 | | ``s[len(s):len(s)] = [x]``) | |
1076 +------------------------------+--------------------------------+---------------------+
1077 | ``s.clear()`` | removes all items from *s* | \(5) |
1078 | | (same as ``del s[:]``) | |
1079 +------------------------------+--------------------------------+---------------------+
1080 | ``s.copy()`` | creates a shallow copy of *s* | \(5) |
1081 | | (same as ``s[:]``) | |
1082 +------------------------------+--------------------------------+---------------------+
1083 | ``s.extend(t)`` or | extends *s* with the | |
1084 | ``s += t`` | contents of *t* (for the | |
1085 | | most part the same as | |
1086 | | ``s[len(s):len(s)] = t``) | |
1087 +------------------------------+--------------------------------+---------------------+
1088 | ``s *= n`` | updates *s* with its contents | \(6) |
1089 | | repeated *n* times | |
1090 +------------------------------+--------------------------------+---------------------+
1091 | ``s.insert(i, x)`` | inserts *x* into *s* at the | |
1092 | | index given by *i* | |
1093 | | (same as ``s[i:i] = [x]``) | |
1094 +------------------------------+--------------------------------+---------------------+
1095 | ``s.pop([i])`` | retrieves the item at *i* and | \(2) |
1096 | | also removes it from *s* | |
1097 +------------------------------+--------------------------------+---------------------+
1098 | ``s.remove(x)`` | remove the first item from *s* | \(3) |
1099 | | where ``s[i]`` is equal to *x* | |
1100 +------------------------------+--------------------------------+---------------------+
1101 | ``s.reverse()`` | reverses the items of *s* in | \(4) |
1103 +------------------------------+--------------------------------+---------------------+
1109 *t* must have the same length as the slice it is replacing.
1112 The optional argument *i* defaults to ``-1``, so that by default the last
1113 item is removed and returned.
1116 :meth:`remove` raises :exc:`ValueError` when *x* is not found in *s*.
1119 The :meth:`reverse` method modifies the sequence in place for economy of
1120 space when reversing a large sequence. To remind users that it operates by
1121 side effect, it does not return the reversed sequence.
1124 :meth:`clear` and :meth:`!copy` are included for consistency with the
1125 interfaces of mutable containers that don't support slicing operations
1126 (such as :class:`dict` and :class:`set`). :meth:`!copy` is not part of the
1127 :class:`collections.abc.MutableSequence` ABC, but most concrete
1128 mutable sequence classes provide it.
1130 .. versionadded:: 3.3
1131 :meth:`clear` and :meth:`!copy` methods.
1134 The value *n* is an integer, or an object implementing
1135 :meth:`~object.__index__`. Zero and negative values of *n* clear
1136 the sequence. Items in the sequence are not copied; they are referenced
1137 multiple times, as explained for ``s * n`` under :ref:`typesseq-common`.
1145 .. index:: object: list
1147 Lists are mutable sequences, typically used to store collections of
1148 homogeneous items (where the precise degree of similarity will vary by
1151 .. class:: list([iterable])
1153 Lists may be constructed in several ways:
1155 * Using a pair of square brackets to denote the empty list: ``[]``
1156 * Using square brackets, separating items with commas: ``[a]``, ``[a, b, c]``
1157 * Using a list comprehension: ``[x for x in iterable]``
1158 * Using the type constructor: ``list()`` or ``list(iterable)``
1160 The constructor builds a list whose items are the same and in the same
1161 order as *iterable*'s items. *iterable* may be either a sequence, a
1162 container that supports iteration, or an iterator object. If *iterable*
1163 is already a list, a copy is made and returned, similar to ``iterable[:]``.
1164 For example, ``list('abc')`` returns ``['a', 'b', 'c']`` and
1165 ``list( (1, 2, 3) )`` returns ``[1, 2, 3]``.
1166 If no argument is given, the constructor creates a new empty list, ``[]``.
1169 Many other operations also produce lists, including the :func:`sorted`
1172 Lists implement all of the :ref:`common <typesseq-common>` and
1173 :ref:`mutable <typesseq-mutable>` sequence operations. Lists also provide the
1174 following additional method:
1176 .. method:: list.sort(*, key=None, reverse=False)
1178 This method sorts the list in place, using only ``<`` comparisons
1179 between items. Exceptions are not suppressed - if any comparison operations
1180 fail, the entire sort operation will fail (and the list will likely be left
1181 in a partially modified state).
1183 :meth:`sort` accepts two arguments that can only be passed by keyword
1184 (:ref:`keyword-only arguments <keyword-only_parameter>`):
1186 *key* specifies a function of one argument that is used to extract a
1187 comparison key from each list element (for example, ``key=str.lower``).
1188 The key corresponding to each item in the list is calculated once and
1189 then used for the entire sorting process. The default value of ``None``
1190 means that list items are sorted directly without calculating a separate
1193 The :func:`functools.cmp_to_key` utility is available to convert a 2.x
1194 style *cmp* function to a *key* function.
1196 *reverse* is a boolean value. If set to ``True``, then the list elements
1197 are sorted as if each comparison were reversed.
1199 This method modifies the sequence in place for economy of space when
1200 sorting a large sequence. To remind users that it operates by side
1201 effect, it does not return the sorted sequence (use :func:`sorted` to
1202 explicitly request a new sorted list instance).
1204 The :meth:`sort` method is guaranteed to be stable. A sort is stable if it
1205 guarantees not to change the relative order of elements that compare equal
1206 --- this is helpful for sorting in multiple passes (for example, sort by
1207 department, then by salary grade).
1209 For sorting examples and a brief sorting tutorial, see :ref:`sortinghowto`.
1213 While a list is being sorted, the effect of attempting to mutate, or even
1214 inspect, the list is undefined. The C implementation of Python makes the
1215 list appear empty for the duration, and raises :exc:`ValueError` if it can
1216 detect that the list has been mutated during a sort.
1224 .. index:: object: tuple
1226 Tuples are immutable sequences, typically used to store collections of
1227 heterogeneous data (such as the 2-tuples produced by the :func:`enumerate`
1228 built-in). Tuples are also used for cases where an immutable sequence of
1229 homogeneous data is needed (such as allowing storage in a :class:`set` or
1230 :class:`dict` instance).
1232 .. class:: tuple([iterable])
1234 Tuples may be constructed in a number of ways:
1236 * Using a pair of parentheses to denote the empty tuple: ``()``
1237 * Using a trailing comma for a singleton tuple: ``a,`` or ``(a,)``
1238 * Separating items with commas: ``a, b, c`` or ``(a, b, c)``
1239 * Using the :func:`tuple` built-in: ``tuple()`` or ``tuple(iterable)``
1241 The constructor builds a tuple whose items are the same and in the same
1242 order as *iterable*'s items. *iterable* may be either a sequence, a
1243 container that supports iteration, or an iterator object. If *iterable*
1244 is already a tuple, it is returned unchanged. For example,
1245 ``tuple('abc')`` returns ``('a', 'b', 'c')`` and
1246 ``tuple( [1, 2, 3] )`` returns ``(1, 2, 3)``.
1247 If no argument is given, the constructor creates a new empty tuple, ``()``.
1249 Note that it is actually the comma which makes a tuple, not the parentheses.
1250 The parentheses are optional, except in the empty tuple case, or
1251 when they are needed to avoid syntactic ambiguity. For example,
1252 ``f(a, b, c)`` is a function call with three arguments, while
1253 ``f((a, b, c))`` is a function call with a 3-tuple as the sole argument.
1255 Tuples implement all of the :ref:`common <typesseq-common>` sequence
1258 For heterogeneous collections of data where access by name is clearer than
1259 access by index, :func:`collections.namedtuple` may be a more appropriate
1260 choice than a simple tuple object.
1268 .. index:: object: range
1270 The :class:`range` type represents an immutable sequence of numbers and is
1271 commonly used for looping a specific number of times in :keyword:`for`
1274 .. class:: range(stop)
1275 range(start, stop[, step])
1277 The arguments to the range constructor must be integers (either built-in
1278 :class:`int` or any object that implements the ``__index__`` special
1279 method). If the *step* argument is omitted, it defaults to ``1``.
1280 If the *start* argument is omitted, it defaults to ``0``.
1281 If *step* is zero, :exc:`ValueError` is raised.
1283 For a positive *step*, the contents of a range ``r`` are determined by the
1284 formula ``r[i] = start + step*i`` where ``i >= 0`` and
1287 For a negative *step*, the contents of the range are still determined by
1288 the formula ``r[i] = start + step*i``, but the constraints are ``i >= 0``
1289 and ``r[i] > stop``.
1291 A range object will be empty if ``r[0]`` does not meet the value
1292 constraint. Ranges do support negative indices, but these are interpreted
1293 as indexing from the end of the sequence determined by the positive
1296 Ranges containing absolute values larger than :data:`sys.maxsize` are
1297 permitted but some features (such as :func:`len`) may raise
1298 :exc:`OverflowError`.
1303 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
1304 >>> list(range(1, 11))
1305 [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
1306 >>> list(range(0, 30, 5))
1307 [0, 5, 10, 15, 20, 25]
1308 >>> list(range(0, 10, 3))
1310 >>> list(range(0, -10, -1))
1311 [0, -1, -2, -3, -4, -5, -6, -7, -8, -9]
1314 >>> list(range(1, 0))
1317 Ranges implement all of the :ref:`common <typesseq-common>` sequence operations
1318 except concatenation and repetition (due to the fact that range objects can
1319 only represent sequences that follow a strict pattern and repetition and
1320 concatenation will usually violate that pattern).
1322 .. attribute:: start
1324 The value of the *start* parameter (or ``0`` if the parameter was
1329 The value of the *stop* parameter
1333 The value of the *step* parameter (or ``1`` if the parameter was
1336 The advantage of the :class:`range` type over a regular :class:`list` or
1337 :class:`tuple` is that a :class:`range` object will always take the same
1338 (small) amount of memory, no matter the size of the range it represents (as it
1339 only stores the ``start``, ``stop`` and ``step`` values, calculating individual
1340 items and subranges as needed).
1342 Range objects implement the :class:`collections.abc.Sequence` ABC, and provide
1343 features such as containment tests, element index lookup, slicing and
1344 support for negative indices (see :ref:`typesseq`):
1346 >>> r = range(0, 20, 2)
1362 Testing range objects for equality with ``==`` and ``!=`` compares
1363 them as sequences. That is, two range objects are considered equal if
1364 they represent the same sequence of values. (Note that two range
1365 objects that compare equal might have different :attr:`~range.start`,
1366 :attr:`~range.stop` and :attr:`~range.step` attributes, for example
1367 ``range(0) == range(2, 1, 3)`` or ``range(0, 3, 2) == range(0, 4, 2)``.)
1369 .. versionchanged:: 3.2
1370 Implement the Sequence ABC.
1371 Support slicing and negative indices.
1372 Test :class:`int` objects for membership in constant time instead of
1373 iterating through all items.
1375 .. versionchanged:: 3.3
1376 Define '==' and '!=' to compare range objects based on the
1377 sequence of values they define (instead of comparing based on
1380 .. versionadded:: 3.3
1381 The :attr:`~range.start`, :attr:`~range.stop` and :attr:`~range.step`
1386 * The `linspace recipe <http://code.activestate.com/recipes/579000/>`_
1387 shows how to implement a lazy version of range suitable for floating
1391 single: string; text sequence type
1392 single: str (built-in class); (see also string)
1397 Text Sequence Type --- :class:`str`
1398 ===================================
1400 Textual data in Python is handled with :class:`str` objects, or :dfn:`strings`.
1401 Strings are immutable
1402 :ref:`sequences <typesseq>` of Unicode code points. String literals are
1403 written in a variety of ways:
1405 * Single quotes: ``'allows embedded "double" quotes'``
1406 * Double quotes: ``"allows embedded 'single' quotes"``.
1407 * Triple quoted: ``'''Three single quotes'''``, ``"""Three double quotes"""``
1409 Triple quoted strings may span multiple lines - all associated whitespace will
1410 be included in the string literal.
1412 String literals that are part of a single expression and have only whitespace
1413 between them will be implicitly converted to a single string literal. That
1414 is, ``("spam " "eggs") == "spam eggs"``.
1416 See :ref:`strings` for more about the various forms of string literal,
1417 including supported escape sequences, and the ``r`` ("raw") prefix that
1418 disables most escape sequence processing.
1420 Strings may also be created from other objects using the :class:`str`
1423 Since there is no separate "character" type, indexing a string produces
1424 strings of length 1. That is, for a non-empty string *s*, ``s[0] == s[0:1]``.
1429 There is also no mutable string type, but :meth:`str.join` or
1430 :class:`io.StringIO` can be used to efficiently construct strings from
1433 .. versionchanged:: 3.3
1434 For backwards compatibility with the Python 2 series, the ``u`` prefix is
1435 once again permitted on string literals. It has no effect on the meaning
1436 of string literals and cannot be combined with the ``r`` prefix.
1440 single: string; str (built-in class)
1442 .. class:: str(object='')
1443 str(object=b'', encoding='utf-8', errors='strict')
1445 Return a :ref:`string <textseq>` version of *object*. If *object* is not
1446 provided, returns the empty string. Otherwise, the behavior of ``str()``
1447 depends on whether *encoding* or *errors* is given, as follows.
1449 If neither *encoding* nor *errors* is given, ``str(object)`` returns
1450 :meth:`object.__str__() <object.__str__>`, which is the "informal" or nicely
1451 printable string representation of *object*. For string objects, this is
1452 the string itself. If *object* does not have a :meth:`~object.__str__`
1453 method, then :func:`str` falls back to returning
1454 :meth:`repr(object) <repr>`.
1457 single: buffer protocol; str (built-in class)
1458 single: bytes; str (built-in class)
1460 If at least one of *encoding* or *errors* is given, *object* should be a
1461 :term:`bytes-like object` (e.g. :class:`bytes` or :class:`bytearray`). In
1462 this case, if *object* is a :class:`bytes` (or :class:`bytearray`) object,
1463 then ``str(bytes, encoding, errors)`` is equivalent to
1464 :meth:`bytes.decode(encoding, errors) <bytes.decode>`. Otherwise, the bytes
1465 object underlying the buffer object is obtained before calling
1466 :meth:`bytes.decode`. See :ref:`binaryseq` and
1467 :ref:`bufferobjects` for information on buffer objects.
1469 Passing a :class:`bytes` object to :func:`str` without the *encoding*
1470 or *errors* arguments falls under the first case of returning the informal
1471 string representation (see also the :option:`-b` command-line option to
1472 Python). For example::
1477 For more information on the ``str`` class and its methods, see
1478 :ref:`textseq` and the :ref:`string-methods` section below. To output
1479 formatted strings, see the :ref:`f-strings` and :ref:`formatstrings`
1480 sections. In addition, see the :ref:`stringservices` section.
1484 pair: string; methods
1494 Strings implement all of the :ref:`common <typesseq-common>` sequence
1495 operations, along with the additional methods described below.
1497 Strings also support two styles of string formatting, one providing a large
1498 degree of flexibility and customization (see :meth:`str.format`,
1499 :ref:`formatstrings` and :ref:`string-formatting`) and the other based on C
1500 ``printf`` style formatting that handles a narrower range of types and is
1501 slightly harder to use correctly, but is often faster for the cases it can
1502 handle (:ref:`old-string-formatting`).
1504 The :ref:`textservices` section of the standard library covers a number of
1505 other modules that provide various text related utilities (including regular
1506 expression support in the :mod:`re` module).
1508 .. method:: str.capitalize()
1510 Return a copy of the string with its first character capitalized and the
1513 .. versionchanged:: 3.8
1514 The first character is now put into titlecase rather than uppercase.
1515 This means that characters like digraphs will only have their first
1516 letter capitalized, instead of the full character.
1518 .. method:: str.casefold()
1520 Return a casefolded copy of the string. Casefolded strings may be used for
1523 Casefolding is similar to lowercasing but more aggressive because it is
1524 intended to remove all case distinctions in a string. For example, the German
1525 lowercase letter ``'ß'`` is equivalent to ``"ss"``. Since it is already
1526 lowercase, :meth:`lower` would do nothing to ``'ß'``; :meth:`casefold`
1527 converts it to ``"ss"``.
1529 The casefolding algorithm is described in section 3.13 of the Unicode
1532 .. versionadded:: 3.3
1535 .. method:: str.center(width[, fillchar])
1537 Return centered in a string of length *width*. Padding is done using the
1538 specified *fillchar* (default is an ASCII space). The original string is
1539 returned if *width* is less than or equal to ``len(s)``.
1543 .. method:: str.count(sub[, start[, end]])
1545 Return the number of non-overlapping occurrences of substring *sub* in the
1546 range [*start*, *end*]. Optional arguments *start* and *end* are
1547 interpreted as in slice notation.
1550 .. method:: str.encode(encoding="utf-8", errors="strict")
1552 Return an encoded version of the string as a bytes object. Default encoding
1553 is ``'utf-8'``. *errors* may be given to set a different error handling scheme.
1554 The default for *errors* is ``'strict'``, meaning that encoding errors raise
1555 a :exc:`UnicodeError`. Other possible
1556 values are ``'ignore'``, ``'replace'``, ``'xmlcharrefreplace'``,
1557 ``'backslashreplace'`` and any other name registered via
1558 :func:`codecs.register_error`, see section :ref:`error-handlers`. For a
1559 list of possible encodings, see section :ref:`standard-encodings`.
1561 By default, the *errors* argument is not checked for best performances, but
1562 only used at the first encoding error. Enable the development mode
1563 (:option:`-X` ``dev`` option), or use a debug build, to check *errors*.
1565 .. versionchanged:: 3.1
1566 Support for keyword arguments added.
1568 .. versionchanged:: 3.9
1569 The *errors* is now checked in development mode and in debug mode.
1572 .. method:: str.endswith(suffix[, start[, end]])
1574 Return ``True`` if the string ends with the specified *suffix*, otherwise return
1575 ``False``. *suffix* can also be a tuple of suffixes to look for. With optional
1576 *start*, test beginning at that position. With optional *end*, stop comparing
1580 .. method:: str.expandtabs(tabsize=8)
1582 Return a copy of the string where all tab characters are replaced by one or
1583 more spaces, depending on the current column and the given tab size. Tab
1584 positions occur every *tabsize* characters (default is 8, giving tab
1585 positions at columns 0, 8, 16 and so on). To expand the string, the current
1586 column is set to zero and the string is examined character by character. If
1587 the character is a tab (``\t``), one or more space characters are inserted
1588 in the result until the current column is equal to the next tab position.
1589 (The tab character itself is not copied.) If the character is a newline
1590 (``\n``) or return (``\r``), it is copied and the current column is reset to
1591 zero. Any other character is copied unchanged and the current column is
1592 incremented by one regardless of how the character is represented when
1595 >>> '01\t012\t0123\t01234'.expandtabs()
1597 >>> '01\t012\t0123\t01234'.expandtabs(4)
1601 .. method:: str.find(sub[, start[, end]])
1603 Return the lowest index in the string where substring *sub* is found within
1604 the slice ``s[start:end]``. Optional arguments *start* and *end* are
1605 interpreted as in slice notation. Return ``-1`` if *sub* is not found.
1609 The :meth:`~str.find` method should be used only if you need to know the
1610 position of *sub*. To check if *sub* is a substring or not, use the
1611 :keyword:`in` operator::
1613 >>> 'Py' in 'Python'
1617 .. method:: str.format(*args, **kwargs)
1619 Perform a string formatting operation. The string on which this method is
1620 called can contain literal text or replacement fields delimited by braces
1621 ``{}``. Each replacement field contains either the numeric index of a
1622 positional argument, or the name of a keyword argument. Returns a copy of
1623 the string where each replacement field is replaced with the string value of
1624 the corresponding argument.
1626 >>> "The sum of 1 + 2 is {0}".format(1+2)
1627 'The sum of 1 + 2 is 3'
1629 See :ref:`formatstrings` for a description of the various formatting options
1630 that can be specified in format strings.
1633 When formatting a number (:class:`int`, :class:`float`, :class:`complex`,
1634 :class:`decimal.Decimal` and subclasses) with the ``n`` type
1635 (ex: ``'{:n}'.format(1234)``), the function temporarily sets the
1636 ``LC_CTYPE`` locale to the ``LC_NUMERIC`` locale to decode
1637 ``decimal_point`` and ``thousands_sep`` fields of :c:func:`localeconv` if
1638 they are non-ASCII or longer than 1 byte, and the ``LC_NUMERIC`` locale is
1639 different than the ``LC_CTYPE`` locale. This temporary change affects
1642 .. versionchanged:: 3.7
1643 When formatting a number with the ``n`` type, the function sets
1644 temporarily the ``LC_CTYPE`` locale to the ``LC_NUMERIC`` locale in some
1648 .. method:: str.format_map(mapping)
1650 Similar to ``str.format(**mapping)``, except that ``mapping`` is
1651 used directly and not copied to a :class:`dict`. This is useful
1652 if for example ``mapping`` is a dict subclass:
1654 >>> class Default(dict):
1655 ... def __missing__(self, key):
1658 >>> '{name} was born in {country}'.format_map(Default(name='Guido'))
1659 'Guido was born in country'
1661 .. versionadded:: 3.2
1664 .. method:: str.index(sub[, start[, end]])
1666 Like :meth:`~str.find`, but raise :exc:`ValueError` when the substring is
1670 .. method:: str.isalnum()
1672 Return ``True`` if all characters in the string are alphanumeric and there is at
1673 least one character, ``False`` otherwise. A character ``c`` is alphanumeric if one
1674 of the following returns ``True``: ``c.isalpha()``, ``c.isdecimal()``,
1675 ``c.isdigit()``, or ``c.isnumeric()``.
1678 .. method:: str.isalpha()
1680 Return ``True`` if all characters in the string are alphabetic and there is at least
1681 one character, ``False`` otherwise. Alphabetic characters are those characters defined
1682 in the Unicode character database as "Letter", i.e., those with general category
1683 property being one of "Lm", "Lt", "Lu", "Ll", or "Lo". Note that this is different
1684 from the "Alphabetic" property defined in the Unicode Standard.
1687 .. method:: str.isascii()
1689 Return ``True`` if the string is empty or all characters in the string are ASCII,
1690 ``False`` otherwise.
1691 ASCII characters have code points in the range U+0000-U+007F.
1693 .. versionadded:: 3.7
1696 .. method:: str.isdecimal()
1698 Return ``True`` if all characters in the string are decimal
1699 characters and there is at least one character, ``False``
1700 otherwise. Decimal characters are those that can be used to form
1701 numbers in base 10, e.g. U+0660, ARABIC-INDIC DIGIT
1702 ZERO. Formally a decimal character is a character in the Unicode
1703 General Category "Nd".
1706 .. method:: str.isdigit()
1708 Return ``True`` if all characters in the string are digits and there is at least one
1709 character, ``False`` otherwise. Digits include decimal characters and digits that need
1710 special handling, such as the compatibility superscript digits.
1711 This covers digits which cannot be used to form numbers in base 10,
1712 like the Kharosthi numbers. Formally, a digit is a character that has the
1713 property value Numeric_Type=Digit or Numeric_Type=Decimal.
1716 .. method:: str.isidentifier()
1718 Return ``True`` if the string is a valid identifier according to the language
1719 definition, section :ref:`identifiers`.
1721 Call :func:`keyword.iskeyword` to test whether string ``s`` is a reserved
1722 identifier, such as :keyword:`def` and :keyword:`class`.
1727 >>> from keyword import iskeyword
1729 >>> 'hello'.isidentifier(), iskeyword('hello')
1731 >>> 'def'.isidentifier(), iskeyword('def')
1735 .. method:: str.islower()
1737 Return ``True`` if all cased characters [4]_ in the string are lowercase and
1738 there is at least one cased character, ``False`` otherwise.
1741 .. method:: str.isnumeric()
1743 Return ``True`` if all characters in the string are numeric
1744 characters, and there is at least one character, ``False``
1745 otherwise. Numeric characters include digit characters, and all characters
1746 that have the Unicode numeric value property, e.g. U+2155,
1747 VULGAR FRACTION ONE FIFTH. Formally, numeric characters are those with the property
1748 value Numeric_Type=Digit, Numeric_Type=Decimal or Numeric_Type=Numeric.
1751 .. method:: str.isprintable()
1753 Return ``True`` if all characters in the string are printable or the string is
1754 empty, ``False`` otherwise. Nonprintable characters are those characters defined
1755 in the Unicode character database as "Other" or "Separator", excepting the
1756 ASCII space (0x20) which is considered printable. (Note that printable
1757 characters in this context are those which should not be escaped when
1758 :func:`repr` is invoked on a string. It has no bearing on the handling of
1759 strings written to :data:`sys.stdout` or :data:`sys.stderr`.)
1762 .. method:: str.isspace()
1764 Return ``True`` if there are only whitespace characters in the string and there is
1765 at least one character, ``False`` otherwise.
1767 A character is *whitespace* if in the Unicode character database
1768 (see :mod:`unicodedata`), either its general category is ``Zs``
1769 ("Separator, space"), or its bidirectional class is one of ``WS``,
1773 .. method:: str.istitle()
1775 Return ``True`` if the string is a titlecased string and there is at least one
1776 character, for example uppercase characters may only follow uncased characters
1777 and lowercase characters only cased ones. Return ``False`` otherwise.
1780 .. method:: str.isupper()
1782 Return ``True`` if all cased characters [4]_ in the string are uppercase and
1783 there is at least one cased character, ``False`` otherwise.
1785 >>> 'BANANA'.isupper()
1787 >>> 'banana'.isupper()
1789 >>> 'baNana'.isupper()
1796 .. method:: str.join(iterable)
1798 Return a string which is the concatenation of the strings in *iterable*.
1799 A :exc:`TypeError` will be raised if there are any non-string values in
1800 *iterable*, including :class:`bytes` objects. The separator between
1801 elements is the string providing this method.
1804 .. method:: str.ljust(width[, fillchar])
1806 Return the string left justified in a string of length *width*. Padding is
1807 done using the specified *fillchar* (default is an ASCII space). The
1808 original string is returned if *width* is less than or equal to ``len(s)``.
1811 .. method:: str.lower()
1813 Return a copy of the string with all the cased characters [4]_ converted to
1816 The lowercasing algorithm used is described in section 3.13 of the Unicode
1820 .. method:: str.lstrip([chars])
1822 Return a copy of the string with leading characters removed. The *chars*
1823 argument is a string specifying the set of characters to be removed. If omitted
1824 or ``None``, the *chars* argument defaults to removing whitespace. The *chars*
1825 argument is not a prefix; rather, all combinations of its values are stripped::
1827 >>> ' spacious '.lstrip()
1829 >>> 'www.example.com'.lstrip('cmowz.')
1833 .. staticmethod:: str.maketrans(x[, y[, z]])
1835 This static method returns a translation table usable for :meth:`str.translate`.
1837 If there is only one argument, it must be a dictionary mapping Unicode
1838 ordinals (integers) or characters (strings of length 1) to Unicode ordinals,
1839 strings (of arbitrary lengths) or ``None``. Character keys will then be
1840 converted to ordinals.
1842 If there are two arguments, they must be strings of equal length, and in the
1843 resulting dictionary, each character in x will be mapped to the character at
1844 the same position in y. If there is a third argument, it must be a string,
1845 whose characters will be mapped to ``None`` in the result.
1848 .. method:: str.partition(sep)
1850 Split the string at the first occurrence of *sep*, and return a 3-tuple
1851 containing the part before the separator, the separator itself, and the part
1852 after the separator. If the separator is not found, return a 3-tuple containing
1853 the string itself, followed by two empty strings.
1856 .. method:: str.replace(old, new[, count])
1858 Return a copy of the string with all occurrences of substring *old* replaced by
1859 *new*. If the optional argument *count* is given, only the first *count*
1860 occurrences are replaced.
1863 .. method:: str.rfind(sub[, start[, end]])
1865 Return the highest index in the string where substring *sub* is found, such
1866 that *sub* is contained within ``s[start:end]``. Optional arguments *start*
1867 and *end* are interpreted as in slice notation. Return ``-1`` on failure.
1870 .. method:: str.rindex(sub[, start[, end]])
1872 Like :meth:`rfind` but raises :exc:`ValueError` when the substring *sub* is not
1876 .. method:: str.rjust(width[, fillchar])
1878 Return the string right justified in a string of length *width*. Padding is
1879 done using the specified *fillchar* (default is an ASCII space). The
1880 original string is returned if *width* is less than or equal to ``len(s)``.
1883 .. method:: str.rpartition(sep)
1885 Split the string at the last occurrence of *sep*, and return a 3-tuple
1886 containing the part before the separator, the separator itself, and the part
1887 after the separator. If the separator is not found, return a 3-tuple containing
1888 two empty strings, followed by the string itself.
1891 .. method:: str.rsplit(sep=None, maxsplit=-1)
1893 Return a list of the words in the string, using *sep* as the delimiter string.
1894 If *maxsplit* is given, at most *maxsplit* splits are done, the *rightmost*
1895 ones. If *sep* is not specified or ``None``, any whitespace string is a
1896 separator. Except for splitting from the right, :meth:`rsplit` behaves like
1897 :meth:`split` which is described in detail below.
1900 .. method:: str.rstrip([chars])
1902 Return a copy of the string with trailing characters removed. The *chars*
1903 argument is a string specifying the set of characters to be removed. If omitted
1904 or ``None``, the *chars* argument defaults to removing whitespace. The *chars*
1905 argument is not a suffix; rather, all combinations of its values are stripped::
1907 >>> ' spacious '.rstrip()
1909 >>> 'mississippi'.rstrip('ipz')
1913 .. method:: str.split(sep=None, maxsplit=-1)
1915 Return a list of the words in the string, using *sep* as the delimiter
1916 string. If *maxsplit* is given, at most *maxsplit* splits are done (thus,
1917 the list will have at most ``maxsplit+1`` elements). If *maxsplit* is not
1918 specified or ``-1``, then there is no limit on the number of splits
1919 (all possible splits are made).
1921 If *sep* is given, consecutive delimiters are not grouped together and are
1922 deemed to delimit empty strings (for example, ``'1,,2'.split(',')`` returns
1923 ``['1', '', '2']``). The *sep* argument may consist of multiple characters
1924 (for example, ``'1<>2<>3'.split('<>')`` returns ``['1', '2', '3']``).
1925 Splitting an empty string with a specified separator returns ``['']``.
1929 >>> '1,2,3'.split(',')
1931 >>> '1,2,3'.split(',', maxsplit=1)
1933 >>> '1,2,,3,'.split(',')
1934 ['1', '2', '', '3', '']
1936 If *sep* is not specified or is ``None``, a different splitting algorithm is
1937 applied: runs of consecutive whitespace are regarded as a single separator,
1938 and the result will contain no empty strings at the start or end if the
1939 string has leading or trailing whitespace. Consequently, splitting an empty
1940 string or a string consisting of just whitespace with a ``None`` separator
1947 >>> '1 2 3'.split(maxsplit=1)
1949 >>> ' 1 2 3 '.split()
1954 single: universal newlines; str.splitlines method
1956 .. method:: str.splitlines([keepends])
1958 Return a list of the lines in the string, breaking at line boundaries. Line
1959 breaks are not included in the resulting list unless *keepends* is given and
1962 This method splits on the following line boundaries. In particular, the
1963 boundaries are a superset of :term:`universal newlines`.
1965 +-----------------------+-----------------------------+
1966 | Representation | Description |
1967 +=======================+=============================+
1968 | ``\n`` | Line Feed |
1969 +-----------------------+-----------------------------+
1970 | ``\r`` | Carriage Return |
1971 +-----------------------+-----------------------------+
1972 | ``\r\n`` | Carriage Return + Line Feed |
1973 +-----------------------+-----------------------------+
1974 | ``\v`` or ``\x0b`` | Line Tabulation |
1975 +-----------------------+-----------------------------+
1976 | ``\f`` or ``\x0c`` | Form Feed |
1977 +-----------------------+-----------------------------+
1978 | ``\x1c`` | File Separator |
1979 +-----------------------+-----------------------------+
1980 | ``\x1d`` | Group Separator |
1981 +-----------------------+-----------------------------+
1982 | ``\x1e`` | Record Separator |
1983 +-----------------------+-----------------------------+
1984 | ``\x85`` | Next Line (C1 Control Code) |
1985 +-----------------------+-----------------------------+
1986 | ``\u2028`` | Line Separator |
1987 +-----------------------+-----------------------------+
1988 | ``\u2029`` | Paragraph Separator |
1989 +-----------------------+-----------------------------+
1991 .. versionchanged:: 3.2
1993 ``\v`` and ``\f`` added to list of line boundaries.
1997 >>> 'ab c\n\nde fg\rkl\r\n'.splitlines()
1998 ['ab c', '', 'de fg', 'kl']
1999 >>> 'ab c\n\nde fg\rkl\r\n'.splitlines(keepends=True)
2000 ['ab c\n', '\n', 'de fg\r', 'kl\r\n']
2002 Unlike :meth:`~str.split` when a delimiter string *sep* is given, this
2003 method returns an empty list for the empty string, and a terminal line
2004 break does not result in an extra line::
2008 >>> "One line\n".splitlines()
2011 For comparison, ``split('\n')`` gives::
2015 >>> 'Two lines\n'.split('\n')
2019 .. method:: str.startswith(prefix[, start[, end]])
2021 Return ``True`` if string starts with the *prefix*, otherwise return ``False``.
2022 *prefix* can also be a tuple of prefixes to look for. With optional *start*,
2023 test string beginning at that position. With optional *end*, stop comparing
2024 string at that position.
2027 .. method:: str.strip([chars])
2029 Return a copy of the string with the leading and trailing characters removed.
2030 The *chars* argument is a string specifying the set of characters to be removed.
2031 If omitted or ``None``, the *chars* argument defaults to removing whitespace.
2032 The *chars* argument is not a prefix or suffix; rather, all combinations of its
2033 values are stripped::
2035 >>> ' spacious '.strip()
2037 >>> 'www.example.com'.strip('cmowz.')
2040 The outermost leading and trailing *chars* argument values are stripped
2041 from the string. Characters are removed from the leading end until
2042 reaching a string character that is not contained in the set of
2043 characters in *chars*. A similar action takes place on the trailing end.
2046 >>> comment_string = '#....... Section 3.2.1 Issue #32 .......'
2047 >>> comment_string.strip('.#! ')
2048 'Section 3.2.1 Issue #32'
2051 .. method:: str.swapcase()
2053 Return a copy of the string with uppercase characters converted to lowercase and
2054 vice versa. Note that it is not necessarily true that
2055 ``s.swapcase().swapcase() == s``.
2058 .. method:: str.title()
2060 Return a titlecased version of the string where words start with an uppercase
2061 character and the remaining characters are lowercase.
2065 >>> 'Hello world'.title()
2068 The algorithm uses a simple language-independent definition of a word as
2069 groups of consecutive letters. The definition works in many contexts but
2070 it means that apostrophes in contractions and possessives form word
2071 boundaries, which may not be the desired result::
2073 >>> "they're bill's friends from the UK".title()
2074 "They'Re Bill'S Friends From The Uk"
2076 A workaround for apostrophes can be constructed using regular expressions::
2079 >>> def titlecase(s):
2080 ... return re.sub(r"[A-Za-z]+('[A-Za-z]+)?",
2081 ... lambda mo: mo.group(0).capitalize(),
2084 >>> titlecase("they're bill's friends.")
2085 "They're Bill's Friends."
2088 .. method:: str.translate(table)
2090 Return a copy of the string in which each character has been mapped through
2091 the given translation table. The table must be an object that implements
2092 indexing via :meth:`__getitem__`, typically a :term:`mapping` or
2093 :term:`sequence`. When indexed by a Unicode ordinal (an integer), the
2094 table object can do any of the following: return a Unicode ordinal or a
2095 string, to map the character to one or more other characters; return
2096 ``None``, to delete the character from the return string; or raise a
2097 :exc:`LookupError` exception, to map the character to itself.
2099 You can use :meth:`str.maketrans` to create a translation map from
2100 character-to-character mappings in different formats.
2102 See also the :mod:`codecs` module for a more flexible approach to custom
2106 .. method:: str.upper()
2108 Return a copy of the string with all the cased characters [4]_ converted to
2109 uppercase. Note that ``s.upper().isupper()`` might be ``False`` if ``s``
2110 contains uncased characters or if the Unicode category of the resulting
2111 character(s) is not "Lu" (Letter, uppercase), but e.g. "Lt" (Letter,
2114 The uppercasing algorithm used is described in section 3.13 of the Unicode
2118 .. method:: str.zfill(width)
2120 Return a copy of the string left filled with ASCII ``'0'`` digits to
2121 make a string of length *width*. A leading sign prefix (``'+'``/``'-'``)
2122 is handled by inserting the padding *after* the sign character rather
2123 than before. The original string is returned if *width* is less than
2124 or equal to ``len(s)``.
2135 .. _old-string-formatting:
2137 ``printf``-style String Formatting
2138 ----------------------------------
2141 single: formatting, string (%)
2142 single: interpolation, string (%)
2143 single: string; formatting, printf
2144 single: string; interpolation, printf
2145 single: printf-style formatting
2146 single: sprintf-style formatting
2147 single: % (percent); printf-style formatting
2151 The formatting operations described here exhibit a variety of quirks that
2152 lead to a number of common errors (such as failing to display tuples and
2153 dictionaries correctly). Using the newer :ref:`formatted string literals
2154 <f-strings>`, the :meth:`str.format` interface, or :ref:`template strings
2155 <template-strings>` may help avoid these errors. Each of these
2156 alternatives provides their own trade-offs and benefits of simplicity,
2157 flexibility, and/or extensibility.
2159 String objects have one unique built-in operation: the ``%`` operator (modulo).
2160 This is also known as the string *formatting* or *interpolation* operator.
2161 Given ``format % values`` (where *format* is a string), ``%`` conversion
2162 specifications in *format* are replaced with zero or more elements of *values*.
2163 The effect is similar to using the :c:func:`sprintf` in the C language.
2165 If *format* requires a single argument, *values* may be a single non-tuple
2166 object. [5]_ Otherwise, *values* must be a tuple with exactly the number of
2167 items specified by the format string, or a single mapping object (for example, a
2171 single: () (parentheses); in printf-style formatting
2172 single: * (asterisk); in printf-style formatting
2173 single: . (dot); in printf-style formatting
2175 A conversion specifier contains two or more characters and has the following
2176 components, which must occur in this order:
2178 #. The ``'%'`` character, which marks the start of the specifier.
2180 #. Mapping key (optional), consisting of a parenthesised sequence of characters
2181 (for example, ``(somename)``).
2183 #. Conversion flags (optional), which affect the result of some conversion
2186 #. Minimum field width (optional). If specified as an ``'*'`` (asterisk), the
2187 actual width is read from the next element of the tuple in *values*, and the
2188 object to convert comes after the minimum field width and optional precision.
2190 #. Precision (optional), given as a ``'.'`` (dot) followed by the precision. If
2191 specified as ``'*'`` (an asterisk), the actual precision is read from the next
2192 element of the tuple in *values*, and the value to convert comes after the
2195 #. Length modifier (optional).
2199 When the right argument is a dictionary (or other mapping type), then the
2200 formats in the string *must* include a parenthesised mapping key into that
2201 dictionary inserted immediately after the ``'%'`` character. The mapping key
2202 selects the value to be formatted from the mapping. For example:
2204 >>> print('%(language)s has %(number)03d quote types.' %
2205 ... {'language': "Python", "number": 2})
2206 Python has 002 quote types.
2208 In this case no ``*`` specifiers may occur in a format (since they require a
2209 sequential parameter list).
2211 The conversion flag characters are:
2214 single: # (hash); in printf-style formatting
2215 single: - (minus); in printf-style formatting
2216 single: + (plus); in printf-style formatting
2217 single: space; in printf-style formatting
2219 +---------+---------------------------------------------------------------------+
2221 +=========+=====================================================================+
2222 | ``'#'`` | The value conversion will use the "alternate form" (where defined |
2224 +---------+---------------------------------------------------------------------+
2225 | ``'0'`` | The conversion will be zero padded for numeric values. |
2226 +---------+---------------------------------------------------------------------+
2227 | ``'-'`` | The converted value is left adjusted (overrides the ``'0'`` |
2228 | | conversion if both are given). |
2229 +---------+---------------------------------------------------------------------+
2230 | ``' '`` | (a space) A blank should be left before a positive number (or empty |
2231 | | string) produced by a signed conversion. |
2232 +---------+---------------------------------------------------------------------+
2233 | ``'+'`` | A sign character (``'+'`` or ``'-'``) will precede the conversion |
2234 | | (overrides a "space" flag). |
2235 +---------+---------------------------------------------------------------------+
2237 A length modifier (``h``, ``l``, or ``L``) may be present, but is ignored as it
2238 is not necessary for Python -- so e.g. ``%ld`` is identical to ``%d``.
2240 The conversion types are:
2242 +------------+-----------------------------------------------------+-------+
2243 | Conversion | Meaning | Notes |
2244 +============+=====================================================+=======+
2245 | ``'d'`` | Signed integer decimal. | |
2246 +------------+-----------------------------------------------------+-------+
2247 | ``'i'`` | Signed integer decimal. | |
2248 +------------+-----------------------------------------------------+-------+
2249 | ``'o'`` | Signed octal value. | \(1) |
2250 +------------+-----------------------------------------------------+-------+
2251 | ``'u'`` | Obsolete type -- it is identical to ``'d'``. | \(6) |
2252 +------------+-----------------------------------------------------+-------+
2253 | ``'x'`` | Signed hexadecimal (lowercase). | \(2) |
2254 +------------+-----------------------------------------------------+-------+
2255 | ``'X'`` | Signed hexadecimal (uppercase). | \(2) |
2256 +------------+-----------------------------------------------------+-------+
2257 | ``'e'`` | Floating point exponential format (lowercase). | \(3) |
2258 +------------+-----------------------------------------------------+-------+
2259 | ``'E'`` | Floating point exponential format (uppercase). | \(3) |
2260 +------------+-----------------------------------------------------+-------+
2261 | ``'f'`` | Floating point decimal format. | \(3) |
2262 +------------+-----------------------------------------------------+-------+
2263 | ``'F'`` | Floating point decimal format. | \(3) |
2264 +------------+-----------------------------------------------------+-------+
2265 | ``'g'`` | Floating point format. Uses lowercase exponential | \(4) |
2266 | | format if exponent is less than -4 or not less than | |
2267 | | precision, decimal format otherwise. | |
2268 +------------+-----------------------------------------------------+-------+
2269 | ``'G'`` | Floating point format. Uses uppercase exponential | \(4) |
2270 | | format if exponent is less than -4 or not less than | |
2271 | | precision, decimal format otherwise. | |
2272 +------------+-----------------------------------------------------+-------+
2273 | ``'c'`` | Single character (accepts integer or single | |
2274 | | character string). | |
2275 +------------+-----------------------------------------------------+-------+
2276 | ``'r'`` | String (converts any Python object using | \(5) |
2277 | | :func:`repr`). | |
2278 +------------+-----------------------------------------------------+-------+
2279 | ``'s'`` | String (converts any Python object using | \(5) |
2280 | | :func:`str`). | |
2281 +------------+-----------------------------------------------------+-------+
2282 | ``'a'`` | String (converts any Python object using | \(5) |
2283 | | :func:`ascii`). | |
2284 +------------+-----------------------------------------------------+-------+
2285 | ``'%'`` | No argument is converted, results in a ``'%'`` | |
2286 | | character in the result. | |
2287 +------------+-----------------------------------------------------+-------+
2292 The alternate form causes a leading octal specifier (``'0o'``) to be
2293 inserted before the first digit.
2296 The alternate form causes a leading ``'0x'`` or ``'0X'`` (depending on whether
2297 the ``'x'`` or ``'X'`` format was used) to be inserted before the first digit.
2300 The alternate form causes the result to always contain a decimal point, even if
2301 no digits follow it.
2303 The precision determines the number of digits after the decimal point and
2307 The alternate form causes the result to always contain a decimal point, and
2308 trailing zeroes are not removed as they would otherwise be.
2310 The precision determines the number of significant digits before and after the
2311 decimal point and defaults to 6.
2314 If precision is ``N``, the output is truncated to ``N`` characters.
2319 Since Python strings have an explicit length, ``%s`` conversions do not assume
2320 that ``'\0'`` is the end of the string.
2324 .. versionchanged:: 3.1
2325 ``%f`` conversions for numbers whose absolute value is over 1e50 are no
2326 longer replaced by ``%g`` conversions.
2330 single: buffer protocol; binary sequence types
2334 Binary Sequence Types --- :class:`bytes`, :class:`bytearray`, :class:`memoryview`
2335 =================================================================================
2343 The core built-in types for manipulating binary data are :class:`bytes` and
2344 :class:`bytearray`. They are supported by :class:`memoryview` which uses
2345 the :ref:`buffer protocol <bufferobjects>` to access the memory of other
2346 binary objects without needing to make a copy.
2348 The :mod:`array` module supports efficient storage of basic data types like
2349 32-bit integers and IEEE754 double-precision floating values.
2356 .. index:: object: bytes
2358 Bytes objects are immutable sequences of single bytes. Since many major
2359 binary protocols are based on the ASCII text encoding, bytes objects offer
2360 several methods that are only valid when working with ASCII compatible
2361 data and are closely related to string objects in a variety of other ways.
2363 .. class:: bytes([source[, encoding[, errors]]])
2365 Firstly, the syntax for bytes literals is largely the same as that for string
2366 literals, except that a ``b`` prefix is added:
2368 * Single quotes: ``b'still allows embedded "double" quotes'``
2369 * Double quotes: ``b"still allows embedded 'single' quotes"``.
2370 * Triple quoted: ``b'''3 single quotes'''``, ``b"""3 double quotes"""``
2372 Only ASCII characters are permitted in bytes literals (regardless of the
2373 declared source code encoding). Any binary values over 127 must be entered
2374 into bytes literals using the appropriate escape sequence.
2376 As with string literals, bytes literals may also use a ``r`` prefix to disable
2377 processing of escape sequences. See :ref:`strings` for more about the various
2378 forms of bytes literal, including supported escape sequences.
2380 While bytes literals and representations are based on ASCII text, bytes
2381 objects actually behave like immutable sequences of integers, with each
2382 value in the sequence restricted such that ``0 <= x < 256`` (attempts to
2383 violate this restriction will trigger :exc:`ValueError`). This is done
2384 deliberately to emphasise that while many binary formats include ASCII based
2385 elements and can be usefully manipulated with some text-oriented algorithms,
2386 this is not generally the case for arbitrary binary data (blindly applying
2387 text processing algorithms to binary data formats that are not ASCII
2388 compatible will usually lead to data corruption).
2390 In addition to the literal forms, bytes objects can be created in a number of
2393 * A zero-filled bytes object of a specified length: ``bytes(10)``
2394 * From an iterable of integers: ``bytes(range(20))``
2395 * Copying existing binary data via the buffer protocol: ``bytes(obj)``
2397 Also see the :ref:`bytes <func-bytes>` built-in.
2399 Since 2 hexadecimal digits correspond precisely to a single byte, hexadecimal
2400 numbers are a commonly used format for describing binary data. Accordingly,
2401 the bytes type has an additional class method to read data in that format:
2403 .. classmethod:: fromhex(string)
2405 This :class:`bytes` class method returns a bytes object, decoding the
2406 given string object. The string must contain two hexadecimal digits per
2407 byte, with ASCII whitespace being ignored.
2409 >>> bytes.fromhex('2Ef0 F1f2 ')
2412 .. versionchanged:: 3.7
2413 :meth:`bytes.fromhex` now skips all ASCII whitespace in the string,
2416 A reverse conversion function exists to transform a bytes object into its
2417 hexadecimal representation.
2421 Return a string object containing two hexadecimal digits for each
2422 byte in the instance.
2424 >>> b'\xf0\xf1\xf2'.hex()
2427 If you want to make the hex string easier to read, you can specify a
2428 single character separator *sep* parameter to include in the output.
2429 By default between each byte. A second optional *bytes_per_sep*
2430 parameter controls the spacing. Positive values calculate the
2431 separator position from the right, negative values from the left.
2433 >>> value = b'\xf0\xf1\xf2'
2436 >>> value.hex('_', 2)
2438 >>> b'UUDDLRLRAB'.hex(' ', -4)
2439 '55554444 4c524c52 4142'
2441 .. versionadded:: 3.5
2443 .. versionchanged:: 3.8
2444 :meth:`bytes.hex` now supports optional *sep* and *bytes_per_sep*
2445 parameters to insert separators between bytes in the hex output.
2447 Since bytes objects are sequences of integers (akin to a tuple), for a bytes
2448 object *b*, ``b[0]`` will be an integer, while ``b[0:1]`` will be a bytes
2449 object of length 1. (This contrasts with text strings, where both indexing
2450 and slicing will produce a string of length 1)
2452 The representation of bytes objects uses the literal format (``b'...'``)
2453 since it is often more useful than e.g. ``bytes([46, 46, 46])``. You can
2454 always convert a bytes object into a list of integers using ``list(b)``.
2457 For Python 2.x users: In the Python 2.x series, a variety of implicit
2458 conversions between 8-bit strings (the closest thing 2.x offers to a
2459 built-in binary data type) and Unicode strings were permitted. This was a
2460 backwards compatibility workaround to account for the fact that Python
2461 originally only supported 8-bit text, and Unicode text was a later
2462 addition. In Python 3.x, those implicit conversions are gone - conversions
2463 between 8-bit binary data and Unicode text must be explicit, and bytes and
2464 string objects will always compare unequal.
2472 .. index:: object: bytearray
2474 :class:`bytearray` objects are a mutable counterpart to :class:`bytes`
2477 .. class:: bytearray([source[, encoding[, errors]]])
2479 There is no dedicated literal syntax for bytearray objects, instead
2480 they are always created by calling the constructor:
2482 * Creating an empty instance: ``bytearray()``
2483 * Creating a zero-filled instance with a given length: ``bytearray(10)``
2484 * From an iterable of integers: ``bytearray(range(20))``
2485 * Copying existing binary data via the buffer protocol: ``bytearray(b'Hi!')``
2487 As bytearray objects are mutable, they support the
2488 :ref:`mutable <typesseq-mutable>` sequence operations in addition to the
2489 common bytes and bytearray operations described in :ref:`bytes-methods`.
2491 Also see the :ref:`bytearray <func-bytearray>` built-in.
2493 Since 2 hexadecimal digits correspond precisely to a single byte, hexadecimal
2494 numbers are a commonly used format for describing binary data. Accordingly,
2495 the bytearray type has an additional class method to read data in that format:
2497 .. classmethod:: fromhex(string)
2499 This :class:`bytearray` class method returns bytearray object, decoding
2500 the given string object. The string must contain two hexadecimal digits
2501 per byte, with ASCII whitespace being ignored.
2503 >>> bytearray.fromhex('2Ef0 F1f2 ')
2504 bytearray(b'.\xf0\xf1\xf2')
2506 .. versionchanged:: 3.7
2507 :meth:`bytearray.fromhex` now skips all ASCII whitespace in the string,
2510 A reverse conversion function exists to transform a bytearray object into its
2511 hexadecimal representation.
2515 Return a string object containing two hexadecimal digits for each
2516 byte in the instance.
2518 >>> bytearray(b'\xf0\xf1\xf2').hex()
2521 .. versionadded:: 3.5
2523 Since bytearray objects are sequences of integers (akin to a list), for a
2524 bytearray object *b*, ``b[0]`` will be an integer, while ``b[0:1]`` will be
2525 a bytearray object of length 1. (This contrasts with text strings, where
2526 both indexing and slicing will produce a string of length 1)
2528 The representation of bytearray objects uses the bytes literal format
2529 (``bytearray(b'...')``) since it is often more useful than e.g.
2530 ``bytearray([46, 46, 46])``. You can always convert a bytearray object into
2531 a list of integers using ``list(b)``.
2536 Bytes and Bytearray Operations
2537 ------------------------------
2539 .. index:: pair: bytes; methods
2540 pair: bytearray; methods
2542 Both bytes and bytearray objects support the :ref:`common <typesseq-common>`
2543 sequence operations. They interoperate not just with operands of the same
2544 type, but with any :term:`bytes-like object`. Due to this flexibility, they can be
2545 freely mixed in operations without causing errors. However, the return type
2546 of the result may depend on the order of operands.
2550 The methods on bytes and bytearray objects don't accept strings as their
2551 arguments, just as the methods on strings don't accept bytes as their
2552 arguments. For example, you have to write::
2555 b = a.replace("a", "f")
2560 b = a.replace(b"a", b"f")
2562 Some bytes and bytearray operations assume the use of ASCII compatible
2563 binary formats, and hence should be avoided when working with arbitrary
2564 binary data. These restrictions are covered below.
2567 Using these ASCII based operations to manipulate binary data that is not
2568 stored in an ASCII based format may lead to data corruption.
2570 The following methods on bytes and bytearray objects can be used with
2571 arbitrary binary data.
2573 .. method:: bytes.count(sub[, start[, end]])
2574 bytearray.count(sub[, start[, end]])
2576 Return the number of non-overlapping occurrences of subsequence *sub* in
2577 the range [*start*, *end*]. Optional arguments *start* and *end* are
2578 interpreted as in slice notation.
2580 The subsequence to search for may be any :term:`bytes-like object` or an
2581 integer in the range 0 to 255.
2583 .. versionchanged:: 3.3
2584 Also accept an integer in the range 0 to 255 as the subsequence.
2587 .. method:: bytes.decode(encoding="utf-8", errors="strict")
2588 bytearray.decode(encoding="utf-8", errors="strict")
2590 Return a string decoded from the given bytes. Default encoding is
2591 ``'utf-8'``. *errors* may be given to set a different
2592 error handling scheme. The default for *errors* is ``'strict'``, meaning
2593 that encoding errors raise a :exc:`UnicodeError`. Other possible values are
2594 ``'ignore'``, ``'replace'`` and any other name registered via
2595 :func:`codecs.register_error`, see section :ref:`error-handlers`. For a
2596 list of possible encodings, see section :ref:`standard-encodings`.
2598 By default, the *errors* argument is not checked for best performances, but
2599 only used at the first decoding error. Enable the development mode
2600 (:option:`-X` ``dev`` option), or use a debug build, to check *errors*.
2604 Passing the *encoding* argument to :class:`str` allows decoding any
2605 :term:`bytes-like object` directly, without needing to make a temporary
2606 bytes or bytearray object.
2608 .. versionchanged:: 3.1
2609 Added support for keyword arguments.
2611 .. versionchanged:: 3.9
2612 The *errors* is now checked in development mode and in debug mode.
2615 .. method:: bytes.endswith(suffix[, start[, end]])
2616 bytearray.endswith(suffix[, start[, end]])
2618 Return ``True`` if the binary data ends with the specified *suffix*,
2619 otherwise return ``False``. *suffix* can also be a tuple of suffixes to
2620 look for. With optional *start*, test beginning at that position. With
2621 optional *end*, stop comparing at that position.
2623 The suffix(es) to search for may be any :term:`bytes-like object`.
2626 .. method:: bytes.find(sub[, start[, end]])
2627 bytearray.find(sub[, start[, end]])
2629 Return the lowest index in the data where the subsequence *sub* is found,
2630 such that *sub* is contained in the slice ``s[start:end]``. Optional
2631 arguments *start* and *end* are interpreted as in slice notation. Return
2632 ``-1`` if *sub* is not found.
2634 The subsequence to search for may be any :term:`bytes-like object` or an
2635 integer in the range 0 to 255.
2639 The :meth:`~bytes.find` method should be used only if you need to know the
2640 position of *sub*. To check if *sub* is a substring or not, use the
2641 :keyword:`in` operator::
2643 >>> b'Py' in b'Python'
2646 .. versionchanged:: 3.3
2647 Also accept an integer in the range 0 to 255 as the subsequence.
2650 .. method:: bytes.index(sub[, start[, end]])
2651 bytearray.index(sub[, start[, end]])
2653 Like :meth:`~bytes.find`, but raise :exc:`ValueError` when the
2654 subsequence is not found.
2656 The subsequence to search for may be any :term:`bytes-like object` or an
2657 integer in the range 0 to 255.
2659 .. versionchanged:: 3.3
2660 Also accept an integer in the range 0 to 255 as the subsequence.
2663 .. method:: bytes.join(iterable)
2664 bytearray.join(iterable)
2666 Return a bytes or bytearray object which is the concatenation of the
2667 binary data sequences in *iterable*. A :exc:`TypeError` will be raised
2668 if there are any values in *iterable* that are not :term:`bytes-like
2669 objects <bytes-like object>`, including :class:`str` objects. The
2670 separator between elements is the contents of the bytes or
2671 bytearray object providing this method.
2674 .. staticmethod:: bytes.maketrans(from, to)
2675 bytearray.maketrans(from, to)
2677 This static method returns a translation table usable for
2678 :meth:`bytes.translate` that will map each character in *from* into the
2679 character at the same position in *to*; *from* and *to* must both be
2680 :term:`bytes-like objects <bytes-like object>` and have the same length.
2682 .. versionadded:: 3.1
2685 .. method:: bytes.partition(sep)
2686 bytearray.partition(sep)
2688 Split the sequence at the first occurrence of *sep*, and return a 3-tuple
2689 containing the part before the separator, the separator itself or its
2690 bytearray copy, and the part after the separator.
2691 If the separator is not found, return a 3-tuple
2692 containing a copy of the original sequence, followed by two empty bytes or
2695 The separator to search for may be any :term:`bytes-like object`.
2698 .. method:: bytes.replace(old, new[, count])
2699 bytearray.replace(old, new[, count])
2701 Return a copy of the sequence with all occurrences of subsequence *old*
2702 replaced by *new*. If the optional argument *count* is given, only the
2703 first *count* occurrences are replaced.
2705 The subsequence to search for and its replacement may be any
2706 :term:`bytes-like object`.
2710 The bytearray version of this method does *not* operate in place - it
2711 always produces a new object, even if no changes were made.
2714 .. method:: bytes.rfind(sub[, start[, end]])
2715 bytearray.rfind(sub[, start[, end]])
2717 Return the highest index in the sequence where the subsequence *sub* is
2718 found, such that *sub* is contained within ``s[start:end]``. Optional
2719 arguments *start* and *end* are interpreted as in slice notation. Return
2722 The subsequence to search for may be any :term:`bytes-like object` or an
2723 integer in the range 0 to 255.
2725 .. versionchanged:: 3.3
2726 Also accept an integer in the range 0 to 255 as the subsequence.
2729 .. method:: bytes.rindex(sub[, start[, end]])
2730 bytearray.rindex(sub[, start[, end]])
2732 Like :meth:`~bytes.rfind` but raises :exc:`ValueError` when the
2733 subsequence *sub* is not found.
2735 The subsequence to search for may be any :term:`bytes-like object` or an
2736 integer in the range 0 to 255.
2738 .. versionchanged:: 3.3
2739 Also accept an integer in the range 0 to 255 as the subsequence.
2742 .. method:: bytes.rpartition(sep)
2743 bytearray.rpartition(sep)
2745 Split the sequence at the last occurrence of *sep*, and return a 3-tuple
2746 containing the part before the separator, the separator itself or its
2747 bytearray copy, and the part after the separator.
2748 If the separator is not found, return a 3-tuple
2749 containing two empty bytes or bytearray objects, followed by a copy of the
2752 The separator to search for may be any :term:`bytes-like object`.
2755 .. method:: bytes.startswith(prefix[, start[, end]])
2756 bytearray.startswith(prefix[, start[, end]])
2758 Return ``True`` if the binary data starts with the specified *prefix*,
2759 otherwise return ``False``. *prefix* can also be a tuple of prefixes to
2760 look for. With optional *start*, test beginning at that position. With
2761 optional *end*, stop comparing at that position.
2763 The prefix(es) to search for may be any :term:`bytes-like object`.
2766 .. method:: bytes.translate(table, /, delete=b'')
2767 bytearray.translate(table, /, delete=b'')
2769 Return a copy of the bytes or bytearray object where all bytes occurring in
2770 the optional argument *delete* are removed, and the remaining bytes have
2771 been mapped through the given translation table, which must be a bytes
2772 object of length 256.
2774 You can use the :func:`bytes.maketrans` method to create a translation
2777 Set the *table* argument to ``None`` for translations that only delete
2780 >>> b'read this short text'.translate(None, b'aeiou')
2783 .. versionchanged:: 3.6
2784 *delete* is now supported as a keyword argument.
2787 The following methods on bytes and bytearray objects have default behaviours
2788 that assume the use of ASCII compatible binary formats, but can still be used
2789 with arbitrary binary data by passing appropriate arguments. Note that all of
2790 the bytearray methods in this section do *not* operate in place, and instead
2791 produce new objects.
2793 .. method:: bytes.center(width[, fillbyte])
2794 bytearray.center(width[, fillbyte])
2796 Return a copy of the object centered in a sequence of length *width*.
2797 Padding is done using the specified *fillbyte* (default is an ASCII
2798 space). For :class:`bytes` objects, the original sequence is returned if
2799 *width* is less than or equal to ``len(s)``.
2803 The bytearray version of this method does *not* operate in place -
2804 it always produces a new object, even if no changes were made.
2807 .. method:: bytes.ljust(width[, fillbyte])
2808 bytearray.ljust(width[, fillbyte])
2810 Return a copy of the object left justified in a sequence of length *width*.
2811 Padding is done using the specified *fillbyte* (default is an ASCII
2812 space). For :class:`bytes` objects, the original sequence is returned if
2813 *width* is less than or equal to ``len(s)``.
2817 The bytearray version of this method does *not* operate in place -
2818 it always produces a new object, even if no changes were made.
2821 .. method:: bytes.lstrip([chars])
2822 bytearray.lstrip([chars])
2824 Return a copy of the sequence with specified leading bytes removed. The
2825 *chars* argument is a binary sequence specifying the set of byte values to
2826 be removed - the name refers to the fact this method is usually used with
2827 ASCII characters. If omitted or ``None``, the *chars* argument defaults
2828 to removing ASCII whitespace. The *chars* argument is not a prefix;
2829 rather, all combinations of its values are stripped::
2831 >>> b' spacious '.lstrip()
2833 >>> b'www.example.com'.lstrip(b'cmowz.')
2836 The binary sequence of byte values to remove may be any
2837 :term:`bytes-like object`.
2841 The bytearray version of this method does *not* operate in place -
2842 it always produces a new object, even if no changes were made.
2845 .. method:: bytes.rjust(width[, fillbyte])
2846 bytearray.rjust(width[, fillbyte])
2848 Return a copy of the object right justified in a sequence of length *width*.
2849 Padding is done using the specified *fillbyte* (default is an ASCII
2850 space). For :class:`bytes` objects, the original sequence is returned if
2851 *width* is less than or equal to ``len(s)``.
2855 The bytearray version of this method does *not* operate in place -
2856 it always produces a new object, even if no changes were made.
2859 .. method:: bytes.rsplit(sep=None, maxsplit=-1)
2860 bytearray.rsplit(sep=None, maxsplit=-1)
2862 Split the binary sequence into subsequences of the same type, using *sep*
2863 as the delimiter string. If *maxsplit* is given, at most *maxsplit* splits
2864 are done, the *rightmost* ones. If *sep* is not specified or ``None``,
2865 any subsequence consisting solely of ASCII whitespace is a separator.
2866 Except for splitting from the right, :meth:`rsplit` behaves like
2867 :meth:`split` which is described in detail below.
2870 .. method:: bytes.rstrip([chars])
2871 bytearray.rstrip([chars])
2873 Return a copy of the sequence with specified trailing bytes removed. The
2874 *chars* argument is a binary sequence specifying the set of byte values to
2875 be removed - the name refers to the fact this method is usually used with
2876 ASCII characters. If omitted or ``None``, the *chars* argument defaults to
2877 removing ASCII whitespace. The *chars* argument is not a suffix; rather,
2878 all combinations of its values are stripped::
2880 >>> b' spacious '.rstrip()
2882 >>> b'mississippi'.rstrip(b'ipz')
2885 The binary sequence of byte values to remove may be any
2886 :term:`bytes-like object`.
2890 The bytearray version of this method does *not* operate in place -
2891 it always produces a new object, even if no changes were made.
2894 .. method:: bytes.split(sep=None, maxsplit=-1)
2895 bytearray.split(sep=None, maxsplit=-1)
2897 Split the binary sequence into subsequences of the same type, using *sep*
2898 as the delimiter string. If *maxsplit* is given and non-negative, at most
2899 *maxsplit* splits are done (thus, the list will have at most ``maxsplit+1``
2900 elements). If *maxsplit* is not specified or is ``-1``, then there is no
2901 limit on the number of splits (all possible splits are made).
2903 If *sep* is given, consecutive delimiters are not grouped together and are
2904 deemed to delimit empty subsequences (for example, ``b'1,,2'.split(b',')``
2905 returns ``[b'1', b'', b'2']``). The *sep* argument may consist of a
2906 multibyte sequence (for example, ``b'1<>2<>3'.split(b'<>')`` returns
2907 ``[b'1', b'2', b'3']``). Splitting an empty sequence with a specified
2908 separator returns ``[b'']`` or ``[bytearray(b'')]`` depending on the type
2909 of object being split. The *sep* argument may be any
2910 :term:`bytes-like object`.
2914 >>> b'1,2,3'.split(b',')
2916 >>> b'1,2,3'.split(b',', maxsplit=1)
2918 >>> b'1,2,,3,'.split(b',')
2919 [b'1', b'2', b'', b'3', b'']
2921 If *sep* is not specified or is ``None``, a different splitting algorithm
2922 is applied: runs of consecutive ASCII whitespace are regarded as a single
2923 separator, and the result will contain no empty strings at the start or
2924 end if the sequence has leading or trailing whitespace. Consequently,
2925 splitting an empty sequence or a sequence consisting solely of ASCII
2926 whitespace without a specified separator returns ``[]``.
2931 >>> b'1 2 3'.split()
2933 >>> b'1 2 3'.split(maxsplit=1)
2935 >>> b' 1 2 3 '.split()
2939 .. method:: bytes.strip([chars])
2940 bytearray.strip([chars])
2942 Return a copy of the sequence with specified leading and trailing bytes
2943 removed. The *chars* argument is a binary sequence specifying the set of
2944 byte values to be removed - the name refers to the fact this method is
2945 usually used with ASCII characters. If omitted or ``None``, the *chars*
2946 argument defaults to removing ASCII whitespace. The *chars* argument is
2947 not a prefix or suffix; rather, all combinations of its values are
2950 >>> b' spacious '.strip()
2952 >>> b'www.example.com'.strip(b'cmowz.')
2955 The binary sequence of byte values to remove may be any
2956 :term:`bytes-like object`.
2960 The bytearray version of this method does *not* operate in place -
2961 it always produces a new object, even if no changes were made.
2964 The following methods on bytes and bytearray objects assume the use of ASCII
2965 compatible binary formats and should not be applied to arbitrary binary data.
2966 Note that all of the bytearray methods in this section do *not* operate in
2967 place, and instead produce new objects.
2969 .. method:: bytes.capitalize()
2970 bytearray.capitalize()
2972 Return a copy of the sequence with each byte interpreted as an ASCII
2973 character, and the first byte capitalized and the rest lowercased.
2974 Non-ASCII byte values are passed through unchanged.
2978 The bytearray version of this method does *not* operate in place - it
2979 always produces a new object, even if no changes were made.
2982 .. method:: bytes.expandtabs(tabsize=8)
2983 bytearray.expandtabs(tabsize=8)
2985 Return a copy of the sequence where all ASCII tab characters are replaced
2986 by one or more ASCII spaces, depending on the current column and the given
2987 tab size. Tab positions occur every *tabsize* bytes (default is 8,
2988 giving tab positions at columns 0, 8, 16 and so on). To expand the
2989 sequence, the current column is set to zero and the sequence is examined
2990 byte by byte. If the byte is an ASCII tab character (``b'\t'``), one or
2991 more space characters are inserted in the result until the current column
2992 is equal to the next tab position. (The tab character itself is not
2993 copied.) If the current byte is an ASCII newline (``b'\n'``) or
2994 carriage return (``b'\r'``), it is copied and the current column is reset
2995 to zero. Any other byte value is copied unchanged and the current column
2996 is incremented by one regardless of how the byte value is represented when
2999 >>> b'01\t012\t0123\t01234'.expandtabs()
3000 b'01 012 0123 01234'
3001 >>> b'01\t012\t0123\t01234'.expandtabs(4)
3002 b'01 012 0123 01234'
3006 The bytearray version of this method does *not* operate in place - it
3007 always produces a new object, even if no changes were made.
3010 .. method:: bytes.isalnum()
3013 Return ``True`` if all bytes in the sequence are alphabetical ASCII characters
3014 or ASCII decimal digits and the sequence is not empty, ``False`` otherwise.
3015 Alphabetic ASCII characters are those byte values in the sequence
3016 ``b'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'``. ASCII decimal
3017 digits are those byte values in the sequence ``b'0123456789'``.
3021 >>> b'ABCabc1'.isalnum()
3023 >>> b'ABC abc1'.isalnum()
3027 .. method:: bytes.isalpha()
3030 Return ``True`` if all bytes in the sequence are alphabetic ASCII characters
3031 and the sequence is not empty, ``False`` otherwise. Alphabetic ASCII
3032 characters are those byte values in the sequence
3033 ``b'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'``.
3037 >>> b'ABCabc'.isalpha()
3039 >>> b'ABCabc1'.isalpha()
3043 .. method:: bytes.isascii()
3046 Return ``True`` if the sequence is empty or all bytes in the sequence are ASCII,
3047 ``False`` otherwise.
3048 ASCII bytes are in the range 0-0x7F.
3050 .. versionadded:: 3.7
3053 .. method:: bytes.isdigit()
3056 Return ``True`` if all bytes in the sequence are ASCII decimal digits
3057 and the sequence is not empty, ``False`` otherwise. ASCII decimal digits are
3058 those byte values in the sequence ``b'0123456789'``.
3062 >>> b'1234'.isdigit()
3064 >>> b'1.23'.isdigit()
3068 .. method:: bytes.islower()
3071 Return ``True`` if there is at least one lowercase ASCII character
3072 in the sequence and no uppercase ASCII characters, ``False`` otherwise.
3076 >>> b'hello world'.islower()
3078 >>> b'Hello world'.islower()
3081 Lowercase ASCII characters are those byte values in the sequence
3082 ``b'abcdefghijklmnopqrstuvwxyz'``. Uppercase ASCII characters
3083 are those byte values in the sequence ``b'ABCDEFGHIJKLMNOPQRSTUVWXYZ'``.
3086 .. method:: bytes.isspace()
3089 Return ``True`` if all bytes in the sequence are ASCII whitespace and the
3090 sequence is not empty, ``False`` otherwise. ASCII whitespace characters are
3091 those byte values in the sequence ``b' \t\n\r\x0b\f'`` (space, tab, newline,
3092 carriage return, vertical tab, form feed).
3095 .. method:: bytes.istitle()
3098 Return ``True`` if the sequence is ASCII titlecase and the sequence is not
3099 empty, ``False`` otherwise. See :meth:`bytes.title` for more details on the
3100 definition of "titlecase".
3104 >>> b'Hello World'.istitle()
3106 >>> b'Hello world'.istitle()
3110 .. method:: bytes.isupper()
3113 Return ``True`` if there is at least one uppercase alphabetic ASCII character
3114 in the sequence and no lowercase ASCII characters, ``False`` otherwise.
3118 >>> b'HELLO WORLD'.isupper()
3120 >>> b'Hello world'.isupper()
3123 Lowercase ASCII characters are those byte values in the sequence
3124 ``b'abcdefghijklmnopqrstuvwxyz'``. Uppercase ASCII characters
3125 are those byte values in the sequence ``b'ABCDEFGHIJKLMNOPQRSTUVWXYZ'``.
3128 .. method:: bytes.lower()
3131 Return a copy of the sequence with all the uppercase ASCII characters
3132 converted to their corresponding lowercase counterpart.
3136 >>> b'Hello World'.lower()
3139 Lowercase ASCII characters are those byte values in the sequence
3140 ``b'abcdefghijklmnopqrstuvwxyz'``. Uppercase ASCII characters
3141 are those byte values in the sequence ``b'ABCDEFGHIJKLMNOPQRSTUVWXYZ'``.
3145 The bytearray version of this method does *not* operate in place - it
3146 always produces a new object, even if no changes were made.
3150 single: universal newlines; bytes.splitlines method
3151 single: universal newlines; bytearray.splitlines method
3153 .. method:: bytes.splitlines(keepends=False)
3154 bytearray.splitlines(keepends=False)
3156 Return a list of the lines in the binary sequence, breaking at ASCII
3157 line boundaries. This method uses the :term:`universal newlines` approach
3158 to splitting lines. Line breaks are not included in the resulting list
3159 unless *keepends* is given and true.
3163 >>> b'ab c\n\nde fg\rkl\r\n'.splitlines()
3164 [b'ab c', b'', b'de fg', b'kl']
3165 >>> b'ab c\n\nde fg\rkl\r\n'.splitlines(keepends=True)
3166 [b'ab c\n', b'\n', b'de fg\r', b'kl\r\n']
3168 Unlike :meth:`~bytes.split` when a delimiter string *sep* is given, this
3169 method returns an empty list for the empty string, and a terminal line
3170 break does not result in an extra line::
3172 >>> b"".split(b'\n'), b"Two lines\n".split(b'\n')
3173 ([b''], [b'Two lines', b''])
3174 >>> b"".splitlines(), b"One line\n".splitlines()
3178 .. method:: bytes.swapcase()
3179 bytearray.swapcase()
3181 Return a copy of the sequence with all the lowercase ASCII characters
3182 converted to their corresponding uppercase counterpart and vice-versa.
3186 >>> b'Hello World'.swapcase()
3189 Lowercase ASCII characters are those byte values in the sequence
3190 ``b'abcdefghijklmnopqrstuvwxyz'``. Uppercase ASCII characters
3191 are those byte values in the sequence ``b'ABCDEFGHIJKLMNOPQRSTUVWXYZ'``.
3193 Unlike :func:`str.swapcase()`, it is always the case that
3194 ``bin.swapcase().swapcase() == bin`` for the binary versions. Case
3195 conversions are symmetrical in ASCII, even though that is not generally
3196 true for arbitrary Unicode code points.
3200 The bytearray version of this method does *not* operate in place - it
3201 always produces a new object, even if no changes were made.
3204 .. method:: bytes.title()
3207 Return a titlecased version of the binary sequence where words start with
3208 an uppercase ASCII character and the remaining characters are lowercase.
3209 Uncased byte values are left unmodified.
3213 >>> b'Hello world'.title()
3216 Lowercase ASCII characters are those byte values in the sequence
3217 ``b'abcdefghijklmnopqrstuvwxyz'``. Uppercase ASCII characters
3218 are those byte values in the sequence ``b'ABCDEFGHIJKLMNOPQRSTUVWXYZ'``.
3219 All other byte values are uncased.
3221 The algorithm uses a simple language-independent definition of a word as
3222 groups of consecutive letters. The definition works in many contexts but
3223 it means that apostrophes in contractions and possessives form word
3224 boundaries, which may not be the desired result::
3226 >>> b"they're bill's friends from the UK".title()
3227 b"They'Re Bill'S Friends From The Uk"
3229 A workaround for apostrophes can be constructed using regular expressions::
3232 >>> def titlecase(s):
3233 ... return re.sub(rb"[A-Za-z]+('[A-Za-z]+)?",
3234 ... lambda mo: mo.group(0)[0:1].upper() +
3235 ... mo.group(0)[1:].lower(),
3238 >>> titlecase(b"they're bill's friends.")
3239 b"They're Bill's Friends."
3243 The bytearray version of this method does *not* operate in place - it
3244 always produces a new object, even if no changes were made.
3247 .. method:: bytes.upper()
3250 Return a copy of the sequence with all the lowercase ASCII characters
3251 converted to their corresponding uppercase counterpart.
3255 >>> b'Hello World'.upper()
3258 Lowercase ASCII characters are those byte values in the sequence
3259 ``b'abcdefghijklmnopqrstuvwxyz'``. Uppercase ASCII characters
3260 are those byte values in the sequence ``b'ABCDEFGHIJKLMNOPQRSTUVWXYZ'``.
3264 The bytearray version of this method does *not* operate in place - it
3265 always produces a new object, even if no changes were made.
3268 .. method:: bytes.zfill(width)
3269 bytearray.zfill(width)
3271 Return a copy of the sequence left filled with ASCII ``b'0'`` digits to
3272 make a sequence of length *width*. A leading sign prefix (``b'+'``/
3273 ``b'-'``) is handled by inserting the padding *after* the sign character
3274 rather than before. For :class:`bytes` objects, the original sequence is
3275 returned if *width* is less than or equal to ``len(seq)``.
3286 The bytearray version of this method does *not* operate in place - it
3287 always produces a new object, even if no changes were made.
3290 .. _bytes-formatting:
3292 ``printf``-style Bytes Formatting
3293 ----------------------------------
3296 single: formatting; bytes (%)
3297 single: formatting; bytearray (%)
3298 single: interpolation; bytes (%)
3299 single: interpolation; bytearray (%)
3300 single: bytes; formatting
3301 single: bytearray; formatting
3302 single: bytes; interpolation
3303 single: bytearray; interpolation
3304 single: printf-style formatting
3305 single: sprintf-style formatting
3306 single: % (percent); printf-style formatting
3310 The formatting operations described here exhibit a variety of quirks that
3311 lead to a number of common errors (such as failing to display tuples and
3312 dictionaries correctly). If the value being printed may be a tuple or
3313 dictionary, wrap it in a tuple.
3315 Bytes objects (``bytes``/``bytearray``) have one unique built-in operation:
3316 the ``%`` operator (modulo).
3317 This is also known as the bytes *formatting* or *interpolation* operator.
3318 Given ``format % values`` (where *format* is a bytes object), ``%`` conversion
3319 specifications in *format* are replaced with zero or more elements of *values*.
3320 The effect is similar to using the :c:func:`sprintf` in the C language.
3322 If *format* requires a single argument, *values* may be a single non-tuple
3323 object. [5]_ Otherwise, *values* must be a tuple with exactly the number of
3324 items specified by the format bytes object, or a single mapping object (for
3325 example, a dictionary).
3328 single: () (parentheses); in printf-style formatting
3329 single: * (asterisk); in printf-style formatting
3330 single: . (dot); in printf-style formatting
3332 A conversion specifier contains two or more characters and has the following
3333 components, which must occur in this order:
3335 #. The ``'%'`` character, which marks the start of the specifier.
3337 #. Mapping key (optional), consisting of a parenthesised sequence of characters
3338 (for example, ``(somename)``).
3340 #. Conversion flags (optional), which affect the result of some conversion
3343 #. Minimum field width (optional). If specified as an ``'*'`` (asterisk), the
3344 actual width is read from the next element of the tuple in *values*, and the
3345 object to convert comes after the minimum field width and optional precision.
3347 #. Precision (optional), given as a ``'.'`` (dot) followed by the precision. If
3348 specified as ``'*'`` (an asterisk), the actual precision is read from the next
3349 element of the tuple in *values*, and the value to convert comes after the
3352 #. Length modifier (optional).
3356 When the right argument is a dictionary (or other mapping type), then the
3357 formats in the bytes object *must* include a parenthesised mapping key into that
3358 dictionary inserted immediately after the ``'%'`` character. The mapping key
3359 selects the value to be formatted from the mapping. For example:
3361 >>> print(b'%(language)s has %(number)03d quote types.' %
3362 ... {b'language': b"Python", b"number": 2})
3363 b'Python has 002 quote types.'
3365 In this case no ``*`` specifiers may occur in a format (since they require a
3366 sequential parameter list).
3368 The conversion flag characters are:
3371 single: # (hash); in printf-style formatting
3372 single: - (minus); in printf-style formatting
3373 single: + (plus); in printf-style formatting
3374 single: space; in printf-style formatting
3376 +---------+---------------------------------------------------------------------+
3378 +=========+=====================================================================+
3379 | ``'#'`` | The value conversion will use the "alternate form" (where defined |
3381 +---------+---------------------------------------------------------------------+
3382 | ``'0'`` | The conversion will be zero padded for numeric values. |
3383 +---------+---------------------------------------------------------------------+
3384 | ``'-'`` | The converted value is left adjusted (overrides the ``'0'`` |
3385 | | conversion if both are given). |
3386 +---------+---------------------------------------------------------------------+
3387 | ``' '`` | (a space) A blank should be left before a positive number (or empty |
3388 | | string) produced by a signed conversion. |
3389 +---------+---------------------------------------------------------------------+
3390 | ``'+'`` | A sign character (``'+'`` or ``'-'``) will precede the conversion |
3391 | | (overrides a "space" flag). |
3392 +---------+---------------------------------------------------------------------+
3394 A length modifier (``h``, ``l``, or ``L``) may be present, but is ignored as it
3395 is not necessary for Python -- so e.g. ``%ld`` is identical to ``%d``.
3397 The conversion types are:
3399 +------------+-----------------------------------------------------+-------+
3400 | Conversion | Meaning | Notes |
3401 +============+=====================================================+=======+
3402 | ``'d'`` | Signed integer decimal. | |
3403 +------------+-----------------------------------------------------+-------+
3404 | ``'i'`` | Signed integer decimal. | |
3405 +------------+-----------------------------------------------------+-------+
3406 | ``'o'`` | Signed octal value. | \(1) |
3407 +------------+-----------------------------------------------------+-------+
3408 | ``'u'`` | Obsolete type -- it is identical to ``'d'``. | \(8) |
3409 +------------+-----------------------------------------------------+-------+
3410 | ``'x'`` | Signed hexadecimal (lowercase). | \(2) |
3411 +------------+-----------------------------------------------------+-------+
3412 | ``'X'`` | Signed hexadecimal (uppercase). | \(2) |
3413 +------------+-----------------------------------------------------+-------+
3414 | ``'e'`` | Floating point exponential format (lowercase). | \(3) |
3415 +------------+-----------------------------------------------------+-------+
3416 | ``'E'`` | Floating point exponential format (uppercase). | \(3) |
3417 +------------+-----------------------------------------------------+-------+
3418 | ``'f'`` | Floating point decimal format. | \(3) |
3419 +------------+-----------------------------------------------------+-------+
3420 | ``'F'`` | Floating point decimal format. | \(3) |
3421 +------------+-----------------------------------------------------+-------+
3422 | ``'g'`` | Floating point format. Uses lowercase exponential | \(4) |
3423 | | format if exponent is less than -4 or not less than | |
3424 | | precision, decimal format otherwise. | |
3425 +------------+-----------------------------------------------------+-------+
3426 | ``'G'`` | Floating point format. Uses uppercase exponential | \(4) |
3427 | | format if exponent is less than -4 or not less than | |
3428 | | precision, decimal format otherwise. | |
3429 +------------+-----------------------------------------------------+-------+
3430 | ``'c'`` | Single byte (accepts integer or single | |
3431 | | byte objects). | |
3432 +------------+-----------------------------------------------------+-------+
3433 | ``'b'`` | Bytes (any object that follows the | \(5) |
3434 | | :ref:`buffer protocol <bufferobjects>` or has | |
3435 | | :meth:`__bytes__`). | |
3436 +------------+-----------------------------------------------------+-------+
3437 | ``'s'`` | ``'s'`` is an alias for ``'b'`` and should only | \(6) |
3438 | | be used for Python2/3 code bases. | |
3439 +------------+-----------------------------------------------------+-------+
3440 | ``'a'`` | Bytes (converts any Python object using | \(5) |
3441 | | ``repr(obj).encode('ascii','backslashreplace)``). | |
3442 +------------+-----------------------------------------------------+-------+
3443 | ``'r'`` | ``'r'`` is an alias for ``'a'`` and should only | \(7) |
3444 | | be used for Python2/3 code bases. | |
3445 +------------+-----------------------------------------------------+-------+
3446 | ``'%'`` | No argument is converted, results in a ``'%'`` | |
3447 | | character in the result. | |
3448 +------------+-----------------------------------------------------+-------+
3453 The alternate form causes a leading octal specifier (``'0o'``) to be
3454 inserted before the first digit.
3457 The alternate form causes a leading ``'0x'`` or ``'0X'`` (depending on whether
3458 the ``'x'`` or ``'X'`` format was used) to be inserted before the first digit.
3461 The alternate form causes the result to always contain a decimal point, even if
3462 no digits follow it.
3464 The precision determines the number of digits after the decimal point and
3468 The alternate form causes the result to always contain a decimal point, and
3469 trailing zeroes are not removed as they would otherwise be.
3471 The precision determines the number of significant digits before and after the
3472 decimal point and defaults to 6.
3475 If precision is ``N``, the output is truncated to ``N`` characters.
3478 ``b'%s'`` is deprecated, but will not be removed during the 3.x series.
3481 ``b'%r'`` is deprecated, but will not be removed during the 3.x series.
3488 The bytearray version of this method does *not* operate in place - it
3489 always produces a new object, even if no changes were made.
3493 :pep:`461` - Adding % formatting to bytes and bytearray
3495 .. versionadded:: 3.5
3502 :class:`memoryview` objects allow Python code to access the internal data
3503 of an object that supports the :ref:`buffer protocol <bufferobjects>` without
3506 .. class:: memoryview(obj)
3508 Create a :class:`memoryview` that references *obj*. *obj* must support the
3509 buffer protocol. Built-in objects that support the buffer protocol include
3510 :class:`bytes` and :class:`bytearray`.
3512 A :class:`memoryview` has the notion of an *element*, which is the
3513 atomic memory unit handled by the originating object *obj*. For many
3514 simple types such as :class:`bytes` and :class:`bytearray`, an element
3515 is a single byte, but other types such as :class:`array.array` may have
3518 ``len(view)`` is equal to the length of :class:`~memoryview.tolist`.
3519 If ``view.ndim = 0``, the length is 1. If ``view.ndim = 1``, the length
3520 is equal to the number of elements in the view. For higher dimensions,
3521 the length is equal to the length of the nested list representation of
3522 the view. The :class:`~memoryview.itemsize` attribute will give you the
3523 number of bytes in a single element.
3525 A :class:`memoryview` supports slicing and indexing to expose its data.
3526 One-dimensional slicing will result in a subview::
3528 >>> v = memoryview(b'abcefg')
3534 <memory at 0x7f3ddc9f4350>
3538 If :class:`~memoryview.format` is one of the native format specifiers
3539 from the :mod:`struct` module, indexing with an integer or a tuple of
3540 integers is also supported and returns a single *element* with
3541 the correct type. One-dimensional memoryviews can be indexed
3542 with an integer or a one-integer tuple. Multi-dimensional memoryviews
3543 can be indexed with tuples of exactly *ndim* integers where *ndim* is
3544 the number of dimensions. Zero-dimensional memoryviews can be indexed
3545 with the empty tuple.
3547 Here is an example with a non-byte format::
3550 >>> a = array.array('l', [-11111111, 22222222, -33333333, 44444444])
3551 >>> m = memoryview(a)
3557 [-11111111, -33333333]
3559 If the underlying object is writable, the memoryview supports
3560 one-dimensional slice assignment. Resizing is not allowed::
3562 >>> data = bytearray(b'abcefg')
3563 >>> v = memoryview(data)
3566 >>> v[0] = ord(b'z')
3568 bytearray(b'zbcefg')
3571 bytearray(b'z123fg')
3572 >>> v[2:3] = b'spam'
3573 Traceback (most recent call last):
3574 File "<stdin>", line 1, in <module>
3575 ValueError: memoryview assignment: lvalue and rvalue have different structures
3576 >>> v[2:6] = b'spam'
3578 bytearray(b'z1spam')
3580 One-dimensional memoryviews of hashable (read-only) types with formats
3581 'B', 'b' or 'c' are also hashable. The hash is defined as
3582 ``hash(m) == hash(m.tobytes())``::
3584 >>> v = memoryview(b'abcefg')
3585 >>> hash(v) == hash(b'abcefg')
3587 >>> hash(v[2:4]) == hash(b'ce')
3589 >>> hash(v[::-2]) == hash(b'abcefg'[::-2])
3592 .. versionchanged:: 3.3
3593 One-dimensional memoryviews can now be sliced.
3594 One-dimensional memoryviews with formats 'B', 'b' or 'c' are now hashable.
3596 .. versionchanged:: 3.4
3597 memoryview is now registered automatically with
3598 :class:`collections.abc.Sequence`
3600 .. versionchanged:: 3.5
3601 memoryviews can now be indexed with tuple of integers.
3603 :class:`memoryview` has several methods:
3605 .. method:: __eq__(exporter)
3607 A memoryview and a :pep:`3118` exporter are equal if their shapes are
3608 equivalent and if all corresponding values are equal when the operands'
3609 respective format codes are interpreted using :mod:`struct` syntax.
3611 For the subset of :mod:`struct` format strings currently supported by
3612 :meth:`tolist`, ``v`` and ``w`` are equal if ``v.tolist() == w.tolist()``::
3615 >>> a = array.array('I', [1, 2, 3, 4, 5])
3616 >>> b = array.array('d', [1.0, 2.0, 3.0, 4.0, 5.0])
3617 >>> c = array.array('b', [5, 3, 1])
3618 >>> x = memoryview(a)
3619 >>> y = memoryview(b)
3620 >>> x == a == y == b
3622 >>> x.tolist() == a.tolist() == y.tolist() == b.tolist()
3627 >>> z.tolist() == c.tolist()
3630 If either format string is not supported by the :mod:`struct` module,
3631 then the objects will always compare as unequal (even if the format
3632 strings and buffer contents are identical)::
3634 >>> from ctypes import BigEndianStructure, c_long
3635 >>> class BEPoint(BigEndianStructure):
3636 ... _fields_ = [("x", c_long), ("y", c_long)]
3638 >>> point = BEPoint(100, 200)
3639 >>> a = memoryview(point)
3640 >>> b = memoryview(point)
3646 Note that, as with floating point numbers, ``v is w`` does *not* imply
3647 ``v == w`` for memoryview objects.
3649 .. versionchanged:: 3.3
3650 Previous versions compared the raw memory disregarding the item format
3651 and the logical array structure.
3653 .. method:: tobytes(order=None)
3655 Return the data in the buffer as a bytestring. This is equivalent to
3656 calling the :class:`bytes` constructor on the memoryview. ::
3658 >>> m = memoryview(b"abc")
3664 For non-contiguous arrays the result is equal to the flattened list
3665 representation with all elements converted to bytes. :meth:`tobytes`
3666 supports all format strings, including those that are not in
3667 :mod:`struct` module syntax.
3669 .. versionadded:: 3.8
3670 *order* can be {'C', 'F', 'A'}. When *order* is 'C' or 'F', the data
3671 of the original array is converted to C or Fortran order. For contiguous
3672 views, 'A' returns an exact copy of the physical memory. In particular,
3673 in-memory Fortran order is preserved. For non-contiguous views, the
3674 data is converted to C first. *order=None* is the same as *order='C'*.
3678 Return a string object containing two hexadecimal digits for each
3679 byte in the buffer. ::
3681 >>> m = memoryview(b"abc")
3685 .. versionadded:: 3.5
3687 .. method:: tolist()
3689 Return the data in the buffer as a list of elements. ::
3691 >>> memoryview(b'abc').tolist()
3694 >>> a = array.array('d', [1.1, 2.2, 3.3])
3695 >>> m = memoryview(a)
3699 .. versionchanged:: 3.3
3700 :meth:`tolist` now supports all single character native formats in
3701 :mod:`struct` module syntax as well as multi-dimensional
3704 .. method:: toreadonly()
3706 Return a readonly version of the memoryview object. The original
3707 memoryview object is unchanged. ::
3709 >>> m = memoryview(bytearray(b'abc'))
3710 >>> mm = m.toreadonly()
3714 Traceback (most recent call last):
3715 File "<stdin>", line 1, in <module>
3716 TypeError: cannot modify read-only memory
3721 .. versionadded:: 3.8
3723 .. method:: release()
3725 Release the underlying buffer exposed by the memoryview object. Many
3726 objects take special actions when a view is held on them (for example,
3727 a :class:`bytearray` would temporarily forbid resizing); therefore,
3728 calling release() is handy to remove these restrictions (and free any
3729 dangling resources) as soon as possible.
3731 After this method has been called, any further operation on the view
3732 raises a :class:`ValueError` (except :meth:`release()` itself which can
3733 be called multiple times)::
3735 >>> m = memoryview(b'abc')
3738 Traceback (most recent call last):
3739 File "<stdin>", line 1, in <module>
3740 ValueError: operation forbidden on released memoryview object
3742 The context management protocol can be used for a similar effect,
3743 using the ``with`` statement::
3745 >>> with memoryview(b'abc') as m:
3750 Traceback (most recent call last):
3751 File "<stdin>", line 1, in <module>
3752 ValueError: operation forbidden on released memoryview object
3754 .. versionadded:: 3.2
3756 .. method:: cast(format[, shape])
3758 Cast a memoryview to a new format or shape. *shape* defaults to
3759 ``[byte_length//new_itemsize]``, which means that the result view
3760 will be one-dimensional. The return value is a new memoryview, but
3761 the buffer itself is not copied. Supported casts are 1D -> C-:term:`contiguous`
3762 and C-contiguous -> 1D.
3764 The destination format is restricted to a single element native format in
3765 :mod:`struct` syntax. One of the formats must be a byte format
3766 ('B', 'b' or 'c'). The byte length of the result must be the same
3767 as the original length.
3769 Cast 1D/long to 1D/unsigned bytes::
3772 >>> a = array.array('l', [1,2,3])
3773 >>> x = memoryview(a)
3792 Cast 1D/unsigned bytes to 1D/char::
3794 >>> b = bytearray(b'zyz')
3795 >>> x = memoryview(b)
3797 Traceback (most recent call last):
3798 File "<stdin>", line 1, in <module>
3799 ValueError: memoryview: invalid value for format "B"
3805 Cast 1D/bytes to 3D/ints to 1D/signed char::
3808 >>> buf = struct.pack("i"*12, *list(range(12)))
3809 >>> x = memoryview(buf)
3810 >>> y = x.cast('i', shape=[2,2,3])
3812 [[[0, 1, 2], [3, 4, 5]], [[6, 7, 8], [9, 10, 11]]]
3831 Cast 1D/unsigned long to 2D/unsigned long::
3833 >>> buf = struct.pack("L"*6, *list(range(6)))
3834 >>> x = memoryview(buf)
3835 >>> y = x.cast('L', shape=[2,3])
3841 [[0, 1, 2], [3, 4, 5]]
3843 .. versionadded:: 3.3
3845 .. versionchanged:: 3.5
3846 The source format is no longer restricted when casting to a byte view.
3848 There are also several readonly attributes available:
3852 The underlying object of the memoryview::
3854 >>> b = bytearray(b'xyz')
3855 >>> m = memoryview(b)
3859 .. versionadded:: 3.3
3861 .. attribute:: nbytes
3863 ``nbytes == product(shape) * itemsize == len(m.tobytes())``. This is
3864 the amount of space in bytes that the array would use in a contiguous
3865 representation. It is not necessarily equal to ``len(m)``::
3868 >>> a = array.array('i', [1,2,3,4,5])
3869 >>> m = memoryview(a)
3879 >>> len(y.tobytes())
3882 Multi-dimensional arrays::
3885 >>> buf = struct.pack("d"*12, *[1.5*x for x in range(12)])
3886 >>> x = memoryview(buf)
3887 >>> y = x.cast('d', shape=[3,4])
3889 [[0.0, 1.5, 3.0, 4.5], [6.0, 7.5, 9.0, 10.5], [12.0, 13.5, 15.0, 16.5]]
3895 .. versionadded:: 3.3
3897 .. attribute:: readonly
3899 A bool indicating whether the memory is read only.
3901 .. attribute:: format
3903 A string containing the format (in :mod:`struct` module style) for each
3904 element in the view. A memoryview can be created from exporters with
3905 arbitrary format strings, but some methods (e.g. :meth:`tolist`) are
3906 restricted to native single element formats.
3908 .. versionchanged:: 3.3
3909 format ``'B'`` is now handled according to the struct module syntax.
3910 This means that ``memoryview(b'abc')[0] == b'abc'[0] == 97``.
3912 .. attribute:: itemsize
3914 The size in bytes of each element of the memoryview::
3916 >>> import array, struct
3917 >>> m = memoryview(array.array('H', [32000, 32001, 32002]))
3922 >>> struct.calcsize('H') == m.itemsize
3927 An integer indicating how many dimensions of a multi-dimensional array the
3930 .. attribute:: shape
3932 A tuple of integers the length of :attr:`ndim` giving the shape of the
3933 memory as an N-dimensional array.
3935 .. versionchanged:: 3.3
3936 An empty tuple instead of ``None`` when ndim = 0.
3938 .. attribute:: strides
3940 A tuple of integers the length of :attr:`ndim` giving the size in bytes to
3941 access each element for each dimension of the array.
3943 .. versionchanged:: 3.3
3944 An empty tuple instead of ``None`` when ndim = 0.
3946 .. attribute:: suboffsets
3948 Used internally for PIL-style arrays. The value is informational only.
3950 .. attribute:: c_contiguous
3952 A bool indicating whether the memory is C-:term:`contiguous`.
3954 .. versionadded:: 3.3
3956 .. attribute:: f_contiguous
3958 A bool indicating whether the memory is Fortran :term:`contiguous`.
3960 .. versionadded:: 3.3
3962 .. attribute:: contiguous
3964 A bool indicating whether the memory is :term:`contiguous`.
3966 .. versionadded:: 3.3
3971 Set Types --- :class:`set`, :class:`frozenset`
3972 ==============================================
3974 .. index:: object: set
3976 A :dfn:`set` object is an unordered collection of distinct :term:`hashable` objects.
3977 Common uses include membership testing, removing duplicates from a sequence, and
3978 computing mathematical operations such as intersection, union, difference, and
3979 symmetric difference.
3980 (For other containers see the built-in :class:`dict`, :class:`list`,
3981 and :class:`tuple` classes, and the :mod:`collections` module.)
3983 Like other collections, sets support ``x in set``, ``len(set)``, and ``for x in
3984 set``. Being an unordered collection, sets do not record element position or
3985 order of insertion. Accordingly, sets do not support indexing, slicing, or
3986 other sequence-like behavior.
3988 There are currently two built-in set types, :class:`set` and :class:`frozenset`.
3989 The :class:`set` type is mutable --- the contents can be changed using methods
3990 like :meth:`~set.add` and :meth:`~set.remove`. Since it is mutable, it has no
3991 hash value and cannot be used as either a dictionary key or as an element of
3992 another set. The :class:`frozenset` type is immutable and :term:`hashable` ---
3993 its contents cannot be altered after it is created; it can therefore be used as
3994 a dictionary key or as an element of another set.
3996 Non-empty sets (not frozensets) can be created by placing a comma-separated list
3997 of elements within braces, for example: ``{'jack', 'sjoerd'}``, in addition to the
3998 :class:`set` constructor.
4000 The constructors for both classes work the same:
4002 .. class:: set([iterable])
4003 frozenset([iterable])
4005 Return a new set or frozenset object whose elements are taken from
4006 *iterable*. The elements of a set must be :term:`hashable`. To
4007 represent sets of sets, the inner sets must be :class:`frozenset`
4008 objects. If *iterable* is not specified, a new empty set is
4011 Instances of :class:`set` and :class:`frozenset` provide the following
4014 .. describe:: len(s)
4016 Return the number of elements in set *s* (cardinality of *s*).
4018 .. describe:: x in s
4020 Test *x* for membership in *s*.
4022 .. describe:: x not in s
4024 Test *x* for non-membership in *s*.
4026 .. method:: isdisjoint(other)
4028 Return ``True`` if the set has no elements in common with *other*. Sets are
4029 disjoint if and only if their intersection is the empty set.
4031 .. method:: issubset(other)
4034 Test whether every element in the set is in *other*.
4036 .. method:: set < other
4038 Test whether the set is a proper subset of *other*, that is,
4039 ``set <= other and set != other``.
4041 .. method:: issuperset(other)
4044 Test whether every element in *other* is in the set.
4046 .. method:: set > other
4048 Test whether the set is a proper superset of *other*, that is, ``set >=
4049 other and set != other``.
4051 .. method:: union(*others)
4054 Return a new set with elements from the set and all others.
4056 .. method:: intersection(*others)
4059 Return a new set with elements common to the set and all others.
4061 .. method:: difference(*others)
4064 Return a new set with elements in the set that are not in the others.
4066 .. method:: symmetric_difference(other)
4069 Return a new set with elements in either the set or *other* but not both.
4073 Return a shallow copy of the set.
4076 Note, the non-operator versions of :meth:`union`, :meth:`intersection`,
4077 :meth:`difference`, and :meth:`symmetric_difference`, :meth:`issubset`, and
4078 :meth:`issuperset` methods will accept any iterable as an argument. In
4079 contrast, their operator based counterparts require their arguments to be
4080 sets. This precludes error-prone constructions like ``set('abc') & 'cbs'``
4081 in favor of the more readable ``set('abc').intersection('cbs')``.
4083 Both :class:`set` and :class:`frozenset` support set to set comparisons. Two
4084 sets are equal if and only if every element of each set is contained in the
4085 other (each is a subset of the other). A set is less than another set if and
4086 only if the first set is a proper subset of the second set (is a subset, but
4087 is not equal). A set is greater than another set if and only if the first set
4088 is a proper superset of the second set (is a superset, but is not equal).
4090 Instances of :class:`set` are compared to instances of :class:`frozenset`
4091 based on their members. For example, ``set('abc') == frozenset('abc')``
4092 returns ``True`` and so does ``set('abc') in set([frozenset('abc')])``.
4094 The subset and equality comparisons do not generalize to a total ordering
4095 function. For example, any two nonempty disjoint sets are not equal and are not
4096 subsets of each other, so *all* of the following return ``False``: ``a<b``,
4097 ``a==b``, or ``a>b``.
4099 Since sets only define partial ordering (subset relationships), the output of
4100 the :meth:`list.sort` method is undefined for lists of sets.
4102 Set elements, like dictionary keys, must be :term:`hashable`.
4104 Binary operations that mix :class:`set` instances with :class:`frozenset`
4105 return the type of the first operand. For example: ``frozenset('ab') |
4106 set('bc')`` returns an instance of :class:`frozenset`.
4108 The following table lists operations available for :class:`set` that do not
4109 apply to immutable instances of :class:`frozenset`:
4111 .. method:: update(*others)
4114 Update the set, adding elements from all others.
4116 .. method:: intersection_update(*others)
4119 Update the set, keeping only elements found in it and all others.
4121 .. method:: difference_update(*others)
4124 Update the set, removing elements found in others.
4126 .. method:: symmetric_difference_update(other)
4129 Update the set, keeping only elements found in either set, but not in both.
4131 .. method:: add(elem)
4133 Add element *elem* to the set.
4135 .. method:: remove(elem)
4137 Remove element *elem* from the set. Raises :exc:`KeyError` if *elem* is
4138 not contained in the set.
4140 .. method:: discard(elem)
4142 Remove element *elem* from the set if it is present.
4146 Remove and return an arbitrary element from the set. Raises
4147 :exc:`KeyError` if the set is empty.
4151 Remove all elements from the set.
4154 Note, the non-operator versions of the :meth:`update`,
4155 :meth:`intersection_update`, :meth:`difference_update`, and
4156 :meth:`symmetric_difference_update` methods will accept any iterable as an
4159 Note, the *elem* argument to the :meth:`__contains__`, :meth:`remove`, and
4160 :meth:`discard` methods may be a set. To support searching for an equivalent
4161 frozenset, a temporary one is created from *elem*.
4166 Mapping Types --- :class:`dict`
4167 ===============================
4172 triple: operations on; mapping; types
4173 triple: operations on; dictionary; type
4177 A :term:`mapping` object maps :term:`hashable` values to arbitrary objects.
4178 Mappings are mutable objects. There is currently only one standard mapping
4179 type, the :dfn:`dictionary`. (For other containers see the built-in
4180 :class:`list`, :class:`set`, and :class:`tuple` classes, and the
4181 :mod:`collections` module.)
4183 A dictionary's keys are *almost* arbitrary values. Values that are not
4184 :term:`hashable`, that is, values containing lists, dictionaries or other
4185 mutable types (that are compared by value rather than by object identity) may
4186 not be used as keys. Numeric types used for keys obey the normal rules for
4187 numeric comparison: if two numbers compare equal (such as ``1`` and ``1.0``)
4188 then they can be used interchangeably to index the same dictionary entry. (Note
4189 however, that since computers store floating-point numbers as approximations it
4190 is usually unwise to use them as dictionary keys.)
4192 Dictionaries can be created by placing a comma-separated list of ``key: value``
4193 pairs within braces, for example: ``{'jack': 4098, 'sjoerd': 4127}`` or ``{4098:
4194 'jack', 4127: 'sjoerd'}``, or by the :class:`dict` constructor.
4196 .. class:: dict(**kwarg)
4197 dict(mapping, **kwarg)
4198 dict(iterable, **kwarg)
4200 Return a new dictionary initialized from an optional positional argument
4201 and a possibly empty set of keyword arguments.
4203 If no positional argument is given, an empty dictionary is created.
4204 If a positional argument is given and it is a mapping object, a dictionary
4205 is created with the same key-value pairs as the mapping object. Otherwise,
4206 the positional argument must be an :term:`iterable` object. Each item in
4207 the iterable must itself be an iterable with exactly two objects. The
4208 first object of each item becomes a key in the new dictionary, and the
4209 second object the corresponding value. If a key occurs more than once, the
4210 last value for that key becomes the corresponding value in the new
4213 If keyword arguments are given, the keyword arguments and their values are
4214 added to the dictionary created from the positional argument. If a key
4215 being added is already present, the value from the keyword argument
4216 replaces the value from the positional argument.
4218 To illustrate, the following examples all return a dictionary equal to
4219 ``{"one": 1, "two": 2, "three": 3}``::
4221 >>> a = dict(one=1, two=2, three=3)
4222 >>> b = {'one': 1, 'two': 2, 'three': 3}
4223 >>> c = dict(zip(['one', 'two', 'three'], [1, 2, 3]))
4224 >>> d = dict([('two', 2), ('one', 1), ('three', 3)])
4225 >>> e = dict({'three': 3, 'one': 1, 'two': 2})
4226 >>> f = dict({'one': 1, 'three': 3}, two=2)
4227 >>> a == b == c == d == e == f
4230 Providing keyword arguments as in the first example only works for keys that
4231 are valid Python identifiers. Otherwise, any valid keys can be used.
4234 These are the operations that dictionaries support (and therefore, custom
4235 mapping types should support too):
4237 .. describe:: list(d)
4239 Return a list of all the keys used in the dictionary *d*.
4241 .. describe:: len(d)
4243 Return the number of items in the dictionary *d*.
4245 .. describe:: d[key]
4247 Return the item of *d* with key *key*. Raises a :exc:`KeyError` if *key* is
4250 .. index:: __missing__()
4252 If a subclass of dict defines a method :meth:`__missing__` and *key*
4253 is not present, the ``d[key]`` operation calls that method with the key *key*
4254 as argument. The ``d[key]`` operation then returns or raises whatever is
4255 returned or raised by the ``__missing__(key)`` call.
4256 No other operations or methods invoke :meth:`__missing__`. If
4257 :meth:`__missing__` is not defined, :exc:`KeyError` is raised.
4258 :meth:`__missing__` must be a method; it cannot be an instance variable::
4260 >>> class Counter(dict):
4261 ... def __missing__(self, key):
4270 The example above shows part of the implementation of
4271 :class:`collections.Counter`. A different ``__missing__`` method is used
4272 by :class:`collections.defaultdict`.
4274 .. describe:: d[key] = value
4276 Set ``d[key]`` to *value*.
4278 .. describe:: del d[key]
4280 Remove ``d[key]`` from *d*. Raises a :exc:`KeyError` if *key* is not in the
4283 .. describe:: key in d
4285 Return ``True`` if *d* has a key *key*, else ``False``.
4287 .. describe:: key not in d
4289 Equivalent to ``not key in d``.
4291 .. describe:: iter(d)
4293 Return an iterator over the keys of the dictionary. This is a shortcut
4294 for ``iter(d.keys())``.
4298 Remove all items from the dictionary.
4302 Return a shallow copy of the dictionary.
4304 .. classmethod:: fromkeys(iterable[, value])
4306 Create a new dictionary with keys from *iterable* and values set to *value*.
4308 :meth:`fromkeys` is a class method that returns a new dictionary. *value*
4309 defaults to ``None``. All of the values refer to just a single instance,
4310 so it generally doesn't make sense for *value* to be a mutable object
4311 such as an empty list. To get distinct values, use a :ref:`dict
4312 comprehension <dict>` instead.
4314 .. method:: get(key[, default])
4316 Return the value for *key* if *key* is in the dictionary, else *default*.
4317 If *default* is not given, it defaults to ``None``, so that this method
4318 never raises a :exc:`KeyError`.
4322 Return a new view of the dictionary's items (``(key, value)`` pairs).
4323 See the :ref:`documentation of view objects <dict-views>`.
4327 Return a new view of the dictionary's keys. See the :ref:`documentation
4328 of view objects <dict-views>`.
4330 .. method:: pop(key[, default])
4332 If *key* is in the dictionary, remove it and return its value, else return
4333 *default*. If *default* is not given and *key* is not in the dictionary,
4334 a :exc:`KeyError` is raised.
4336 .. method:: popitem()
4338 Remove and return a ``(key, value)`` pair from the dictionary.
4339 Pairs are returned in :abbr:`LIFO (last-in, first-out)` order.
4341 :meth:`popitem` is useful to destructively iterate over a dictionary, as
4342 often used in set algorithms. If the dictionary is empty, calling
4343 :meth:`popitem` raises a :exc:`KeyError`.
4345 .. versionchanged:: 3.7
4346 LIFO order is now guaranteed. In prior versions, :meth:`popitem` would
4347 return an arbitrary key/value pair.
4349 .. describe:: reversed(d)
4351 Return a reverse iterator over the keys of the dictionary. This is a
4352 shortcut for ``reversed(d.keys())``.
4354 .. method:: setdefault(key[, default])
4356 If *key* is in the dictionary, return its value. If not, insert *key*
4357 with a value of *default* and return *default*. *default* defaults to
4360 .. method:: update([other])
4362 Update the dictionary with the key/value pairs from *other*, overwriting
4363 existing keys. Return ``None``.
4365 :meth:`update` accepts either another dictionary object or an iterable of
4366 key/value pairs (as tuples or other iterables of length two). If keyword
4367 arguments are specified, the dictionary is then updated with those
4368 key/value pairs: ``d.update(red=1, blue=2)``.
4370 .. method:: values()
4372 Return a new view of the dictionary's values. See the
4373 :ref:`documentation of view objects <dict-views>`.
4375 An equality comparison between one ``dict.values()`` view and another
4376 will always return ``False``. This also applies when comparing
4377 ``dict.values()`` to itself::
4380 >>> d.values() == d.values()
4383 Dictionaries compare equal if and only if they have the same ``(key,
4384 value)`` pairs (regardless of ordering). Order comparisons ('<', '<=', '>=', '>') raise
4387 Dictionaries preserve insertion order. Note that updating a key does not
4388 affect the order. Keys added after deletion are inserted at the end. ::
4390 >>> d = {"one": 1, "two": 2, "three": 3, "four": 4}
4392 {'one': 1, 'two': 2, 'three': 3, 'four': 4}
4394 ['one', 'two', 'three', 'four']
4395 >>> list(d.values())
4399 {'one': 42, 'two': 2, 'three': 3, 'four': 4}
4403 {'one': 42, 'three': 3, 'four': 4, 'two': None}
4405 .. versionchanged:: 3.7
4406 Dictionary order is guaranteed to be insertion order. This behavior was
4407 an implementation detail of CPython from 3.6.
4409 Dictionaries and dictionary views are reversible. ::
4411 >>> d = {"one": 1, "two": 2, "three": 3, "four": 4}
4413 {'one': 1, 'two': 2, 'three': 3, 'four': 4}
4414 >>> list(reversed(d))
4415 ['four', 'three', 'two', 'one']
4416 >>> list(reversed(d.values()))
4418 >>> list(reversed(d.items()))
4419 [('four', 4), ('three', 3), ('two', 2), ('one', 1)]
4421 .. versionchanged:: 3.8
4422 Dictionaries are now reversible.
4426 :class:`types.MappingProxyType` can be used to create a read-only view
4432 Dictionary view objects
4433 -----------------------
4435 The objects returned by :meth:`dict.keys`, :meth:`dict.values` and
4436 :meth:`dict.items` are *view objects*. They provide a dynamic view on the
4437 dictionary's entries, which means that when the dictionary changes, the view
4438 reflects these changes.
4440 Dictionary views can be iterated over to yield their respective data, and
4441 support membership tests:
4443 .. describe:: len(dictview)
4445 Return the number of entries in the dictionary.
4447 .. describe:: iter(dictview)
4449 Return an iterator over the keys, values or items (represented as tuples of
4450 ``(key, value)``) in the dictionary.
4452 Keys and values are iterated over in insertion order.
4453 This allows the creation of ``(value, key)`` pairs
4454 using :func:`zip`: ``pairs = zip(d.values(), d.keys())``. Another way to
4455 create the same list is ``pairs = [(v, k) for (k, v) in d.items()]``.
4457 Iterating views while adding or deleting entries in the dictionary may raise
4458 a :exc:`RuntimeError` or fail to iterate over all entries.
4460 .. versionchanged:: 3.7
4461 Dictionary order is guaranteed to be insertion order.
4463 .. describe:: x in dictview
4465 Return ``True`` if *x* is in the underlying dictionary's keys, values or
4466 items (in the latter case, *x* should be a ``(key, value)`` tuple).
4468 .. describe:: reversed(dictview)
4470 Return a reverse iterator over the keys, values or items of the dictionary.
4471 The view will be iterated in reverse order of the insertion.
4473 .. versionchanged:: 3.8
4474 Dictionary views are now reversible.
4477 Keys views are set-like since their entries are unique and hashable. If all
4478 values are hashable, so that ``(key, value)`` pairs are unique and hashable,
4479 then the items view is also set-like. (Values views are not treated as set-like
4480 since the entries are generally not unique.) For set-like views, all of the
4481 operations defined for the abstract base class :class:`collections.abc.Set` are
4482 available (for example, ``==``, ``<``, or ``^``).
4484 An example of dictionary view usage::
4486 >>> dishes = {'eggs': 2, 'sausage': 1, 'bacon': 1, 'spam': 500}
4487 >>> keys = dishes.keys()
4488 >>> values = dishes.values()
4492 >>> for val in values:
4497 >>> # keys and values are iterated over in the same order (insertion order)
4499 ['eggs', 'sausage', 'bacon', 'spam']
4503 >>> # view objects are dynamic and reflect dict changes
4504 >>> del dishes['eggs']
4505 >>> del dishes['sausage']
4509 >>> # set operations
4510 >>> keys & {'eggs', 'bacon', 'salad'}
4512 >>> keys ^ {'sausage', 'juice'}
4513 {'juice', 'sausage', 'bacon', 'spam'}
4516 .. _typecontextmanager:
4518 Context Manager Types
4519 =====================
4522 single: context manager
4523 single: context management protocol
4524 single: protocol; context management
4526 Python's :keyword:`with` statement supports the concept of a runtime context
4527 defined by a context manager. This is implemented using a pair of methods
4528 that allow user-defined classes to define a runtime context that is entered
4529 before the statement body is executed and exited when the statement ends:
4532 .. method:: contextmanager.__enter__()
4534 Enter the runtime context and return either this object or another object
4535 related to the runtime context. The value returned by this method is bound to
4536 the identifier in the :keyword:`!as` clause of :keyword:`with` statements using
4537 this context manager.
4539 An example of a context manager that returns itself is a :term:`file object`.
4540 File objects return themselves from __enter__() to allow :func:`open` to be
4541 used as the context expression in a :keyword:`with` statement.
4543 An example of a context manager that returns a related object is the one
4544 returned by :func:`decimal.localcontext`. These managers set the active
4545 decimal context to a copy of the original decimal context and then return the
4546 copy. This allows changes to be made to the current decimal context in the body
4547 of the :keyword:`with` statement without affecting code outside the
4548 :keyword:`!with` statement.
4551 .. method:: contextmanager.__exit__(exc_type, exc_val, exc_tb)
4553 Exit the runtime context and return a Boolean flag indicating if any exception
4554 that occurred should be suppressed. If an exception occurred while executing the
4555 body of the :keyword:`with` statement, the arguments contain the exception type,
4556 value and traceback information. Otherwise, all three arguments are ``None``.
4558 Returning a true value from this method will cause the :keyword:`with` statement
4559 to suppress the exception and continue execution with the statement immediately
4560 following the :keyword:`!with` statement. Otherwise the exception continues
4561 propagating after this method has finished executing. Exceptions that occur
4562 during execution of this method will replace any exception that occurred in the
4563 body of the :keyword:`!with` statement.
4565 The exception passed in should never be reraised explicitly - instead, this
4566 method should return a false value to indicate that the method completed
4567 successfully and does not want to suppress the raised exception. This allows
4568 context management code to easily detect whether or not an :meth:`__exit__`
4569 method has actually failed.
4571 Python defines several context managers to support easy thread synchronisation,
4572 prompt closure of files or other objects, and simpler manipulation of the active
4573 decimal arithmetic context. The specific types are not treated specially beyond
4574 their implementation of the context management protocol. See the
4575 :mod:`contextlib` module for some examples.
4577 Python's :term:`generator`\s and the :class:`contextlib.contextmanager` decorator
4578 provide a convenient way to implement these protocols. If a generator function is
4579 decorated with the :class:`contextlib.contextmanager` decorator, it will return a
4580 context manager implementing the necessary :meth:`__enter__` and
4581 :meth:`__exit__` methods, rather than the iterator produced by an undecorated
4584 Note that there is no specific slot for any of these methods in the type
4585 structure for Python objects in the Python/C API. Extension types wanting to
4586 define these methods must provide them as a normal Python accessible method.
4587 Compared to the overhead of setting up the runtime context, the overhead of a
4588 single class dictionary lookup is negligible.
4593 Other Built-in Types
4594 ====================
4596 The interpreter supports several other kinds of objects. Most of these support
4597 only one or two operations.
4605 The only special operation on a module is attribute access: ``m.name``, where
4606 *m* is a module and *name* accesses a name defined in *m*'s symbol table.
4607 Module attributes can be assigned to. (Note that the :keyword:`import`
4608 statement is not, strictly speaking, an operation on a module object; ``import
4609 foo`` does not require a module object named *foo* to exist, rather it requires
4610 an (external) *definition* for a module named *foo* somewhere.)
4612 A special attribute of every module is :attr:`~object.__dict__`. This is the
4613 dictionary containing the module's symbol table. Modifying this dictionary will
4614 actually change the module's symbol table, but direct assignment to the
4615 :attr:`~object.__dict__` attribute is not possible (you can write
4616 ``m.__dict__['a'] = 1``, which defines ``m.a`` to be ``1``, but you can't write
4617 ``m.__dict__ = {}``). Modifying :attr:`~object.__dict__` directly is
4620 Modules built into the interpreter are written like this: ``<module 'sys'
4621 (built-in)>``. If loaded from a file, they are written as ``<module 'os' from
4622 '/usr/local/lib/pythonX.Y/os.pyc'>``.
4627 Classes and Class Instances
4628 ---------------------------
4630 See :ref:`objects` and :ref:`class` for these.
4638 Function objects are created by function definitions. The only operation on a
4639 function object is to call it: ``func(argument-list)``.
4641 There are really two flavors of function objects: built-in functions and
4642 user-defined functions. Both support the same operation (to call the function),
4643 but the implementation is different, hence the different object types.
4645 See :ref:`function` for more information.
4653 .. index:: object: method
4655 Methods are functions that are called using the attribute notation. There are
4656 two flavors: built-in methods (such as :meth:`append` on lists) and class
4657 instance methods. Built-in methods are described with the types that support
4660 If you access a method (a function defined in a class namespace) through an
4661 instance, you get a special object: a :dfn:`bound method` (also called
4662 :dfn:`instance method`) object. When called, it will add the ``self`` argument
4663 to the argument list. Bound methods have two special read-only attributes:
4664 ``m.__self__`` is the object on which the method operates, and ``m.__func__`` is
4665 the function implementing the method. Calling ``m(arg-1, arg-2, ..., arg-n)``
4666 is completely equivalent to calling ``m.__func__(m.__self__, arg-1, arg-2, ...,
4669 Like function objects, bound method objects support getting arbitrary
4670 attributes. However, since method attributes are actually stored on the
4671 underlying function object (``meth.__func__``), setting method attributes on
4672 bound methods is disallowed. Attempting to set an attribute on a method
4673 results in an :exc:`AttributeError` being raised. In order to set a method
4674 attribute, you need to explicitly set it on the underlying function object::
4677 ... def method(self):
4681 >>> c.method.whoami = 'my name is method' # can't set on the method
4682 Traceback (most recent call last):
4683 File "<stdin>", line 1, in <module>
4684 AttributeError: 'method' object has no attribute 'whoami'
4685 >>> c.method.__func__.whoami = 'my name is method'
4689 See :ref:`types` for more information.
4692 .. index:: object; code, code object
4694 .. _bltin-code-objects:
4701 single: __code__ (function object attribute)
4703 Code objects are used by the implementation to represent "pseudo-compiled"
4704 executable Python code such as a function body. They differ from function
4705 objects because they don't contain a reference to their global execution
4706 environment. Code objects are returned by the built-in :func:`compile` function
4707 and can be extracted from function objects through their :attr:`__code__`
4708 attribute. See also the :mod:`code` module.
4714 A code object can be executed or evaluated by passing it (instead of a source
4715 string) to the :func:`exec` or :func:`eval` built-in functions.
4717 See :ref:`types` for more information.
4720 .. _bltin-type-objects:
4729 Type objects represent the various object types. An object's type is accessed
4730 by the built-in function :func:`type`. There are no special operations on
4731 types. The standard module :mod:`types` defines names for all standard built-in
4734 Types are written like this: ``<class 'int'>``.
4737 .. _bltin-null-object:
4742 This object is returned by functions that don't explicitly return a value. It
4743 supports no special operations. There is exactly one null object, named
4744 ``None`` (a built-in name). ``type(None)()`` produces the same singleton.
4746 It is written as ``None``.
4749 .. index:: single: ...; ellipsis literal
4750 .. _bltin-ellipsis-object:
4755 This object is commonly used by slicing (see :ref:`slicings`). It supports no
4756 special operations. There is exactly one ellipsis object, named
4757 :const:`Ellipsis` (a built-in name). ``type(Ellipsis)()`` produces the
4758 :const:`Ellipsis` singleton.
4760 It is written as ``Ellipsis`` or ``...``.
4763 .. _bltin-notimplemented-object:
4765 The NotImplemented Object
4766 -------------------------
4768 This object is returned from comparisons and binary operations when they are
4769 asked to operate on types they don't support. See :ref:`comparisons` for more
4770 information. There is exactly one ``NotImplemented`` object.
4771 ``type(NotImplemented)()`` produces the singleton instance.
4773 It is written as ``NotImplemented``.
4776 .. _bltin-boolean-values:
4781 Boolean values are the two constant objects ``False`` and ``True``. They are
4782 used to represent truth values (although other values can also be considered
4783 false or true). In numeric contexts (for example when used as the argument to
4784 an arithmetic operator), they behave like the integers 0 and 1, respectively.
4785 The built-in function :func:`bool` can be used to convert any value to a
4786 Boolean, if the value can be interpreted as a truth value (see section
4787 :ref:`truth` above).
4792 pair: Boolean; values
4794 They are written as ``False`` and ``True``, respectively.
4802 See :ref:`types` for this information. It describes stack frame objects,
4803 traceback objects, and slice objects.
4811 The implementation adds a few special read-only attributes to several object
4812 types, where they are relevant. Some of these are not reported by the
4813 :func:`dir` built-in function.
4816 .. attribute:: object.__dict__
4818 A dictionary or other mapping object used to store an object's (writable)
4822 .. attribute:: instance.__class__
4824 The class to which a class instance belongs.
4827 .. attribute:: class.__bases__
4829 The tuple of base classes of a class object.
4832 .. attribute:: definition.__name__
4834 The name of the class, function, method, descriptor, or
4838 .. attribute:: definition.__qualname__
4840 The :term:`qualified name` of the class, function, method, descriptor,
4841 or generator instance.
4843 .. versionadded:: 3.3
4846 .. attribute:: class.__mro__
4848 This attribute is a tuple of classes that are considered when looking for
4849 base classes during method resolution.
4852 .. method:: class.mro()
4854 This method can be overridden by a metaclass to customize the method
4855 resolution order for its instances. It is called at class instantiation, and
4856 its result is stored in :attr:`~class.__mro__`.
4859 .. method:: class.__subclasses__
4861 Each class keeps a list of weak references to its immediate subclasses. This
4862 method returns a list of all those references still alive.
4865 >>> int.__subclasses__()
4869 .. rubric:: Footnotes
4871 .. [1] Additional information on these special methods may be found in the Python
4872 Reference Manual (:ref:`customization`).
4874 .. [2] As a consequence, the list ``[1, 2]`` is considered equal to ``[1.0, 2.0]``, and
4875 similarly for tuples.
4877 .. [3] They must have since the parser can't tell the type of the operands.
4879 .. [4] Cased characters are those with general category property being one of
4880 "Lu" (Letter, uppercase), "Ll" (Letter, lowercase), or "Lt" (Letter, titlecase).
4882 .. [5] To format only a tuple you should therefore provide a singleton tuple whose only
4883 element is the tuple to be formatted.