]> git.ipfire.org Git - thirdparty/Python/cpython.git/blob
1149508
[thirdparty/Python/cpython.git] /
1 .. XXX: reference/datamodel and this have quite a few overlaps!
2
3
4 .. _bltin-types:
5
6 **************
7 Built-in Types
8 **************
9
10 The following sections describe the standard types that are built into the
11 interpreter.
12
13 .. index:: pair: built-in; types
14
15 The principal built-in types are numerics, sequences, mappings, classes,
16 instances and exceptions.
17
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``.
21
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.
27
28
29 .. _truth:
30
31 Truth Value Testing
32 ===================
33
34 .. index::
35 statement: if
36 statement: while
37 pair: truth; value
38 pair: Boolean; operations
39 single: false
40
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.
43
44 .. index:: single: true
45
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:
50
51 .. index::
52 single: None (Built-in object)
53 single: False (Built-in object)
54
55 * constants defined to be false: ``None`` and ``False``.
56
57 * zero of any numeric type: ``0``, ``0.0``, ``0j``, ``Decimal(0)``,
58 ``Fraction(0, 1)``
59
60 * empty sequences and collections: ``''``, ``()``, ``[]``, ``{}``, ``set()``,
61 ``range(0)``
62
63 .. index::
64 operator: or
65 operator: and
66 single: False
67 single: True
68
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.)
73
74
75 .. _boolean:
76
77 Boolean Operations --- :keyword:`!and`, :keyword:`!or`, :keyword:`!not`
78 =======================================================================
79
80 .. index:: pair: Boolean; operations
81
82 These are the Boolean operations, ordered by ascending priority:
83
84 +-------------+---------------------------------+-------+
85 | Operation | Result | Notes |
86 +=============+=================================+=======+
87 | ``x or y`` | if *x* is false, then *y*, else | \(1) |
88 | | *x* | |
89 +-------------+---------------------------------+-------+
90 | ``x and y`` | if *x* is false, then *x*, else | \(2) |
91 | | *y* | |
92 +-------------+---------------------------------+-------+
93 | ``not x`` | if *x* is false, then ``True``, | \(3) |
94 | | else ``False`` | |
95 +-------------+---------------------------------+-------+
96
97 .. index::
98 operator: and
99 operator: or
100 operator: not
101
102 Notes:
103
104 (1)
105 This is a short-circuit operator, so it only evaluates the second
106 argument if the first one is false.
107
108 (2)
109 This is a short-circuit operator, so it only evaluates the second
110 argument if the first one is true.
111
112 (3)
113 ``not`` has a lower priority than non-Boolean operators, so ``not a == b`` is
114 interpreted as ``not (a == b)``, and ``a == not b`` is a syntax error.
115
116
117 .. _stdcomparisons:
118
119 Comparisons
120 ===========
121
122 .. index::
123 pair: chaining; comparisons
124 pair: operator; comparison
125 operator: ==
126 operator: < (less)
127 operator: <=
128 operator: > (greater)
129 operator: >=
130 operator: !=
131 operator: is
132 operator: is not
133
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).
139
140 This table summarizes the comparison operations:
141
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 +------------+-------------------------+
153 | ``==`` | equal |
154 +------------+-------------------------+
155 | ``!=`` | not equal |
156 +------------+-------------------------+
157 | ``is`` | object identity |
158 +------------+-------------------------+
159 | ``is not`` | negated object identity |
160 +------------+-------------------------+
161
162 .. index::
163 pair: object; numeric
164 pair: objects; comparing
165
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.
171
172 .. index::
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)
179
180 Non-identical instances of a class normally compare as non-equal unless the
181 class defines the :meth:`__eq__` method.
182
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).
188
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
191 exception.
192
193 .. index::
194 operator: in
195 operator: not in
196
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.
200
201 .. _typesnumeric:
202
203 Numeric Types --- :class:`int`, :class:`float`, :class:`complex`
204 ================================================================
205
206 .. index::
207 object: numeric
208 object: Boolean
209 object: integer
210 object: floating point
211 object: complex number
212 pair: C; language
213
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.)
226
227 .. index::
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
235
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
242 and imaginary parts.
243
244 .. index::
245 single: arithmetic
246 builtin: int
247 builtin: float
248 builtin: complex
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)
256 operator: / (slash)
257 operator: //
258 operator: % (percent)
259 operator: **
260
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.
267
268 All numeric types (except complex) support the following operations (for priorities of
269 the operations, see :ref:`operator-summary`):
270
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) | |
283 | | *y* | | |
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` |
292 | | *x* | | |
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 | | |
303 | | *c* | | |
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 +---------------------+---------------------------------+---------+--------------------+
311
312 .. index::
313 triple: operations on; numeric; types
314 single: conjugate() (complex number method)
315
316 Notes:
317
318 (1)
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``.
323
324 (2)
325 Not for complex numbers. Instead convert to floats using :func:`abs` if
326 appropriate.
327
328 (3)
329 .. index::
330 module: math
331 single: floor() (in module math)
332 single: ceil() (in module math)
333 single: trunc() (in module math)
334 pair: numeric; conversions
335 pair: C; language
336
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.
340
341 (4)
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.
344
345 (5)
346 Python defines ``pow(0, 0)`` and ``0 ** 0`` to be ``1``, as is common for
347 programming languages.
348
349 (6)
350 The numeric literals accepted include the digits ``0`` to ``9`` or any
351 Unicode equivalent (code points with the ``Nd`` property).
352
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.
355
356
357 All :class:`numbers.Real` types (:class:`int` and :class:`float`) also include
358 the following operations:
359
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* |
374 | <math.ceil>` | |
375 +--------------------+---------------------------------------------+
376
377 For additional numeric operations see the :mod:`math` and :mod:`cmath`
378 modules.
379
380 .. XXXJH exceptions: overflow (when? what operations?) zerodivision
381
382
383 .. _bitstring-ops:
384
385 Bitwise Operations on Integer Types
386 -----------------------------------
387
388 .. index::
389 triple: operations on; integer; types
390 pair: bitwise; operations
391 pair: shifting; operations
392 pair: masking; operations
393 operator: | (vertical bar)
394 operator: ^ (caret)
395 operator: & (ampersand)
396 operator: <<
397 operator: >>
398 operator: ~ (tilde)
399
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.
403
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 ``-``).
407
408 This table lists the bitwise operations sorted in ascending priority:
409
410 +------------+--------------------------------+----------+
411 | Operation | Result | Notes |
412 +============+================================+==========+
413 | ``x | y`` | bitwise :dfn:`or` of *x* and | \(4) |
414 | | *y* | |
415 +------------+--------------------------------+----------+
416 | ``x ^ y`` | bitwise :dfn:`exclusive or` of | \(4) |
417 | | *x* and *y* | |
418 +------------+--------------------------------+----------+
419 | ``x & y`` | bitwise :dfn:`and` of *x* and | \(4) |
420 | | *y* | |
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 +------------+--------------------------------+----------+
428
429 Notes:
430
431 (1)
432 Negative shift counts are illegal and cause a :exc:`ValueError` to be raised.
433
434 (2)
435 A left shift by *n* bits is equivalent to multiplication by ``pow(2, n)``
436 without overflow check.
437
438 (3)
439 A right shift by *n* bits is equivalent to division by ``pow(2, n)`` without
440 overflow check.
441
442 (4)
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.
447
448
449 Additional Methods on Integer Types
450 -----------------------------------
451
452 The int type implements the :class:`numbers.Integral` :term:`abstract base
453 class`. In addition, it provides a few more methods:
454
455 .. method:: int.bit_length()
456
457 Return the number of bits necessary to represent an integer in binary,
458 excluding the sign and leading zeros::
459
460 >>> n = -37
461 >>> bin(n)
462 '-0b100101'
463 >>> n.bit_length()
464 6
465
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``.
471
472 Equivalent to::
473
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
478
479 .. versionadded:: 3.1
480
481 .. method:: int.to_bytes(length, byteorder, \*, signed=False)
482
483 Return an array of bytes representing an integer.
484
485 >>> (1024).to_bytes(2, byteorder='big')
486 b'\x04\x00'
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'
491 >>> x = 1000
492 >>> x.to_bytes((x.bit_length() + 7) // 8, byteorder='little')
493 b'\xe8\x03'
494
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
497 bytes.
498
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
504 value.
505
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*
509 is ``False``.
510
511 .. versionadded:: 3.2
512
513 .. classmethod:: int.from_bytes(bytes, byteorder, \*, signed=False)
514
515 Return the integer represented by the given array of bytes.
516
517 >>> int.from_bytes(b'\x00\x10', byteorder='big')
518 16
519 >>> int.from_bytes(b'\x00\x10', byteorder='little')
520 4096
521 >>> int.from_bytes(b'\xfc\x00', byteorder='big', signed=True)
522 -1024
523 >>> int.from_bytes(b'\xfc\x00', byteorder='big', signed=False)
524 64512
525 >>> int.from_bytes([255, 0, 0], byteorder='big')
526 16711680
527
528 The argument *bytes* must either be a :term:`bytes-like object` or an
529 iterable producing bytes.
530
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
536 value.
537
538 The *signed* argument indicates whether two's complement is used to
539 represent the integer.
540
541 .. versionadded:: 3.2
542
543 .. method:: int.as_integer_ratio()
544
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
548 denominator.
549
550 .. versionadded:: 3.8
551
552 Additional Methods on Float
553 ---------------------------
554
555 The float type implements the :class:`numbers.Real` :term:`abstract base
556 class`. float also has the following additional methods.
557
558 .. method:: float.as_integer_ratio()
559
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
563 NaNs.
564
565 .. method:: float.is_integer()
566
567 Return ``True`` if the float instance is finite with integral
568 value, and ``False`` otherwise::
569
570 >>> (-2.0).is_integer()
571 True
572 >>> (3.2).is_integer()
573 False
574
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.
582
583
584 .. method:: float.hex()
585
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
589 exponent.
590
591
592 .. classmethod:: float.fromhex(s)
593
594 Class method to return the float represented by a hexadecimal
595 string *s*. The string *s* may have leading and trailing
596 whitespace.
597
598
599 Note that :meth:`float.hex` is an instance method, while
600 :meth:`float.fromhex` is a class method.
601
602 A hexadecimal string takes the form::
603
604 [sign] ['0x'] integer ['.' fraction] ['p' exponent]
605
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`.
617
618
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
623 ``3740.0``::
624
625 >>> float.fromhex('0x3.a7p10')
626 3740.0
627
628
629 Applying the reverse conversion to ``3740.0`` gives a different
630 hexadecimal string representing the same number::
631
632 >>> float.hex(3740.0)
633 '0x1.d380000000000p+11'
634
635
636 .. _numeric-hash:
637
638 Hashing of numeric types
639 ------------------------
640
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`.
653
654 .. impl-detail::
655
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.
658
659 Here are the rules in detail:
660
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``.
664
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``.
669
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
672 ``-2``.
673
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.)
678
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``.
685
686
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`::
690
691
692 import sys, math
693
694 def hash_fraction(m, n):
695 """Compute the hash of a rational number m / n.
696
697 Assumes m and n are integers, with n positive.
698 Equivalent to hash(fractions.Fraction(m, n)).
699
700 """
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
705
706 if n % P == 0:
707 hash_value = sys.hash_info.inf
708 else:
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
712 if m < 0:
713 hash_value = -hash_value
714 if hash_value == -1:
715 hash_value = -2
716 return hash_value
717
718 def hash_float(x):
719 """Compute the hash of a float x."""
720
721 if math.isnan(x):
722 return sys.hash_info.nan
723 elif math.isinf(x):
724 return sys.hash_info.inf if x > 0 else -sys.hash_info.inf
725 else:
726 return hash_fraction(*x.as_integer_ratio())
727
728 def hash_complex(z):
729 """Compute the hash of a complex number z."""
730
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)
735 if hash_value == -1:
736 hash_value = -2
737 return hash_value
738
739 .. _typeiter:
740
741 Iterator Types
742 ==============
743
744 .. index::
745 single: iterator protocol
746 single: protocol; iterator
747 single: sequence; iteration
748 single: container; iteration over
749
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.
754
755 One method needs to be defined for container objects to provide iteration
756 support:
757
758 .. XXX duplicated in reference/datamodel!
759
760 .. method:: container.__iter__()
761
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
769 API.
770
771 The iterator objects themselves are required to support the following two
772 methods, which together form the :dfn:`iterator protocol`:
773
774
775 .. method:: iterator.__iter__()
776
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.
781
782
783 .. method:: iterator.__next__()
784
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
788 Python/C API.
789
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
793 protocol.
794
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.
798
799
800 .. _generator-types:
801
802 Generator Types
803 ---------------
804
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__`
809 methods.
810 More information about generators can be found in :ref:`the documentation for
811 the yield expression <yieldexpr>`.
812
813
814 .. _typesseq:
815
816 Sequence Types --- :class:`list`, :class:`tuple`, :class:`range`
817 ================================================================
818
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.
823
824
825 .. _typesseq-common:
826
827 Common Sequence Operations
828 --------------------------
829
830 .. index:: object: sequence
831
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.
836
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*.
841
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]_
845
846 .. index::
847 triple: operations on; sequence; types
848 builtin: len
849 builtin: min
850 builtin: max
851 pair: concatenation; operation
852 pair: repetition; operation
853 pair: subscript; operation
854 pair: slice; operation
855 operator: in
856 operator: not in
857 single: count() (sequence method)
858 single: index() (sequence method)
859
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) |
870 | | *t* | |
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 | |
893 | | *x* in *s* | |
894 +--------------------------+--------------------------------+----------+
895
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.)
901
902 Notes:
903
904 (1)
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::
909
910 >>> "gg" in "eggs"
911 True
912
913 (2)
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::
918
919 >>> lists = [[]] * 3
920 >>> lists
921 [[], [], []]
922 >>> lists[0].append(3)
923 >>> lists
924 [[3], [3], [3]]
925
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::
930
931 >>> lists = [[] for i in range(3)]
932 >>> lists[0].append(3)
933 >>> lists[1].append(5)
934 >>> lists[2].append(7)
935 >>> lists
936 [[3], [5], [7]]
937
938 Further explanation is available in the FAQ entry
939 :ref:`faq-multidimensional-list`.
940
941 (3)
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
944 still ``0``.
945
946 (4)
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
951 empty.
952
953 (5)
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``.
963
964 (6)
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:
969
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
973
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
978
979 * if concatenating :class:`tuple` objects, extend a :class:`list` instead
980
981 * for other types, investigate the relevant class documentation
982
983
984 (7)
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.
988
989 (8)
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.
996
997
998 .. _typesseq-immutable:
999
1000 Immutable Sequence Types
1001 ------------------------
1002
1003 .. index::
1004 triple: immutable; sequence; types
1005 object: tuple
1006 builtin: hash
1007
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`
1010 built-in.
1011
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`
1014 instances.
1015
1016 Attempting to hash an immutable sequence that contains unhashable values will
1017 result in :exc:`TypeError`.
1018
1019
1020 .. _typesseq-mutable:
1021
1022 Mutable Sequence Types
1023 ----------------------
1024
1025 .. index::
1026 triple: mutable; sequence; types
1027 object: list
1028 object: bytearray
1029
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.
1033
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``).
1038
1039
1040 .. index::
1041 triple: operations on; sequence; types
1042 triple: operations on; list; type
1043 pair: subscript; assignment
1044 pair: slice; assignment
1045 statement: del
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)
1054
1055 +------------------------------+--------------------------------+---------------------+
1056 | Operation | Result | Notes |
1057 +==============================+================================+=====================+
1058 | ``s[i] = x`` | item *i* of *s* is replaced by | |
1059 | | *x* | |
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) |
1102 | | place | |
1103 +------------------------------+--------------------------------+---------------------+
1104
1105
1106 Notes:
1107
1108 (1)
1109 *t* must have the same length as the slice it is replacing.
1110
1111 (2)
1112 The optional argument *i* defaults to ``-1``, so that by default the last
1113 item is removed and returned.
1114
1115 (3)
1116 :meth:`remove` raises :exc:`ValueError` when *x* is not found in *s*.
1117
1118 (4)
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.
1122
1123 (5)
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.
1129
1130 .. versionadded:: 3.3
1131 :meth:`clear` and :meth:`!copy` methods.
1132
1133 (6)
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`.
1138
1139
1140 .. _typesseq-list:
1141
1142 Lists
1143 -----
1144
1145 .. index:: object: list
1146
1147 Lists are mutable sequences, typically used to store collections of
1148 homogeneous items (where the precise degree of similarity will vary by
1149 application).
1150
1151 .. class:: list([iterable])
1152
1153 Lists may be constructed in several ways:
1154
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)``
1159
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, ``[]``.
1167
1168
1169 Many other operations also produce lists, including the :func:`sorted`
1170 built-in.
1171
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:
1175
1176 .. method:: list.sort(*, key=None, reverse=False)
1177
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).
1182
1183 :meth:`sort` accepts two arguments that can only be passed by keyword
1184 (:ref:`keyword-only arguments <keyword-only_parameter>`):
1185
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
1191 key value.
1192
1193 The :func:`functools.cmp_to_key` utility is available to convert a 2.x
1194 style *cmp* function to a *key* function.
1195
1196 *reverse* is a boolean value. If set to ``True``, then the list elements
1197 are sorted as if each comparison were reversed.
1198
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).
1203
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).
1208
1209 For sorting examples and a brief sorting tutorial, see :ref:`sortinghowto`.
1210
1211 .. impl-detail::
1212
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.
1217
1218
1219 .. _typesseq-tuple:
1220
1221 Tuples
1222 ------
1223
1224 .. index:: object: tuple
1225
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).
1231
1232 .. class:: tuple([iterable])
1233
1234 Tuples may be constructed in a number of ways:
1235
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)``
1240
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, ``()``.
1248
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.
1254
1255 Tuples implement all of the :ref:`common <typesseq-common>` sequence
1256 operations.
1257
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.
1261
1262
1263 .. _typesseq-range:
1264
1265 Ranges
1266 ------
1267
1268 .. index:: object: range
1269
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`
1272 loops.
1273
1274 .. class:: range(stop)
1275 range(start, stop[, step])
1276
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.
1282
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
1285 ``r[i] < stop``.
1286
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``.
1290
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
1294 indices.
1295
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`.
1299
1300 Range examples::
1301
1302 >>> list(range(10))
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))
1309 [0, 3, 6, 9]
1310 >>> list(range(0, -10, -1))
1311 [0, -1, -2, -3, -4, -5, -6, -7, -8, -9]
1312 >>> list(range(0))
1313 []
1314 >>> list(range(1, 0))
1315 []
1316
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).
1321
1322 .. attribute:: start
1323
1324 The value of the *start* parameter (or ``0`` if the parameter was
1325 not supplied)
1326
1327 .. attribute:: stop
1328
1329 The value of the *stop* parameter
1330
1331 .. attribute:: step
1332
1333 The value of the *step* parameter (or ``1`` if the parameter was
1334 not supplied)
1335
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).
1341
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`):
1345
1346 >>> r = range(0, 20, 2)
1347 >>> r
1348 range(0, 20, 2)
1349 >>> 11 in r
1350 False
1351 >>> 10 in r
1352 True
1353 >>> r.index(10)
1354 5
1355 >>> r[5]
1356 10
1357 >>> r[:5]
1358 range(0, 10, 2)
1359 >>> r[-1]
1360 18
1361
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)``.)
1368
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.
1374
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
1378 object identity).
1379
1380 .. versionadded:: 3.3
1381 The :attr:`~range.start`, :attr:`~range.stop` and :attr:`~range.step`
1382 attributes.
1383
1384 .. seealso::
1385
1386 * The `linspace recipe <http://code.activestate.com/recipes/579000/>`_
1387 shows how to implement a lazy version of range suitable for floating
1388 point applications.
1389
1390 .. index::
1391 single: string; text sequence type
1392 single: str (built-in class); (see also string)
1393 object: string
1394
1395 .. _textseq:
1396
1397 Text Sequence Type --- :class:`str`
1398 ===================================
1399
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:
1404
1405 * Single quotes: ``'allows embedded "double" quotes'``
1406 * Double quotes: ``"allows embedded 'single' quotes"``.
1407 * Triple quoted: ``'''Three single quotes'''``, ``"""Three double quotes"""``
1408
1409 Triple quoted strings may span multiple lines - all associated whitespace will
1410 be included in the string literal.
1411
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"``.
1415
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.
1419
1420 Strings may also be created from other objects using the :class:`str`
1421 constructor.
1422
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]``.
1425
1426 .. index::
1427 object: io.StringIO
1428
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
1431 multiple fragments.
1432
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.
1437
1438
1439 .. index::
1440 single: string; str (built-in class)
1441
1442 .. class:: str(object='')
1443 str(object=b'', encoding='utf-8', errors='strict')
1444
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.
1448
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>`.
1455
1456 .. index::
1457 single: buffer protocol; str (built-in class)
1458 single: bytes; str (built-in class)
1459
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.
1468
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::
1473
1474 >>> str(b'Zoot!')
1475 "b'Zoot!'"
1476
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.
1481
1482
1483 .. index::
1484 pair: string; methods
1485
1486 .. _string-methods:
1487
1488 String Methods
1489 --------------
1490
1491 .. index::
1492 module: re
1493
1494 Strings implement all of the :ref:`common <typesseq-common>` sequence
1495 operations, along with the additional methods described below.
1496
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`).
1503
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).
1507
1508 .. method:: str.capitalize()
1509
1510 Return a copy of the string with its first character capitalized and the
1511 rest lowercased.
1512
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.
1517
1518 .. method:: str.casefold()
1519
1520 Return a casefolded copy of the string. Casefolded strings may be used for
1521 caseless matching.
1522
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"``.
1528
1529 The casefolding algorithm is described in section 3.13 of the Unicode
1530 Standard.
1531
1532 .. versionadded:: 3.3
1533
1534
1535 .. method:: str.center(width[, fillchar])
1536
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)``.
1540
1541
1542
1543 .. method:: str.count(sub[, start[, end]])
1544
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.
1548
1549
1550 .. method:: str.encode(encoding="utf-8", errors="strict")
1551
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`.
1560
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*.
1564
1565 .. versionchanged:: 3.1
1566 Support for keyword arguments added.
1567
1568 .. versionchanged:: 3.9
1569 The *errors* is now checked in development mode and in debug mode.
1570
1571
1572 .. method:: str.endswith(suffix[, start[, end]])
1573
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
1577 at that position.
1578
1579
1580 .. method:: str.expandtabs(tabsize=8)
1581
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
1593 printed.
1594
1595 >>> '01\t012\t0123\t01234'.expandtabs()
1596 '01 012 0123 01234'
1597 >>> '01\t012\t0123\t01234'.expandtabs(4)
1598 '01 012 0123 01234'
1599
1600
1601 .. method:: str.find(sub[, start[, end]])
1602
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.
1606
1607 .. note::
1608
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::
1612
1613 >>> 'Py' in 'Python'
1614 True
1615
1616
1617 .. method:: str.format(*args, **kwargs)
1618
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.
1625
1626 >>> "The sum of 1 + 2 is {0}".format(1+2)
1627 'The sum of 1 + 2 is 3'
1628
1629 See :ref:`formatstrings` for a description of the various formatting options
1630 that can be specified in format strings.
1631
1632 .. note::
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
1640 other threads.
1641
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
1645 cases.
1646
1647
1648 .. method:: str.format_map(mapping)
1649
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:
1653
1654 >>> class Default(dict):
1655 ... def __missing__(self, key):
1656 ... return key
1657 ...
1658 >>> '{name} was born in {country}'.format_map(Default(name='Guido'))
1659 'Guido was born in country'
1660
1661 .. versionadded:: 3.2
1662
1663
1664 .. method:: str.index(sub[, start[, end]])
1665
1666 Like :meth:`~str.find`, but raise :exc:`ValueError` when the substring is
1667 not found.
1668
1669
1670 .. method:: str.isalnum()
1671
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()``.
1676
1677
1678 .. method:: str.isalpha()
1679
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.
1685
1686
1687 .. method:: str.isascii()
1688
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.
1692
1693 .. versionadded:: 3.7
1694
1695
1696 .. method:: str.isdecimal()
1697
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".
1704
1705
1706 .. method:: str.isdigit()
1707
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.
1714
1715
1716 .. method:: str.isidentifier()
1717
1718 Return ``True`` if the string is a valid identifier according to the language
1719 definition, section :ref:`identifiers`.
1720
1721 Call :func:`keyword.iskeyword` to test whether string ``s`` is a reserved
1722 identifier, such as :keyword:`def` and :keyword:`class`.
1723
1724 Example:
1725 ::
1726
1727 >>> from keyword import iskeyword
1728
1729 >>> 'hello'.isidentifier(), iskeyword('hello')
1730 True, False
1731 >>> 'def'.isidentifier(), iskeyword('def')
1732 True, True
1733
1734
1735 .. method:: str.islower()
1736
1737 Return ``True`` if all cased characters [4]_ in the string are lowercase and
1738 there is at least one cased character, ``False`` otherwise.
1739
1740
1741 .. method:: str.isnumeric()
1742
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.
1749
1750
1751 .. method:: str.isprintable()
1752
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`.)
1760
1761
1762 .. method:: str.isspace()
1763
1764 Return ``True`` if there are only whitespace characters in the string and there is
1765 at least one character, ``False`` otherwise.
1766
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``,
1770 ``B``, or ``S``.
1771
1772
1773 .. method:: str.istitle()
1774
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.
1778
1779
1780 .. method:: str.isupper()
1781
1782 Return ``True`` if all cased characters [4]_ in the string are uppercase and
1783 there is at least one cased character, ``False`` otherwise.
1784
1785 >>> 'BANANA'.isupper()
1786 True
1787 >>> 'banana'.isupper()
1788 False
1789 >>> 'baNana'.isupper()
1790 False
1791 >>> ' '.isupper()
1792 False
1793
1794
1795
1796 .. method:: str.join(iterable)
1797
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.
1802
1803
1804 .. method:: str.ljust(width[, fillchar])
1805
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)``.
1809
1810
1811 .. method:: str.lower()
1812
1813 Return a copy of the string with all the cased characters [4]_ converted to
1814 lowercase.
1815
1816 The lowercasing algorithm used is described in section 3.13 of the Unicode
1817 Standard.
1818
1819
1820 .. method:: str.lstrip([chars])
1821
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::
1826
1827 >>> ' spacious '.lstrip()
1828 'spacious '
1829 >>> 'www.example.com'.lstrip('cmowz.')
1830 'example.com'
1831
1832
1833 .. staticmethod:: str.maketrans(x[, y[, z]])
1834
1835 This static method returns a translation table usable for :meth:`str.translate`.
1836
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.
1841
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.
1846
1847
1848 .. method:: str.partition(sep)
1849
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.
1854
1855
1856 .. method:: str.replace(old, new[, count])
1857
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.
1861
1862
1863 .. method:: str.rfind(sub[, start[, end]])
1864
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.
1868
1869
1870 .. method:: str.rindex(sub[, start[, end]])
1871
1872 Like :meth:`rfind` but raises :exc:`ValueError` when the substring *sub* is not
1873 found.
1874
1875
1876 .. method:: str.rjust(width[, fillchar])
1877
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)``.
1881
1882
1883 .. method:: str.rpartition(sep)
1884
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.
1889
1890
1891 .. method:: str.rsplit(sep=None, maxsplit=-1)
1892
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.
1898
1899
1900 .. method:: str.rstrip([chars])
1901
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::
1906
1907 >>> ' spacious '.rstrip()
1908 ' spacious'
1909 >>> 'mississippi'.rstrip('ipz')
1910 'mississ'
1911
1912
1913 .. method:: str.split(sep=None, maxsplit=-1)
1914
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).
1920
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 ``['']``.
1926
1927 For example::
1928
1929 >>> '1,2,3'.split(',')
1930 ['1', '2', '3']
1931 >>> '1,2,3'.split(',', maxsplit=1)
1932 ['1', '2,3']
1933 >>> '1,2,,3,'.split(',')
1934 ['1', '2', '', '3', '']
1935
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
1941 returns ``[]``.
1942
1943 For example::
1944
1945 >>> '1 2 3'.split()
1946 ['1', '2', '3']
1947 >>> '1 2 3'.split(maxsplit=1)
1948 ['1', '2 3']
1949 >>> ' 1 2 3 '.split()
1950 ['1', '2', '3']
1951
1952
1953 .. index::
1954 single: universal newlines; str.splitlines method
1955
1956 .. method:: str.splitlines([keepends])
1957
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
1960 true.
1961
1962 This method splits on the following line boundaries. In particular, the
1963 boundaries are a superset of :term:`universal newlines`.
1964
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 +-----------------------+-----------------------------+
1990
1991 .. versionchanged:: 3.2
1992
1993 ``\v`` and ``\f`` added to list of line boundaries.
1994
1995 For example::
1996
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']
2001
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::
2005
2006 >>> "".splitlines()
2007 []
2008 >>> "One line\n".splitlines()
2009 ['One line']
2010
2011 For comparison, ``split('\n')`` gives::
2012
2013 >>> ''.split('\n')
2014 ['']
2015 >>> 'Two lines\n'.split('\n')
2016 ['Two lines', '']
2017
2018
2019 .. method:: str.startswith(prefix[, start[, end]])
2020
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.
2025
2026
2027 .. method:: str.strip([chars])
2028
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::
2034
2035 >>> ' spacious '.strip()
2036 'spacious'
2037 >>> 'www.example.com'.strip('cmowz.')
2038 'example'
2039
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.
2044 For example::
2045
2046 >>> comment_string = '#....... Section 3.2.1 Issue #32 .......'
2047 >>> comment_string.strip('.#! ')
2048 'Section 3.2.1 Issue #32'
2049
2050
2051 .. method:: str.swapcase()
2052
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``.
2056
2057
2058 .. method:: str.title()
2059
2060 Return a titlecased version of the string where words start with an uppercase
2061 character and the remaining characters are lowercase.
2062
2063 For example::
2064
2065 >>> 'Hello world'.title()
2066 'Hello World'
2067
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::
2072
2073 >>> "they're bill's friends from the UK".title()
2074 "They'Re Bill'S Friends From The Uk"
2075
2076 A workaround for apostrophes can be constructed using regular expressions::
2077
2078 >>> import re
2079 >>> def titlecase(s):
2080 ... return re.sub(r"[A-Za-z]+('[A-Za-z]+)?",
2081 ... lambda mo: mo.group(0).capitalize(),
2082 ... s)
2083 ...
2084 >>> titlecase("they're bill's friends.")
2085 "They're Bill's Friends."
2086
2087
2088 .. method:: str.translate(table)
2089
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.
2098
2099 You can use :meth:`str.maketrans` to create a translation map from
2100 character-to-character mappings in different formats.
2101
2102 See also the :mod:`codecs` module for a more flexible approach to custom
2103 character mappings.
2104
2105
2106 .. method:: str.upper()
2107
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,
2112 titlecase).
2113
2114 The uppercasing algorithm used is described in section 3.13 of the Unicode
2115 Standard.
2116
2117
2118 .. method:: str.zfill(width)
2119
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)``.
2125
2126 For example::
2127
2128 >>> "42".zfill(5)
2129 '00042'
2130 >>> "-42".zfill(5)
2131 '-0042'
2132
2133
2134
2135 .. _old-string-formatting:
2136
2137 ``printf``-style String Formatting
2138 ----------------------------------
2139
2140 .. index::
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
2148
2149 .. note::
2150
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.
2158
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.
2164
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
2168 dictionary).
2169
2170 .. index::
2171 single: () (parentheses); in printf-style formatting
2172 single: * (asterisk); in printf-style formatting
2173 single: . (dot); in printf-style formatting
2174
2175 A conversion specifier contains two or more characters and has the following
2176 components, which must occur in this order:
2177
2178 #. The ``'%'`` character, which marks the start of the specifier.
2179
2180 #. Mapping key (optional), consisting of a parenthesised sequence of characters
2181 (for example, ``(somename)``).
2182
2183 #. Conversion flags (optional), which affect the result of some conversion
2184 types.
2185
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.
2189
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
2193 precision.
2194
2195 #. Length modifier (optional).
2196
2197 #. Conversion type.
2198
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:
2203
2204 >>> print('%(language)s has %(number)03d quote types.' %
2205 ... {'language': "Python", "number": 2})
2206 Python has 002 quote types.
2207
2208 In this case no ``*`` specifiers may occur in a format (since they require a
2209 sequential parameter list).
2210
2211 The conversion flag characters are:
2212
2213 .. index::
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
2218
2219 +---------+---------------------------------------------------------------------+
2220 | Flag | Meaning |
2221 +=========+=====================================================================+
2222 | ``'#'`` | The value conversion will use the "alternate form" (where defined |
2223 | | below). |
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 +---------+---------------------------------------------------------------------+
2236
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``.
2239
2240 The conversion types are:
2241
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 +------------+-----------------------------------------------------+-------+
2288
2289 Notes:
2290
2291 (1)
2292 The alternate form causes a leading octal specifier (``'0o'``) to be
2293 inserted before the first digit.
2294
2295 (2)
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.
2298
2299 (3)
2300 The alternate form causes the result to always contain a decimal point, even if
2301 no digits follow it.
2302
2303 The precision determines the number of digits after the decimal point and
2304 defaults to 6.
2305
2306 (4)
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.
2309
2310 The precision determines the number of significant digits before and after the
2311 decimal point and defaults to 6.
2312
2313 (5)
2314 If precision is ``N``, the output is truncated to ``N`` characters.
2315
2316 (6)
2317 See :pep:`237`.
2318
2319 Since Python strings have an explicit length, ``%s`` conversions do not assume
2320 that ``'\0'`` is the end of the string.
2321
2322 .. XXX Examples?
2323
2324 .. versionchanged:: 3.1
2325 ``%f`` conversions for numbers whose absolute value is over 1e50 are no
2326 longer replaced by ``%g`` conversions.
2327
2328
2329 .. index::
2330 single: buffer protocol; binary sequence types
2331
2332 .. _binaryseq:
2333
2334 Binary Sequence Types --- :class:`bytes`, :class:`bytearray`, :class:`memoryview`
2335 =================================================================================
2336
2337 .. index::
2338 object: bytes
2339 object: bytearray
2340 object: memoryview
2341 module: array
2342
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.
2347
2348 The :mod:`array` module supports efficient storage of basic data types like
2349 32-bit integers and IEEE754 double-precision floating values.
2350
2351 .. _typebytes:
2352
2353 Bytes Objects
2354 -------------
2355
2356 .. index:: object: bytes
2357
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.
2362
2363 .. class:: bytes([source[, encoding[, errors]]])
2364
2365 Firstly, the syntax for bytes literals is largely the same as that for string
2366 literals, except that a ``b`` prefix is added:
2367
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"""``
2371
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.
2375
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.
2379
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).
2389
2390 In addition to the literal forms, bytes objects can be created in a number of
2391 other ways:
2392
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)``
2396
2397 Also see the :ref:`bytes <func-bytes>` built-in.
2398
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:
2402
2403 .. classmethod:: fromhex(string)
2404
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.
2408
2409 >>> bytes.fromhex('2Ef0 F1f2 ')
2410 b'.\xf0\xf1\xf2'
2411
2412 .. versionchanged:: 3.7
2413 :meth:`bytes.fromhex` now skips all ASCII whitespace in the string,
2414 not just spaces.
2415
2416 A reverse conversion function exists to transform a bytes object into its
2417 hexadecimal representation.
2418
2419 .. method:: hex()
2420
2421 Return a string object containing two hexadecimal digits for each
2422 byte in the instance.
2423
2424 >>> b'\xf0\xf1\xf2'.hex()
2425 'f0f1f2'
2426
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.
2432
2433 >>> value = b'\xf0\xf1\xf2'
2434 >>> value.hex('-')
2435 'f0-f1-f2'
2436 >>> value.hex('_', 2)
2437 'f0_f1f2'
2438 >>> b'UUDDLRLRAB'.hex(' ', -4)
2439 '55554444 4c524c52 4142'
2440
2441 .. versionadded:: 3.5
2442
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.
2446
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)
2451
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)``.
2455
2456 .. note::
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.
2465
2466
2467 .. _typebytearray:
2468
2469 Bytearray Objects
2470 -----------------
2471
2472 .. index:: object: bytearray
2473
2474 :class:`bytearray` objects are a mutable counterpart to :class:`bytes`
2475 objects.
2476
2477 .. class:: bytearray([source[, encoding[, errors]]])
2478
2479 There is no dedicated literal syntax for bytearray objects, instead
2480 they are always created by calling the constructor:
2481
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!')``
2486
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`.
2490
2491 Also see the :ref:`bytearray <func-bytearray>` built-in.
2492
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:
2496
2497 .. classmethod:: fromhex(string)
2498
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.
2502
2503 >>> bytearray.fromhex('2Ef0 F1f2 ')
2504 bytearray(b'.\xf0\xf1\xf2')
2505
2506 .. versionchanged:: 3.7
2507 :meth:`bytearray.fromhex` now skips all ASCII whitespace in the string,
2508 not just spaces.
2509
2510 A reverse conversion function exists to transform a bytearray object into its
2511 hexadecimal representation.
2512
2513 .. method:: hex()
2514
2515 Return a string object containing two hexadecimal digits for each
2516 byte in the instance.
2517
2518 >>> bytearray(b'\xf0\xf1\xf2').hex()
2519 'f0f1f2'
2520
2521 .. versionadded:: 3.5
2522
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)
2527
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)``.
2532
2533
2534 .. _bytes-methods:
2535
2536 Bytes and Bytearray Operations
2537 ------------------------------
2538
2539 .. index:: pair: bytes; methods
2540 pair: bytearray; methods
2541
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.
2547
2548 .. note::
2549
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::
2553
2554 a = "abc"
2555 b = a.replace("a", "f")
2556
2557 and::
2558
2559 a = b"abc"
2560 b = a.replace(b"a", b"f")
2561
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.
2565
2566 .. note::
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.
2569
2570 The following methods on bytes and bytearray objects can be used with
2571 arbitrary binary data.
2572
2573 .. method:: bytes.count(sub[, start[, end]])
2574 bytearray.count(sub[, start[, end]])
2575
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.
2579
2580 The subsequence to search for may be any :term:`bytes-like object` or an
2581 integer in the range 0 to 255.
2582
2583 .. versionchanged:: 3.3
2584 Also accept an integer in the range 0 to 255 as the subsequence.
2585
2586
2587 .. method:: bytes.decode(encoding="utf-8", errors="strict")
2588 bytearray.decode(encoding="utf-8", errors="strict")
2589
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`.
2597
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*.
2601
2602 .. note::
2603
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.
2607
2608 .. versionchanged:: 3.1
2609 Added support for keyword arguments.
2610
2611 .. versionchanged:: 3.9
2612 The *errors* is now checked in development mode and in debug mode.
2613
2614
2615 .. method:: bytes.endswith(suffix[, start[, end]])
2616 bytearray.endswith(suffix[, start[, end]])
2617
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.
2622
2623 The suffix(es) to search for may be any :term:`bytes-like object`.
2624
2625
2626 .. method:: bytes.find(sub[, start[, end]])
2627 bytearray.find(sub[, start[, end]])
2628
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.
2633
2634 The subsequence to search for may be any :term:`bytes-like object` or an
2635 integer in the range 0 to 255.
2636
2637 .. note::
2638
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::
2642
2643 >>> b'Py' in b'Python'
2644 True
2645
2646 .. versionchanged:: 3.3
2647 Also accept an integer in the range 0 to 255 as the subsequence.
2648
2649
2650 .. method:: bytes.index(sub[, start[, end]])
2651 bytearray.index(sub[, start[, end]])
2652
2653 Like :meth:`~bytes.find`, but raise :exc:`ValueError` when the
2654 subsequence is not found.
2655
2656 The subsequence to search for may be any :term:`bytes-like object` or an
2657 integer in the range 0 to 255.
2658
2659 .. versionchanged:: 3.3
2660 Also accept an integer in the range 0 to 255 as the subsequence.
2661
2662
2663 .. method:: bytes.join(iterable)
2664 bytearray.join(iterable)
2665
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.
2672
2673
2674 .. staticmethod:: bytes.maketrans(from, to)
2675 bytearray.maketrans(from, to)
2676
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.
2681
2682 .. versionadded:: 3.1
2683
2684
2685 .. method:: bytes.partition(sep)
2686 bytearray.partition(sep)
2687
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
2693 bytearray objects.
2694
2695 The separator to search for may be any :term:`bytes-like object`.
2696
2697
2698 .. method:: bytes.replace(old, new[, count])
2699 bytearray.replace(old, new[, count])
2700
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.
2704
2705 The subsequence to search for and its replacement may be any
2706 :term:`bytes-like object`.
2707
2708 .. note::
2709
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.
2712
2713
2714 .. method:: bytes.rfind(sub[, start[, end]])
2715 bytearray.rfind(sub[, start[, end]])
2716
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
2720 ``-1`` on failure.
2721
2722 The subsequence to search for may be any :term:`bytes-like object` or an
2723 integer in the range 0 to 255.
2724
2725 .. versionchanged:: 3.3
2726 Also accept an integer in the range 0 to 255 as the subsequence.
2727
2728
2729 .. method:: bytes.rindex(sub[, start[, end]])
2730 bytearray.rindex(sub[, start[, end]])
2731
2732 Like :meth:`~bytes.rfind` but raises :exc:`ValueError` when the
2733 subsequence *sub* is not found.
2734
2735 The subsequence to search for may be any :term:`bytes-like object` or an
2736 integer in the range 0 to 255.
2737
2738 .. versionchanged:: 3.3
2739 Also accept an integer in the range 0 to 255 as the subsequence.
2740
2741
2742 .. method:: bytes.rpartition(sep)
2743 bytearray.rpartition(sep)
2744
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
2750 original sequence.
2751
2752 The separator to search for may be any :term:`bytes-like object`.
2753
2754
2755 .. method:: bytes.startswith(prefix[, start[, end]])
2756 bytearray.startswith(prefix[, start[, end]])
2757
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.
2762
2763 The prefix(es) to search for may be any :term:`bytes-like object`.
2764
2765
2766 .. method:: bytes.translate(table, /, delete=b'')
2767 bytearray.translate(table, /, delete=b'')
2768
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.
2773
2774 You can use the :func:`bytes.maketrans` method to create a translation
2775 table.
2776
2777 Set the *table* argument to ``None`` for translations that only delete
2778 characters::
2779
2780 >>> b'read this short text'.translate(None, b'aeiou')
2781 b'rd ths shrt txt'
2782
2783 .. versionchanged:: 3.6
2784 *delete* is now supported as a keyword argument.
2785
2786
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.
2792
2793 .. method:: bytes.center(width[, fillbyte])
2794 bytearray.center(width[, fillbyte])
2795
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)``.
2800
2801 .. note::
2802
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.
2805
2806
2807 .. method:: bytes.ljust(width[, fillbyte])
2808 bytearray.ljust(width[, fillbyte])
2809
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)``.
2814
2815 .. note::
2816
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.
2819
2820
2821 .. method:: bytes.lstrip([chars])
2822 bytearray.lstrip([chars])
2823
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::
2830
2831 >>> b' spacious '.lstrip()
2832 b'spacious '
2833 >>> b'www.example.com'.lstrip(b'cmowz.')
2834 b'example.com'
2835
2836 The binary sequence of byte values to remove may be any
2837 :term:`bytes-like object`.
2838
2839 .. note::
2840
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.
2843
2844
2845 .. method:: bytes.rjust(width[, fillbyte])
2846 bytearray.rjust(width[, fillbyte])
2847
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)``.
2852
2853 .. note::
2854
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.
2857
2858
2859 .. method:: bytes.rsplit(sep=None, maxsplit=-1)
2860 bytearray.rsplit(sep=None, maxsplit=-1)
2861
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.
2868
2869
2870 .. method:: bytes.rstrip([chars])
2871 bytearray.rstrip([chars])
2872
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::
2879
2880 >>> b' spacious '.rstrip()
2881 b' spacious'
2882 >>> b'mississippi'.rstrip(b'ipz')
2883 b'mississ'
2884
2885 The binary sequence of byte values to remove may be any
2886 :term:`bytes-like object`.
2887
2888 .. note::
2889
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.
2892
2893
2894 .. method:: bytes.split(sep=None, maxsplit=-1)
2895 bytearray.split(sep=None, maxsplit=-1)
2896
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).
2902
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`.
2911
2912 For example::
2913
2914 >>> b'1,2,3'.split(b',')
2915 [b'1', b'2', b'3']
2916 >>> b'1,2,3'.split(b',', maxsplit=1)
2917 [b'1', b'2,3']
2918 >>> b'1,2,,3,'.split(b',')
2919 [b'1', b'2', b'', b'3', b'']
2920
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 ``[]``.
2927
2928 For example::
2929
2930
2931 >>> b'1 2 3'.split()
2932 [b'1', b'2', b'3']
2933 >>> b'1 2 3'.split(maxsplit=1)
2934 [b'1', b'2 3']
2935 >>> b' 1 2 3 '.split()
2936 [b'1', b'2', b'3']
2937
2938
2939 .. method:: bytes.strip([chars])
2940 bytearray.strip([chars])
2941
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
2948 stripped::
2949
2950 >>> b' spacious '.strip()
2951 b'spacious'
2952 >>> b'www.example.com'.strip(b'cmowz.')
2953 b'example'
2954
2955 The binary sequence of byte values to remove may be any
2956 :term:`bytes-like object`.
2957
2958 .. note::
2959
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.
2962
2963
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.
2968
2969 .. method:: bytes.capitalize()
2970 bytearray.capitalize()
2971
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.
2975
2976 .. note::
2977
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.
2980
2981
2982 .. method:: bytes.expandtabs(tabsize=8)
2983 bytearray.expandtabs(tabsize=8)
2984
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
2997 printed::
2998
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'
3003
3004 .. note::
3005
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.
3008
3009
3010 .. method:: bytes.isalnum()
3011 bytearray.isalnum()
3012
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'``.
3018
3019 For example::
3020
3021 >>> b'ABCabc1'.isalnum()
3022 True
3023 >>> b'ABC abc1'.isalnum()
3024 False
3025
3026
3027 .. method:: bytes.isalpha()
3028 bytearray.isalpha()
3029
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'``.
3034
3035 For example::
3036
3037 >>> b'ABCabc'.isalpha()
3038 True
3039 >>> b'ABCabc1'.isalpha()
3040 False
3041
3042
3043 .. method:: bytes.isascii()
3044 bytearray.isascii()
3045
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.
3049
3050 .. versionadded:: 3.7
3051
3052
3053 .. method:: bytes.isdigit()
3054 bytearray.isdigit()
3055
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'``.
3059
3060 For example::
3061
3062 >>> b'1234'.isdigit()
3063 True
3064 >>> b'1.23'.isdigit()
3065 False
3066
3067
3068 .. method:: bytes.islower()
3069 bytearray.islower()
3070
3071 Return ``True`` if there is at least one lowercase ASCII character
3072 in the sequence and no uppercase ASCII characters, ``False`` otherwise.
3073
3074 For example::
3075
3076 >>> b'hello world'.islower()
3077 True
3078 >>> b'Hello world'.islower()
3079 False
3080
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'``.
3084
3085
3086 .. method:: bytes.isspace()
3087 bytearray.isspace()
3088
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).
3093
3094
3095 .. method:: bytes.istitle()
3096 bytearray.istitle()
3097
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".
3101
3102 For example::
3103
3104 >>> b'Hello World'.istitle()
3105 True
3106 >>> b'Hello world'.istitle()
3107 False
3108
3109
3110 .. method:: bytes.isupper()
3111 bytearray.isupper()
3112
3113 Return ``True`` if there is at least one uppercase alphabetic ASCII character
3114 in the sequence and no lowercase ASCII characters, ``False`` otherwise.
3115
3116 For example::
3117
3118 >>> b'HELLO WORLD'.isupper()
3119 True
3120 >>> b'Hello world'.isupper()
3121 False
3122
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'``.
3126
3127
3128 .. method:: bytes.lower()
3129 bytearray.lower()
3130
3131 Return a copy of the sequence with all the uppercase ASCII characters
3132 converted to their corresponding lowercase counterpart.
3133
3134 For example::
3135
3136 >>> b'Hello World'.lower()
3137 b'hello world'
3138
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'``.
3142
3143 .. note::
3144
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.
3147
3148
3149 .. index::
3150 single: universal newlines; bytes.splitlines method
3151 single: universal newlines; bytearray.splitlines method
3152
3153 .. method:: bytes.splitlines(keepends=False)
3154 bytearray.splitlines(keepends=False)
3155
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.
3160
3161 For example::
3162
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']
3167
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::
3171
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()
3175 ([], [b'One line'])
3176
3177
3178 .. method:: bytes.swapcase()
3179 bytearray.swapcase()
3180
3181 Return a copy of the sequence with all the lowercase ASCII characters
3182 converted to their corresponding uppercase counterpart and vice-versa.
3183
3184 For example::
3185
3186 >>> b'Hello World'.swapcase()
3187 b'hELLO wORLD'
3188
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'``.
3192
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.
3197
3198 .. note::
3199
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.
3202
3203
3204 .. method:: bytes.title()
3205 bytearray.title()
3206
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.
3210
3211 For example::
3212
3213 >>> b'Hello world'.title()
3214 b'Hello World'
3215
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.
3220
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::
3225
3226 >>> b"they're bill's friends from the UK".title()
3227 b"They'Re Bill'S Friends From The Uk"
3228
3229 A workaround for apostrophes can be constructed using regular expressions::
3230
3231 >>> import re
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(),
3236 ... s)
3237 ...
3238 >>> titlecase(b"they're bill's friends.")
3239 b"They're Bill's Friends."
3240
3241 .. note::
3242
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.
3245
3246
3247 .. method:: bytes.upper()
3248 bytearray.upper()
3249
3250 Return a copy of the sequence with all the lowercase ASCII characters
3251 converted to their corresponding uppercase counterpart.
3252
3253 For example::
3254
3255 >>> b'Hello World'.upper()
3256 b'HELLO WORLD'
3257
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'``.
3261
3262 .. note::
3263
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.
3266
3267
3268 .. method:: bytes.zfill(width)
3269 bytearray.zfill(width)
3270
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)``.
3276
3277 For example::
3278
3279 >>> b"42".zfill(5)
3280 b'00042'
3281 >>> b"-42".zfill(5)
3282 b'-0042'
3283
3284 .. note::
3285
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.
3288
3289
3290 .. _bytes-formatting:
3291
3292 ``printf``-style Bytes Formatting
3293 ----------------------------------
3294
3295 .. index::
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
3307
3308 .. note::
3309
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.
3314
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.
3321
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).
3326
3327 .. index::
3328 single: () (parentheses); in printf-style formatting
3329 single: * (asterisk); in printf-style formatting
3330 single: . (dot); in printf-style formatting
3331
3332 A conversion specifier contains two or more characters and has the following
3333 components, which must occur in this order:
3334
3335 #. The ``'%'`` character, which marks the start of the specifier.
3336
3337 #. Mapping key (optional), consisting of a parenthesised sequence of characters
3338 (for example, ``(somename)``).
3339
3340 #. Conversion flags (optional), which affect the result of some conversion
3341 types.
3342
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.
3346
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
3350 precision.
3351
3352 #. Length modifier (optional).
3353
3354 #. Conversion type.
3355
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:
3360
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.'
3364
3365 In this case no ``*`` specifiers may occur in a format (since they require a
3366 sequential parameter list).
3367
3368 The conversion flag characters are:
3369
3370 .. index::
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
3375
3376 +---------+---------------------------------------------------------------------+
3377 | Flag | Meaning |
3378 +=========+=====================================================================+
3379 | ``'#'`` | The value conversion will use the "alternate form" (where defined |
3380 | | below). |
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 +---------+---------------------------------------------------------------------+
3393
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``.
3396
3397 The conversion types are:
3398
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 +------------+-----------------------------------------------------+-------+
3449
3450 Notes:
3451
3452 (1)
3453 The alternate form causes a leading octal specifier (``'0o'``) to be
3454 inserted before the first digit.
3455
3456 (2)
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.
3459
3460 (3)
3461 The alternate form causes the result to always contain a decimal point, even if
3462 no digits follow it.
3463
3464 The precision determines the number of digits after the decimal point and
3465 defaults to 6.
3466
3467 (4)
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.
3470
3471 The precision determines the number of significant digits before and after the
3472 decimal point and defaults to 6.
3473
3474 (5)
3475 If precision is ``N``, the output is truncated to ``N`` characters.
3476
3477 (6)
3478 ``b'%s'`` is deprecated, but will not be removed during the 3.x series.
3479
3480 (7)
3481 ``b'%r'`` is deprecated, but will not be removed during the 3.x series.
3482
3483 (8)
3484 See :pep:`237`.
3485
3486 .. note::
3487
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.
3490
3491 .. seealso::
3492
3493 :pep:`461` - Adding % formatting to bytes and bytearray
3494
3495 .. versionadded:: 3.5
3496
3497 .. _typememoryview:
3498
3499 Memory Views
3500 ------------
3501
3502 :class:`memoryview` objects allow Python code to access the internal data
3503 of an object that supports the :ref:`buffer protocol <bufferobjects>` without
3504 copying.
3505
3506 .. class:: memoryview(obj)
3507
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`.
3511
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
3516 bigger elements.
3517
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.
3524
3525 A :class:`memoryview` supports slicing and indexing to expose its data.
3526 One-dimensional slicing will result in a subview::
3527
3528 >>> v = memoryview(b'abcefg')
3529 >>> v[1]
3530 98
3531 >>> v[-1]
3532 103
3533 >>> v[1:4]
3534 <memory at 0x7f3ddc9f4350>
3535 >>> bytes(v[1:4])
3536 b'bce'
3537
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.
3546
3547 Here is an example with a non-byte format::
3548
3549 >>> import array
3550 >>> a = array.array('l', [-11111111, 22222222, -33333333, 44444444])
3551 >>> m = memoryview(a)
3552 >>> m[0]
3553 -11111111
3554 >>> m[-1]
3555 44444444
3556 >>> m[::2].tolist()
3557 [-11111111, -33333333]
3558
3559 If the underlying object is writable, the memoryview supports
3560 one-dimensional slice assignment. Resizing is not allowed::
3561
3562 >>> data = bytearray(b'abcefg')
3563 >>> v = memoryview(data)
3564 >>> v.readonly
3565 False
3566 >>> v[0] = ord(b'z')
3567 >>> data
3568 bytearray(b'zbcefg')
3569 >>> v[1:4] = b'123'
3570 >>> data
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'
3577 >>> data
3578 bytearray(b'z1spam')
3579
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())``::
3583
3584 >>> v = memoryview(b'abcefg')
3585 >>> hash(v) == hash(b'abcefg')
3586 True
3587 >>> hash(v[2:4]) == hash(b'ce')
3588 True
3589 >>> hash(v[::-2]) == hash(b'abcefg'[::-2])
3590 True
3591
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.
3595
3596 .. versionchanged:: 3.4
3597 memoryview is now registered automatically with
3598 :class:`collections.abc.Sequence`
3599
3600 .. versionchanged:: 3.5
3601 memoryviews can now be indexed with tuple of integers.
3602
3603 :class:`memoryview` has several methods:
3604
3605 .. method:: __eq__(exporter)
3606
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.
3610
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()``::
3613
3614 >>> import array
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
3621 True
3622 >>> x.tolist() == a.tolist() == y.tolist() == b.tolist()
3623 True
3624 >>> z = y[::-2]
3625 >>> z == c
3626 True
3627 >>> z.tolist() == c.tolist()
3628 True
3629
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)::
3633
3634 >>> from ctypes import BigEndianStructure, c_long
3635 >>> class BEPoint(BigEndianStructure):
3636 ... _fields_ = [("x", c_long), ("y", c_long)]
3637 ...
3638 >>> point = BEPoint(100, 200)
3639 >>> a = memoryview(point)
3640 >>> b = memoryview(point)
3641 >>> a == point
3642 False
3643 >>> a == b
3644 False
3645
3646 Note that, as with floating point numbers, ``v is w`` does *not* imply
3647 ``v == w`` for memoryview objects.
3648
3649 .. versionchanged:: 3.3
3650 Previous versions compared the raw memory disregarding the item format
3651 and the logical array structure.
3652
3653 .. method:: tobytes(order=None)
3654
3655 Return the data in the buffer as a bytestring. This is equivalent to
3656 calling the :class:`bytes` constructor on the memoryview. ::
3657
3658 >>> m = memoryview(b"abc")
3659 >>> m.tobytes()
3660 b'abc'
3661 >>> bytes(m)
3662 b'abc'
3663
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.
3668
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'*.
3675
3676 .. method:: hex()
3677
3678 Return a string object containing two hexadecimal digits for each
3679 byte in the buffer. ::
3680
3681 >>> m = memoryview(b"abc")
3682 >>> m.hex()
3683 '616263'
3684
3685 .. versionadded:: 3.5
3686
3687 .. method:: tolist()
3688
3689 Return the data in the buffer as a list of elements. ::
3690
3691 >>> memoryview(b'abc').tolist()
3692 [97, 98, 99]
3693 >>> import array
3694 >>> a = array.array('d', [1.1, 2.2, 3.3])
3695 >>> m = memoryview(a)
3696 >>> m.tolist()
3697 [1.1, 2.2, 3.3]
3698
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
3702 representations.
3703
3704 .. method:: toreadonly()
3705
3706 Return a readonly version of the memoryview object. The original
3707 memoryview object is unchanged. ::
3708
3709 >>> m = memoryview(bytearray(b'abc'))
3710 >>> mm = m.toreadonly()
3711 >>> mm.tolist()
3712 [89, 98, 99]
3713 >>> mm[0] = 42
3714 Traceback (most recent call last):
3715 File "<stdin>", line 1, in <module>
3716 TypeError: cannot modify read-only memory
3717 >>> m[0] = 43
3718 >>> mm.tolist()
3719 [43, 98, 99]
3720
3721 .. versionadded:: 3.8
3722
3723 .. method:: release()
3724
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.
3730
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)::
3734
3735 >>> m = memoryview(b'abc')
3736 >>> m.release()
3737 >>> m[0]
3738 Traceback (most recent call last):
3739 File "<stdin>", line 1, in <module>
3740 ValueError: operation forbidden on released memoryview object
3741
3742 The context management protocol can be used for a similar effect,
3743 using the ``with`` statement::
3744
3745 >>> with memoryview(b'abc') as m:
3746 ... m[0]
3747 ...
3748 97
3749 >>> m[0]
3750 Traceback (most recent call last):
3751 File "<stdin>", line 1, in <module>
3752 ValueError: operation forbidden on released memoryview object
3753
3754 .. versionadded:: 3.2
3755
3756 .. method:: cast(format[, shape])
3757
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.
3763
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.
3768
3769 Cast 1D/long to 1D/unsigned bytes::
3770
3771 >>> import array
3772 >>> a = array.array('l', [1,2,3])
3773 >>> x = memoryview(a)
3774 >>> x.format
3775 'l'
3776 >>> x.itemsize
3777 8
3778 >>> len(x)
3779 3
3780 >>> x.nbytes
3781 24
3782 >>> y = x.cast('B')
3783 >>> y.format
3784 'B'
3785 >>> y.itemsize
3786 1
3787 >>> len(y)
3788 24
3789 >>> y.nbytes
3790 24
3791
3792 Cast 1D/unsigned bytes to 1D/char::
3793
3794 >>> b = bytearray(b'zyz')
3795 >>> x = memoryview(b)
3796 >>> x[0] = b'a'
3797 Traceback (most recent call last):
3798 File "<stdin>", line 1, in <module>
3799 ValueError: memoryview: invalid value for format "B"
3800 >>> y = x.cast('c')
3801 >>> y[0] = b'a'
3802 >>> b
3803 bytearray(b'ayz')
3804
3805 Cast 1D/bytes to 3D/ints to 1D/signed char::
3806
3807 >>> import struct
3808 >>> buf = struct.pack("i"*12, *list(range(12)))
3809 >>> x = memoryview(buf)
3810 >>> y = x.cast('i', shape=[2,2,3])
3811 >>> y.tolist()
3812 [[[0, 1, 2], [3, 4, 5]], [[6, 7, 8], [9, 10, 11]]]
3813 >>> y.format
3814 'i'
3815 >>> y.itemsize
3816 4
3817 >>> len(y)
3818 2
3819 >>> y.nbytes
3820 48
3821 >>> z = y.cast('b')
3822 >>> z.format
3823 'b'
3824 >>> z.itemsize
3825 1
3826 >>> len(z)
3827 48
3828 >>> z.nbytes
3829 48
3830
3831 Cast 1D/unsigned long to 2D/unsigned long::
3832
3833 >>> buf = struct.pack("L"*6, *list(range(6)))
3834 >>> x = memoryview(buf)
3835 >>> y = x.cast('L', shape=[2,3])
3836 >>> len(y)
3837 2
3838 >>> y.nbytes
3839 48
3840 >>> y.tolist()
3841 [[0, 1, 2], [3, 4, 5]]
3842
3843 .. versionadded:: 3.3
3844
3845 .. versionchanged:: 3.5
3846 The source format is no longer restricted when casting to a byte view.
3847
3848 There are also several readonly attributes available:
3849
3850 .. attribute:: obj
3851
3852 The underlying object of the memoryview::
3853
3854 >>> b = bytearray(b'xyz')
3855 >>> m = memoryview(b)
3856 >>> m.obj is b
3857 True
3858
3859 .. versionadded:: 3.3
3860
3861 .. attribute:: nbytes
3862
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)``::
3866
3867 >>> import array
3868 >>> a = array.array('i', [1,2,3,4,5])
3869 >>> m = memoryview(a)
3870 >>> len(m)
3871 5
3872 >>> m.nbytes
3873 20
3874 >>> y = m[::2]
3875 >>> len(y)
3876 3
3877 >>> y.nbytes
3878 12
3879 >>> len(y.tobytes())
3880 12
3881
3882 Multi-dimensional arrays::
3883
3884 >>> import struct
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])
3888 >>> y.tolist()
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]]
3890 >>> len(y)
3891 3
3892 >>> y.nbytes
3893 96
3894
3895 .. versionadded:: 3.3
3896
3897 .. attribute:: readonly
3898
3899 A bool indicating whether the memory is read only.
3900
3901 .. attribute:: format
3902
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.
3907
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``.
3911
3912 .. attribute:: itemsize
3913
3914 The size in bytes of each element of the memoryview::
3915
3916 >>> import array, struct
3917 >>> m = memoryview(array.array('H', [32000, 32001, 32002]))
3918 >>> m.itemsize
3919 2
3920 >>> m[0]
3921 32000
3922 >>> struct.calcsize('H') == m.itemsize
3923 True
3924
3925 .. attribute:: ndim
3926
3927 An integer indicating how many dimensions of a multi-dimensional array the
3928 memory represents.
3929
3930 .. attribute:: shape
3931
3932 A tuple of integers the length of :attr:`ndim` giving the shape of the
3933 memory as an N-dimensional array.
3934
3935 .. versionchanged:: 3.3
3936 An empty tuple instead of ``None`` when ndim = 0.
3937
3938 .. attribute:: strides
3939
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.
3942
3943 .. versionchanged:: 3.3
3944 An empty tuple instead of ``None`` when ndim = 0.
3945
3946 .. attribute:: suboffsets
3947
3948 Used internally for PIL-style arrays. The value is informational only.
3949
3950 .. attribute:: c_contiguous
3951
3952 A bool indicating whether the memory is C-:term:`contiguous`.
3953
3954 .. versionadded:: 3.3
3955
3956 .. attribute:: f_contiguous
3957
3958 A bool indicating whether the memory is Fortran :term:`contiguous`.
3959
3960 .. versionadded:: 3.3
3961
3962 .. attribute:: contiguous
3963
3964 A bool indicating whether the memory is :term:`contiguous`.
3965
3966 .. versionadded:: 3.3
3967
3968
3969 .. _types-set:
3970
3971 Set Types --- :class:`set`, :class:`frozenset`
3972 ==============================================
3973
3974 .. index:: object: set
3975
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.)
3982
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.
3987
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.
3995
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.
3999
4000 The constructors for both classes work the same:
4001
4002 .. class:: set([iterable])
4003 frozenset([iterable])
4004
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
4009 returned.
4010
4011 Instances of :class:`set` and :class:`frozenset` provide the following
4012 operations:
4013
4014 .. describe:: len(s)
4015
4016 Return the number of elements in set *s* (cardinality of *s*).
4017
4018 .. describe:: x in s
4019
4020 Test *x* for membership in *s*.
4021
4022 .. describe:: x not in s
4023
4024 Test *x* for non-membership in *s*.
4025
4026 .. method:: isdisjoint(other)
4027
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.
4030
4031 .. method:: issubset(other)
4032 set <= other
4033
4034 Test whether every element in the set is in *other*.
4035
4036 .. method:: set < other
4037
4038 Test whether the set is a proper subset of *other*, that is,
4039 ``set <= other and set != other``.
4040
4041 .. method:: issuperset(other)
4042 set >= other
4043
4044 Test whether every element in *other* is in the set.
4045
4046 .. method:: set > other
4047
4048 Test whether the set is a proper superset of *other*, that is, ``set >=
4049 other and set != other``.
4050
4051 .. method:: union(*others)
4052 set | other | ...
4053
4054 Return a new set with elements from the set and all others.
4055
4056 .. method:: intersection(*others)
4057 set & other & ...
4058
4059 Return a new set with elements common to the set and all others.
4060
4061 .. method:: difference(*others)
4062 set - other - ...
4063
4064 Return a new set with elements in the set that are not in the others.
4065
4066 .. method:: symmetric_difference(other)
4067 set ^ other
4068
4069 Return a new set with elements in either the set or *other* but not both.
4070
4071 .. method:: copy()
4072
4073 Return a shallow copy of the set.
4074
4075
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')``.
4082
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).
4089
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')])``.
4093
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``.
4098
4099 Since sets only define partial ordering (subset relationships), the output of
4100 the :meth:`list.sort` method is undefined for lists of sets.
4101
4102 Set elements, like dictionary keys, must be :term:`hashable`.
4103
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`.
4107
4108 The following table lists operations available for :class:`set` that do not
4109 apply to immutable instances of :class:`frozenset`:
4110
4111 .. method:: update(*others)
4112 set |= other | ...
4113
4114 Update the set, adding elements from all others.
4115
4116 .. method:: intersection_update(*others)
4117 set &= other & ...
4118
4119 Update the set, keeping only elements found in it and all others.
4120
4121 .. method:: difference_update(*others)
4122 set -= other | ...
4123
4124 Update the set, removing elements found in others.
4125
4126 .. method:: symmetric_difference_update(other)
4127 set ^= other
4128
4129 Update the set, keeping only elements found in either set, but not in both.
4130
4131 .. method:: add(elem)
4132
4133 Add element *elem* to the set.
4134
4135 .. method:: remove(elem)
4136
4137 Remove element *elem* from the set. Raises :exc:`KeyError` if *elem* is
4138 not contained in the set.
4139
4140 .. method:: discard(elem)
4141
4142 Remove element *elem* from the set if it is present.
4143
4144 .. method:: pop()
4145
4146 Remove and return an arbitrary element from the set. Raises
4147 :exc:`KeyError` if the set is empty.
4148
4149 .. method:: clear()
4150
4151 Remove all elements from the set.
4152
4153
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
4157 argument.
4158
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*.
4162
4163
4164 .. _typesmapping:
4165
4166 Mapping Types --- :class:`dict`
4167 ===============================
4168
4169 .. index::
4170 object: mapping
4171 object: dictionary
4172 triple: operations on; mapping; types
4173 triple: operations on; dictionary; type
4174 statement: del
4175 builtin: len
4176
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.)
4182
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.)
4191
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.
4195
4196 .. class:: dict(**kwarg)
4197 dict(mapping, **kwarg)
4198 dict(iterable, **kwarg)
4199
4200 Return a new dictionary initialized from an optional positional argument
4201 and a possibly empty set of keyword arguments.
4202
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
4211 dictionary.
4212
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.
4217
4218 To illustrate, the following examples all return a dictionary equal to
4219 ``{"one": 1, "two": 2, "three": 3}``::
4220
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
4228 True
4229
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.
4232
4233
4234 These are the operations that dictionaries support (and therefore, custom
4235 mapping types should support too):
4236
4237 .. describe:: list(d)
4238
4239 Return a list of all the keys used in the dictionary *d*.
4240
4241 .. describe:: len(d)
4242
4243 Return the number of items in the dictionary *d*.
4244
4245 .. describe:: d[key]
4246
4247 Return the item of *d* with key *key*. Raises a :exc:`KeyError` if *key* is
4248 not in the map.
4249
4250 .. index:: __missing__()
4251
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::
4259
4260 >>> class Counter(dict):
4261 ... def __missing__(self, key):
4262 ... return 0
4263 >>> c = Counter()
4264 >>> c['red']
4265 0
4266 >>> c['red'] += 1
4267 >>> c['red']
4268 1
4269
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`.
4273
4274 .. describe:: d[key] = value
4275
4276 Set ``d[key]`` to *value*.
4277
4278 .. describe:: del d[key]
4279
4280 Remove ``d[key]`` from *d*. Raises a :exc:`KeyError` if *key* is not in the
4281 map.
4282
4283 .. describe:: key in d
4284
4285 Return ``True`` if *d* has a key *key*, else ``False``.
4286
4287 .. describe:: key not in d
4288
4289 Equivalent to ``not key in d``.
4290
4291 .. describe:: iter(d)
4292
4293 Return an iterator over the keys of the dictionary. This is a shortcut
4294 for ``iter(d.keys())``.
4295
4296 .. method:: clear()
4297
4298 Remove all items from the dictionary.
4299
4300 .. method:: copy()
4301
4302 Return a shallow copy of the dictionary.
4303
4304 .. classmethod:: fromkeys(iterable[, value])
4305
4306 Create a new dictionary with keys from *iterable* and values set to *value*.
4307
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.
4313
4314 .. method:: get(key[, default])
4315
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`.
4319
4320 .. method:: items()
4321
4322 Return a new view of the dictionary's items (``(key, value)`` pairs).
4323 See the :ref:`documentation of view objects <dict-views>`.
4324
4325 .. method:: keys()
4326
4327 Return a new view of the dictionary's keys. See the :ref:`documentation
4328 of view objects <dict-views>`.
4329
4330 .. method:: pop(key[, default])
4331
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.
4335
4336 .. method:: popitem()
4337
4338 Remove and return a ``(key, value)`` pair from the dictionary.
4339 Pairs are returned in :abbr:`LIFO (last-in, first-out)` order.
4340
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`.
4344
4345 .. versionchanged:: 3.7
4346 LIFO order is now guaranteed. In prior versions, :meth:`popitem` would
4347 return an arbitrary key/value pair.
4348
4349 .. describe:: reversed(d)
4350
4351 Return a reverse iterator over the keys of the dictionary. This is a
4352 shortcut for ``reversed(d.keys())``.
4353
4354 .. method:: setdefault(key[, default])
4355
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
4358 ``None``.
4359
4360 .. method:: update([other])
4361
4362 Update the dictionary with the key/value pairs from *other*, overwriting
4363 existing keys. Return ``None``.
4364
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)``.
4369
4370 .. method:: values()
4371
4372 Return a new view of the dictionary's values. See the
4373 :ref:`documentation of view objects <dict-views>`.
4374
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::
4378
4379 >>> d = {'a': 1}
4380 >>> d.values() == d.values()
4381 False
4382
4383 Dictionaries compare equal if and only if they have the same ``(key,
4384 value)`` pairs (regardless of ordering). Order comparisons ('<', '<=', '>=', '>') raise
4385 :exc:`TypeError`.
4386
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. ::
4389
4390 >>> d = {"one": 1, "two": 2, "three": 3, "four": 4}
4391 >>> d
4392 {'one': 1, 'two': 2, 'three': 3, 'four': 4}
4393 >>> list(d)
4394 ['one', 'two', 'three', 'four']
4395 >>> list(d.values())
4396 [1, 2, 3, 4]
4397 >>> d["one"] = 42
4398 >>> d
4399 {'one': 42, 'two': 2, 'three': 3, 'four': 4}
4400 >>> del d["two"]
4401 >>> d["two"] = None
4402 >>> d
4403 {'one': 42, 'three': 3, 'four': 4, 'two': None}
4404
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.
4408
4409 Dictionaries and dictionary views are reversible. ::
4410
4411 >>> d = {"one": 1, "two": 2, "three": 3, "four": 4}
4412 >>> d
4413 {'one': 1, 'two': 2, 'three': 3, 'four': 4}
4414 >>> list(reversed(d))
4415 ['four', 'three', 'two', 'one']
4416 >>> list(reversed(d.values()))
4417 [4, 3, 2, 1]
4418 >>> list(reversed(d.items()))
4419 [('four', 4), ('three', 3), ('two', 2), ('one', 1)]
4420
4421 .. versionchanged:: 3.8
4422 Dictionaries are now reversible.
4423
4424
4425 .. seealso::
4426 :class:`types.MappingProxyType` can be used to create a read-only view
4427 of a :class:`dict`.
4428
4429
4430 .. _dict-views:
4431
4432 Dictionary view objects
4433 -----------------------
4434
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.
4439
4440 Dictionary views can be iterated over to yield their respective data, and
4441 support membership tests:
4442
4443 .. describe:: len(dictview)
4444
4445 Return the number of entries in the dictionary.
4446
4447 .. describe:: iter(dictview)
4448
4449 Return an iterator over the keys, values or items (represented as tuples of
4450 ``(key, value)``) in the dictionary.
4451
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()]``.
4456
4457 Iterating views while adding or deleting entries in the dictionary may raise
4458 a :exc:`RuntimeError` or fail to iterate over all entries.
4459
4460 .. versionchanged:: 3.7
4461 Dictionary order is guaranteed to be insertion order.
4462
4463 .. describe:: x in dictview
4464
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).
4467
4468 .. describe:: reversed(dictview)
4469
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.
4472
4473 .. versionchanged:: 3.8
4474 Dictionary views are now reversible.
4475
4476
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 ``^``).
4483
4484 An example of dictionary view usage::
4485
4486 >>> dishes = {'eggs': 2, 'sausage': 1, 'bacon': 1, 'spam': 500}
4487 >>> keys = dishes.keys()
4488 >>> values = dishes.values()
4489
4490 >>> # iteration
4491 >>> n = 0
4492 >>> for val in values:
4493 ... n += val
4494 >>> print(n)
4495 504
4496
4497 >>> # keys and values are iterated over in the same order (insertion order)
4498 >>> list(keys)
4499 ['eggs', 'sausage', 'bacon', 'spam']
4500 >>> list(values)
4501 [2, 1, 1, 500]
4502
4503 >>> # view objects are dynamic and reflect dict changes
4504 >>> del dishes['eggs']
4505 >>> del dishes['sausage']
4506 >>> list(keys)
4507 ['bacon', 'spam']
4508
4509 >>> # set operations
4510 >>> keys & {'eggs', 'bacon', 'salad'}
4511 {'bacon'}
4512 >>> keys ^ {'sausage', 'juice'}
4513 {'juice', 'sausage', 'bacon', 'spam'}
4514
4515
4516 .. _typecontextmanager:
4517
4518 Context Manager Types
4519 =====================
4520
4521 .. index::
4522 single: context manager
4523 single: context management protocol
4524 single: protocol; context management
4525
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:
4530
4531
4532 .. method:: contextmanager.__enter__()
4533
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.
4538
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.
4542
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.
4549
4550
4551 .. method:: contextmanager.__exit__(exc_type, exc_val, exc_tb)
4552
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``.
4557
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.
4564
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.
4570
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.
4576
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
4582 generator function.
4583
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.
4589
4590
4591 .. _typesother:
4592
4593 Other Built-in Types
4594 ====================
4595
4596 The interpreter supports several other kinds of objects. Most of these support
4597 only one or two operations.
4598
4599
4600 .. _typesmodules:
4601
4602 Modules
4603 -------
4604
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.)
4611
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
4618 not recommended.
4619
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'>``.
4623
4624
4625 .. _typesobjects:
4626
4627 Classes and Class Instances
4628 ---------------------------
4629
4630 See :ref:`objects` and :ref:`class` for these.
4631
4632
4633 .. _typesfunctions:
4634
4635 Functions
4636 ---------
4637
4638 Function objects are created by function definitions. The only operation on a
4639 function object is to call it: ``func(argument-list)``.
4640
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.
4644
4645 See :ref:`function` for more information.
4646
4647
4648 .. _typesmethods:
4649
4650 Methods
4651 -------
4652
4653 .. index:: object: method
4654
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
4658 them.
4659
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, ...,
4667 arg-n)``.
4668
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::
4675
4676 >>> class C:
4677 ... def method(self):
4678 ... pass
4679 ...
4680 >>> c = C()
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'
4686 >>> c.method.whoami
4687 'my name is method'
4688
4689 See :ref:`types` for more information.
4690
4691
4692 .. index:: object; code, code object
4693
4694 .. _bltin-code-objects:
4695
4696 Code Objects
4697 ------------
4698
4699 .. index::
4700 builtin: compile
4701 single: __code__ (function object attribute)
4702
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.
4709
4710 .. index::
4711 builtin: exec
4712 builtin: eval
4713
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.
4716
4717 See :ref:`types` for more information.
4718
4719
4720 .. _bltin-type-objects:
4721
4722 Type Objects
4723 ------------
4724
4725 .. index::
4726 builtin: type
4727 module: types
4728
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
4732 types.
4733
4734 Types are written like this: ``<class 'int'>``.
4735
4736
4737 .. _bltin-null-object:
4738
4739 The Null Object
4740 ---------------
4741
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.
4745
4746 It is written as ``None``.
4747
4748
4749 .. index:: single: ...; ellipsis literal
4750 .. _bltin-ellipsis-object:
4751
4752 The Ellipsis Object
4753 -------------------
4754
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.
4759
4760 It is written as ``Ellipsis`` or ``...``.
4761
4762
4763 .. _bltin-notimplemented-object:
4764
4765 The NotImplemented Object
4766 -------------------------
4767
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.
4772
4773 It is written as ``NotImplemented``.
4774
4775
4776 .. _bltin-boolean-values:
4777
4778 Boolean Values
4779 --------------
4780
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).
4788
4789 .. index::
4790 single: False
4791 single: True
4792 pair: Boolean; values
4793
4794 They are written as ``False`` and ``True``, respectively.
4795
4796
4797 .. _typesinternal:
4798
4799 Internal Objects
4800 ----------------
4801
4802 See :ref:`types` for this information. It describes stack frame objects,
4803 traceback objects, and slice objects.
4804
4805
4806 .. _specialattrs:
4807
4808 Special Attributes
4809 ==================
4810
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.
4814
4815
4816 .. attribute:: object.__dict__
4817
4818 A dictionary or other mapping object used to store an object's (writable)
4819 attributes.
4820
4821
4822 .. attribute:: instance.__class__
4823
4824 The class to which a class instance belongs.
4825
4826
4827 .. attribute:: class.__bases__
4828
4829 The tuple of base classes of a class object.
4830
4831
4832 .. attribute:: definition.__name__
4833
4834 The name of the class, function, method, descriptor, or
4835 generator instance.
4836
4837
4838 .. attribute:: definition.__qualname__
4839
4840 The :term:`qualified name` of the class, function, method, descriptor,
4841 or generator instance.
4842
4843 .. versionadded:: 3.3
4844
4845
4846 .. attribute:: class.__mro__
4847
4848 This attribute is a tuple of classes that are considered when looking for
4849 base classes during method resolution.
4850
4851
4852 .. method:: class.mro()
4853
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__`.
4857
4858
4859 .. method:: class.__subclasses__
4860
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.
4863 Example::
4864
4865 >>> int.__subclasses__()
4866 [<class 'bool'>]
4867
4868
4869 .. rubric:: Footnotes
4870
4871 .. [1] Additional information on these special methods may be found in the Python
4872 Reference Manual (:ref:`customization`).
4873
4874 .. [2] As a consequence, the list ``[1, 2]`` is considered equal to ``[1.0, 2.0]``, and
4875 similarly for tuples.
4876
4877 .. [3] They must have since the parser can't tell the type of the operands.
4878
4879 .. [4] Cased characters are those with general category property being one of
4880 "Lu" (Letter, uppercase), "Ll" (Letter, lowercase), or "Lt" (Letter, titlecase).
4881
4882 .. [5] To format only a tuple you should therefore provide a singleton tuple whose only
4883 element is the tuple to be formatted.