1.. XXX: reference/datamodel and this have quite a few overlaps! 2 3 4.. _bltin-types: 5 6************** 7Built-in Types 8************** 9 10The following sections describe the standard types that are built into the 11interpreter. 12 13.. index:: pair: built-in; types 14 15The principal built-in types are numerics, sequences, mappings, classes, 16instances and exceptions. 17 18Some collection classes are mutable. The methods that add, subtract, or 19rearrange their members in place, and don't return a specific item, never return 20the collection instance itself but ``None``. 21 22Some operations are supported by several object types; in particular, 23practically all objects can be compared for equality, tested for truth 24value, and converted to a string (with the :func:`repr` function or the 25slightly different :func:`str` function). The latter function is implicitly 26used when an object is written by the :func:`print` function. 27 28 29.. _truth: 30 31Truth Value Testing 32=================== 33 34.. index:: 35 statement: if 36 statement: while 37 pair: truth; value 38 pair: Boolean; operations 39 single: false 40 41Any object can be tested for truth value, for use in an :keyword:`if` or 42:keyword:`while` condition or as operand of the Boolean operations below. 43 44.. index:: single: true 45 46By default, an object is considered true unless its class defines either a 47:meth:`__bool__` method that returns ``False`` or a :meth:`__len__` method that 48returns zero, when called with the object. [1]_ Here are most of the built-in 49objects considered false: 50 51 .. index:: 52 single: None (Built-in object) 53 single: False (Built-in object) 54 55* constants defined to be false: ``None`` and ``False``. 56 57* zero of any numeric type: ``0``, ``0.0``, ``0j``, ``Decimal(0)``, 58 ``Fraction(0, 1)`` 59 60* empty sequences and collections: ``''``, ``()``, ``[]``, ``{}``, ``set()``, 61 ``range(0)`` 62 63.. index:: 64 operator: or 65 operator: and 66 single: False 67 single: True 68 69Operations and built-in functions that have a Boolean result always return ``0`` 70or ``False`` for false and ``1`` or ``True`` for true, unless otherwise stated. 71(Important exception: the Boolean operations ``or`` and ``and`` always return 72one of their operands.) 73 74 75.. _boolean: 76 77Boolean Operations --- :keyword:`!and`, :keyword:`!or`, :keyword:`!not` 78======================================================================= 79 80.. index:: pair: Boolean; operations 81 82These are the Boolean operations, ordered by ascending priority: 83 84+-------------+---------------------------------+-------+ 85| Operation | Result | Notes | 86+=============+=================================+=======+ 87| ``x or y`` | if *x* is false, then *y*, else | \(1) | 88| | *x* | | 89+-------------+---------------------------------+-------+ 90| ``x and y`` | if *x* is false, then *x*, else | \(2) | 91| | *y* | | 92+-------------+---------------------------------+-------+ 93| ``not x`` | if *x* is false, then ``True``, | \(3) | 94| | else ``False`` | | 95+-------------+---------------------------------+-------+ 96 97.. index:: 98 operator: and 99 operator: or 100 operator: not 101 102Notes: 103 104(1) 105 This is a short-circuit operator, so it only evaluates the second 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 119Comparisons 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 134There are eight comparison operations in Python. They all have the same 135priority (which is higher than that of the Boolean operations). Comparisons can 136be chained arbitrarily; for example, ``x < y <= z`` is equivalent to ``x < y and 137y <= z``, except that *y* is evaluated only once (but in both cases *z* is not 138evaluated at all when ``x < y`` is found to be false). 139 140This table summarizes the comparison operations: 141 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 166Objects of different types, except different numeric types, never compare equal. 167The ``==`` operator is always defined but for some object types (for example, 168class objects) is equivalent to :keyword:`is`. The ``<``, ``<=``, ``>`` and ``>=`` 169operators 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 180Non-identical instances of a class normally compare as non-equal unless the 181class defines the :meth:`__eq__` method. 182 183Instances of a class cannot be ordered with respect to other instances of the 184same class, or other types of object, unless the class defines enough of the 185methods :meth:`__lt__`, :meth:`__le__`, :meth:`__gt__`, and :meth:`__ge__` (in 186general, :meth:`__lt__` and :meth:`__eq__` are sufficient, if you want the 187conventional meanings of the comparison operators). 188 189The behavior of the :keyword:`is` and :keyword:`is not` operators cannot be 190customized; also they can be applied to any two objects and never raise an 191exception. 192 193.. index:: 194 operator: in 195 operator: not in 196 197Two more operations with the same syntactic priority, :keyword:`in` and 198:keyword:`not in`, are supported by types that are :term:`iterable` or 199implement the :meth:`__contains__` method. 200 201.. _typesnumeric: 202 203Numeric 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 214There are three distinct numeric types: :dfn:`integers`, :dfn:`floating 215point numbers`, and :dfn:`complex numbers`. In addition, Booleans are a 216subtype of integers. Integers have unlimited precision. Floating point 217numbers are usually implemented using :c:type:`double` in C; information 218about the precision and internal representation of floating point 219numbers for the machine on which your program is running is available 220in :data:`sys.float_info`. Complex numbers have a real and imaginary 221part, which are each a floating point number. To extract these parts 222from a complex number *z*, use ``z.real`` and ``z.imag``. (The standard 223library includes the additional numeric types :mod:`fractions.Fraction`, for 224rationals, and :mod:`decimal.Decimal`, for floating-point numbers with 225user-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 236Numbers are created by numeric literals or as the result of built-in functions 237and operators. Unadorned integer literals (including hex, octal and binary 238numbers) yield integers. Numeric literals containing a decimal point or an 239exponent sign yield floating point numbers. Appending ``'j'`` or ``'J'`` to a 240numeric literal yields an imaginary number (a complex number with a zero real 241part) which you can add to an integer or float to get a complex number with real 242and 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 261Python fully supports mixed arithmetic: when a binary arithmetic operator has 262operands of different numeric types, the operand with the "narrower" type is 263widened to that of the other, where integer is narrower than floating point, 264which is narrower than complex. A comparison between numbers of different types 265behaves as though the exact values of those numbers were being compared. [2]_ 266 267The constructors :func:`int`, :func:`float`, and 268:func:`complex` can be used to produce numbers of a specific type. 269 270All numeric types (except complex) support the following operations (for priorities of 271the operations, see :ref:`operator-summary`): 272 273+---------------------+---------------------------------+---------+--------------------+ 274| Operation | Result | Notes | Full documentation | 275+=====================+=================================+=========+====================+ 276| ``x + y`` | sum of *x* and *y* | | | 277+---------------------+---------------------------------+---------+--------------------+ 278| ``x - y`` | difference of *x* and *y* | | | 279+---------------------+---------------------------------+---------+--------------------+ 280| ``x * y`` | product of *x* and *y* | | | 281+---------------------+---------------------------------+---------+--------------------+ 282| ``x / y`` | quotient of *x* and *y* | | | 283+---------------------+---------------------------------+---------+--------------------+ 284| ``x // y`` | floored quotient of *x* and | \(1) | | 285| | *y* | | | 286+---------------------+---------------------------------+---------+--------------------+ 287| ``x % y`` | remainder of ``x / y`` | \(2) | | 288+---------------------+---------------------------------+---------+--------------------+ 289| ``-x`` | *x* negated | | | 290+---------------------+---------------------------------+---------+--------------------+ 291| ``+x`` | *x* unchanged | | | 292+---------------------+---------------------------------+---------+--------------------+ 293| ``abs(x)`` | absolute value or magnitude of | | :func:`abs` | 294| | *x* | | | 295+---------------------+---------------------------------+---------+--------------------+ 296| ``int(x)`` | *x* converted to integer | \(3)\(6)| :func:`int` | 297+---------------------+---------------------------------+---------+--------------------+ 298| ``float(x)`` | *x* converted to floating point | \(4)\(6)| :func:`float` | 299+---------------------+---------------------------------+---------+--------------------+ 300| ``complex(re, im)`` | a complex number with real part | \(6) | :func:`complex` | 301| | *re*, imaginary part *im*. | | | 302| | *im* defaults to zero. | | | 303+---------------------+---------------------------------+---------+--------------------+ 304| ``c.conjugate()`` | conjugate of the complex number | | | 305| | *c* | | | 306+---------------------+---------------------------------+---------+--------------------+ 307| ``divmod(x, y)`` | the pair ``(x // y, x % y)`` | \(2) | :func:`divmod` | 308+---------------------+---------------------------------+---------+--------------------+ 309| ``pow(x, y)`` | *x* to the power *y* | \(5) | :func:`pow` | 310+---------------------+---------------------------------+---------+--------------------+ 311| ``x ** y`` | *x* to the power *y* | \(5) | | 312+---------------------+---------------------------------+---------+--------------------+ 313 314.. index:: 315 triple: operations on; numeric; types 316 single: conjugate() (complex number method) 317 318Notes: 319 320(1) 321 Also referred to as integer division. The resultant value is a whole 322 integer, though the result's type is not necessarily int. The result is 323 always rounded towards minus infinity: ``1//2`` is ``0``, ``(-1)//2`` is 324 ``-1``, ``1//(-2)`` is ``-1``, and ``(-1)//(-2)`` is ``0``. 325 326(2) 327 Not for complex numbers. Instead convert to floats using :func:`abs` if 328 appropriate. 329 330(3) 331 .. index:: 332 module: math 333 single: floor() (in module math) 334 single: ceil() (in module math) 335 single: trunc() (in module math) 336 pair: numeric; conversions 337 pair: C; language 338 339 Conversion from floating point to integer may round or truncate 340 as in C; see functions :func:`math.floor` and :func:`math.ceil` for 341 well-defined conversions. 342 343(4) 344 float also accepts the strings "nan" and "inf" with an optional prefix "+" 345 or "-" for Not a Number (NaN) and positive or negative infinity. 346 347(5) 348 Python defines ``pow(0, 0)`` and ``0 ** 0`` to be ``1``, as is common for 349 programming languages. 350 351(6) 352 The numeric literals accepted include the digits ``0`` to ``9`` or any 353 Unicode equivalent (code points with the ``Nd`` property). 354 355 See https://www.unicode.org/Public/13.0.0/ucd/extracted/DerivedNumericType.txt 356 for a complete list of code points with the ``Nd`` property. 357 358 359All :class:`numbers.Real` types (:class:`int` and :class:`float`) also include 360the following operations: 361 362+--------------------+---------------------------------------------+ 363| Operation | Result | 364+====================+=============================================+ 365| :func:`math.trunc(\| *x* truncated to :class:`~numbers.Integral` | 366| x) <math.trunc>` | | 367+--------------------+---------------------------------------------+ 368| :func:`round(x[, | *x* rounded to *n* digits, | 369| n]) <round>` | rounding half to even. If *n* is | 370| | omitted, it defaults to 0. | 371+--------------------+---------------------------------------------+ 372| :func:`math.floor(\| the greatest :class:`~numbers.Integral` | 373| x) <math.floor>` | <= *x* | 374+--------------------+---------------------------------------------+ 375| :func:`math.ceil(x)| the least :class:`~numbers.Integral` >= *x* | 376| <math.ceil>` | | 377+--------------------+---------------------------------------------+ 378 379For additional numeric operations see the :mod:`math` and :mod:`cmath` 380modules. 381 382.. XXXJH exceptions: overflow (when? what operations?) zerodivision 383 384 385.. _bitstring-ops: 386 387Bitwise Operations on Integer Types 388----------------------------------- 389 390.. index:: 391 triple: operations on; integer; types 392 pair: bitwise; operations 393 pair: shifting; operations 394 pair: masking; operations 395 operator: | (vertical bar) 396 operator: ^ (caret) 397 operator: & (ampersand) 398 operator: << 399 operator: >> 400 operator: ~ (tilde) 401 402Bitwise operations only make sense for integers. The result of bitwise 403operations is calculated as though carried out in two's complement with an 404infinite number of sign bits. 405 406The priorities of the binary bitwise operations are all lower than the numeric 407operations and higher than the comparisons; the unary operation ``~`` has the 408same priority as the other unary numeric operations (``+`` and ``-``). 409 410This table lists the bitwise operations sorted in ascending priority: 411 412+------------+--------------------------------+----------+ 413| Operation | Result | Notes | 414+============+================================+==========+ 415| ``x | y`` | bitwise :dfn:`or` of *x* and | \(4) | 416| | *y* | | 417+------------+--------------------------------+----------+ 418| ``x ^ y`` | bitwise :dfn:`exclusive or` of | \(4) | 419| | *x* and *y* | | 420+------------+--------------------------------+----------+ 421| ``x & y`` | bitwise :dfn:`and` of *x* and | \(4) | 422| | *y* | | 423+------------+--------------------------------+----------+ 424| ``x << n`` | *x* shifted left by *n* bits | (1)(2) | 425+------------+--------------------------------+----------+ 426| ``x >> n`` | *x* shifted right by *n* bits | (1)(3) | 427+------------+--------------------------------+----------+ 428| ``~x`` | the bits of *x* inverted | | 429+------------+--------------------------------+----------+ 430 431Notes: 432 433(1) 434 Negative shift counts are illegal and cause a :exc:`ValueError` to be raised. 435 436(2) 437 A left shift by *n* bits is equivalent to multiplication by ``pow(2, n)``. 438 439(3) 440 A right shift by *n* bits is equivalent to floor division by ``pow(2, n)``. 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 449Additional Methods on Integer Types 450----------------------------------- 451 452The int type implements the :class:`numbers.Integral` :term:`abstract base 453class`. 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 552Additional Methods on Float 553--------------------------- 554 555The float type implements the :class:`numbers.Real` :term:`abstract base 556class`. 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 575Two methods support conversion to 576and from hexadecimal strings. Since Python's floats are stored 577internally as binary numbers, converting a float to or from a 578*decimal* string usually involves a small rounding error. In 579contrast, hexadecimal strings allow exact representation and 580specification of floating-point numbers. This can be useful when 581debugging, 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 599Note that :meth:`float.hex` is an instance method, while 600:meth:`float.fromhex` is a class method. 601 602A hexadecimal string takes the form:: 603 604 [sign] ['0x'] integer ['.' fraction] ['p' exponent] 605 606where the optional ``sign`` may by either ``+`` or ``-``, ``integer`` 607and ``fraction`` are strings of hexadecimal digits, and ``exponent`` 608is a decimal integer with an optional leading sign. Case is not 609significant, and there must be at least one hexadecimal digit in 610either the integer or the fraction. This syntax is similar to the 611syntax specified in section 6.4.4.2 of the C99 standard, and also to 612the 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 614C or Java code, and hexadecimal strings produced by C's ``%a`` format 615character or Java's ``Double.toHexString`` are accepted by 616:meth:`float.fromhex`. 617 618 619Note that the exponent is written in decimal rather than hexadecimal, 620and that it gives the power of 2 by which to multiply the coefficient. 621For example, the hexadecimal string ``0x3.a7p10`` represents the 622floating-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 629Applying the reverse conversion to ``3740.0`` gives a different 630hexadecimal string representing the same number:: 631 632 >>> float.hex(3740.0) 633 '0x1.d380000000000p+11' 634 635 636.. _numeric-hash: 637 638Hashing of numeric types 639------------------------ 640 641For numbers ``x`` and ``y``, possibly of different types, it's a requirement 642that ``hash(x) == hash(y)`` whenever ``x == y`` (see the :meth:`__hash__` 643method documentation for more details). For ease of implementation and 644efficiency across a variety of numeric types (including :class:`int`, 645:class:`float`, :class:`decimal.Decimal` and :class:`fractions.Fraction`) 646Python's hash for numeric types is based on a single mathematical function 647that'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 650given by reduction modulo ``P`` for a fixed prime ``P``. The value of ``P`` is 651made 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 659Here 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 687To clarify the above rules, here's some example Python code, 688equivalent to the built-in hash, for computing the hash of a rational 689number, :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 741Iterator Types 742============== 743 744.. index:: 745 single: iterator protocol 746 single: protocol; iterator 747 single: sequence; iteration 748 single: container; iteration over 749 750Python supports a concept of iteration over containers. This is implemented 751using two distinct methods; these are used to allow user-defined classes to 752support iteration. Sequences, described below in more detail, always support 753the iteration methods. 754 755One method needs to be defined for container objects to provide iteration 756support: 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 771The iterator objects themselves are required to support the following two 772methods, 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 790Python defines several iterator objects to support iteration over general and 791specific sequence types, dictionaries, and other more specialized forms. The 792specific types are not important beyond their implementation of the iterator 793protocol. 794 795Once an iterator's :meth:`~iterator.__next__` method raises 796:exc:`StopIteration`, it must continue to do so on subsequent calls. 797Implementations that do not obey this property are deemed broken. 798 799 800.. _generator-types: 801 802Generator Types 803--------------- 804 805Python's :term:`generator`\s provide a convenient way to implement the iterator 806protocol. If a container object's :meth:`__iter__` method is implemented as a 807generator, it will automatically return an iterator object (technically, a 808generator object) supplying the :meth:`__iter__` and :meth:`~generator.__next__` 809methods. 810More information about generators can be found in :ref:`the documentation for 811the yield expression <yieldexpr>`. 812 813 814.. _typesseq: 815 816Sequence Types --- :class:`list`, :class:`tuple`, :class:`range` 817================================================================ 818 819There are three basic sequence types: lists, tuples, and range objects. 820Additional sequence types tailored for processing of 821:ref:`binary data <binaryseq>` and :ref:`text strings <textseq>` are 822described in dedicated sections. 823 824 825.. _typesseq-common: 826 827Common Sequence Operations 828-------------------------- 829 830.. index:: object: sequence 831 832The operations in the following table are supported by most sequence types, 833both mutable and immutable. The :class:`collections.abc.Sequence` ABC is 834provided to make it easier to correctly implement these operations on 835custom sequence types. 836 837This table lists the sequence operations sorted in ascending priority. In the 838table, *s* and *t* are sequences of the same type, *n*, *i*, *j* and *k* are 839integers and *x* is an arbitrary object that meets any type and value 840restrictions imposed by *s*. 841 842The ``in`` and ``not in`` operations have the same priorities as the 843comparison operations. The ``+`` (concatenation) and ``*`` (repetition) 844operations 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 896Sequences of the same type also support comparisons. In particular, tuples 897and lists are compared lexicographically by comparing corresponding elements. 898This means that to compare equal, every element must compare equal and the 899two sequences must be of the same type and have the same length. (For full 900details see :ref:`comparisons` in the language reference.) 901 902Notes: 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 1000Immutable Sequence Types 1001------------------------ 1002 1003.. index:: 1004 triple: immutable; sequence; types 1005 object: tuple 1006 builtin: hash 1007 1008The only operation that immutable sequence types generally implement that is 1009not also implemented by mutable sequence types is support for the :func:`hash` 1010built-in. 1011 1012This support allows immutable sequences, such as :class:`tuple` instances, to 1013be used as :class:`dict` keys and stored in :class:`set` and :class:`frozenset` 1014instances. 1015 1016Attempting to hash an immutable sequence that contains unhashable values will 1017result in :exc:`TypeError`. 1018 1019 1020.. _typesseq-mutable: 1021 1022Mutable Sequence Types 1023---------------------- 1024 1025.. index:: 1026 triple: mutable; sequence; types 1027 object: list 1028 object: bytearray 1029 1030The operations in the following table are defined on mutable sequence types. 1031The :class:`collections.abc.MutableSequence` ABC is provided to make it 1032easier to correctly implement these operations on custom sequence types. 1033 1034In the table *s* is an instance of a mutable sequence type, *t* is any 1035iterable object and *x* is an arbitrary object that meets any type 1036and value restrictions imposed by *s* (for example, :class:`bytearray` only 1037accepts 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 1106Notes: 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 1142Lists 1143----- 1144 1145.. index:: object: list 1146 1147Lists are mutable sequences, typically used to store collections of 1148homogeneous items (where the precise degree of similarity will vary by 1149application). 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 1221Tuples 1222------ 1223 1224.. index:: object: tuple 1225 1226Tuples are immutable sequences, typically used to store collections of 1227heterogeneous data (such as the 2-tuples produced by the :func:`enumerate` 1228built-in). Tuples are also used for cases where an immutable sequence of 1229homogeneous 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 1258For heterogeneous collections of data where access by name is clearer than 1259access by index, :func:`collections.namedtuple` may be a more appropriate 1260choice than a simple tuple object. 1261 1262 1263.. _typesseq-range: 1264 1265Ranges 1266------ 1267 1268.. index:: object: range 1269 1270The :class:`range` type represents an immutable sequence of numbers and is 1271commonly used for looping a specific number of times in :keyword:`for` 1272loops. 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 1336The 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 1339only stores the ``start``, ``stop`` and ``step`` values, calculating individual 1340items and subranges as needed). 1341 1342Range objects implement the :class:`collections.abc.Sequence` ABC, and provide 1343features such as containment tests, element index lookup, slicing and 1344support 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 1362Testing range objects for equality with ``==`` and ``!=`` compares 1363them as sequences. That is, two range objects are considered equal if 1364they represent the same sequence of values. (Note that two range 1365objects 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 1397Text Sequence Type --- :class:`str` 1398=================================== 1399 1400Textual data in Python is handled with :class:`str` objects, or :dfn:`strings`. 1401Strings are immutable 1402:ref:`sequences <typesseq>` of Unicode code points. String literals are 1403written 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 1409Triple quoted strings may span multiple lines - all associated whitespace will 1410be included in the string literal. 1411 1412String literals that are part of a single expression and have only whitespace 1413between them will be implicitly converted to a single string literal. That 1414is, ``("spam " "eggs") == "spam eggs"``. 1415 1416See :ref:`strings` for more about the various forms of string literal, 1417including supported escape sequences, and the ``r`` ("raw") prefix that 1418disables most escape sequence processing. 1419 1420Strings may also be created from other objects using the :class:`str` 1421constructor. 1422 1423Since there is no separate "character" type, indexing a string produces 1424strings of length 1. That is, for a non-empty string *s*, ``s[0] == s[0:1]``. 1425 1426.. index:: 1427 object: io.StringIO 1428 1429There is also no mutable string type, but :meth:`str.join` or 1430:class:`io.StringIO` can be used to efficiently construct strings from 1431multiple 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 1488String Methods 1489-------------- 1490 1491.. index:: 1492 module: re 1493 1494Strings implement all of the :ref:`common <typesseq-common>` sequence 1495operations, along with the additional methods described below. 1496 1497Strings also support two styles of string formatting, one providing a large 1498degree 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 1501slightly harder to use correctly, but is often faster for the cases it can 1502handle (:ref:`old-string-formatting`). 1503 1504The :ref:`textservices` section of the standard library covers a number of 1505other modules that provide various text related utilities (including regular 1506expression 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 :ref:`Python Development 1563 Mode <devmode>`, 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 See :meth:`str.removeprefix` for a method that will remove a single prefix 1833 string rather than all of a set of characters. For example:: 1834 1835 >>> 'Arthur: three!'.lstrip('Arthur: ') 1836 'ee!' 1837 >>> 'Arthur: three!'.removeprefix('Arthur: ') 1838 'three!' 1839 1840 1841.. staticmethod:: str.maketrans(x[, y[, z]]) 1842 1843 This static method returns a translation table usable for :meth:`str.translate`. 1844 1845 If there is only one argument, it must be a dictionary mapping Unicode 1846 ordinals (integers) or characters (strings of length 1) to Unicode ordinals, 1847 strings (of arbitrary lengths) or ``None``. Character keys will then be 1848 converted to ordinals. 1849 1850 If there are two arguments, they must be strings of equal length, and in the 1851 resulting dictionary, each character in x will be mapped to the character at 1852 the same position in y. If there is a third argument, it must be a string, 1853 whose characters will be mapped to ``None`` in the result. 1854 1855 1856.. method:: str.partition(sep) 1857 1858 Split the string at the first occurrence of *sep*, and return a 3-tuple 1859 containing the part before the separator, the separator itself, and the part 1860 after the separator. If the separator is not found, return a 3-tuple containing 1861 the string itself, followed by two empty strings. 1862 1863 1864.. method:: str.removeprefix(prefix, /) 1865 1866 If the string starts with the *prefix* string, return 1867 ``string[len(prefix):]``. Otherwise, return a copy of the original 1868 string:: 1869 1870 >>> 'TestHook'.removeprefix('Test') 1871 'Hook' 1872 >>> 'BaseTestCase'.removeprefix('Test') 1873 'BaseTestCase' 1874 1875 .. versionadded:: 3.9 1876 1877 1878.. method:: str.removesuffix(suffix, /) 1879 1880 If the string ends with the *suffix* string and that *suffix* is not empty, 1881 return ``string[:-len(suffix)]``. Otherwise, return a copy of the 1882 original string:: 1883 1884 >>> 'MiscTests'.removesuffix('Tests') 1885 'Misc' 1886 >>> 'TmpDirMixin'.removesuffix('Tests') 1887 'TmpDirMixin' 1888 1889 .. versionadded:: 3.9 1890 1891 1892.. method:: str.replace(old, new[, count]) 1893 1894 Return a copy of the string with all occurrences of substring *old* replaced by 1895 *new*. If the optional argument *count* is given, only the first *count* 1896 occurrences are replaced. 1897 1898 1899.. method:: str.rfind(sub[, start[, end]]) 1900 1901 Return the highest index in the string where substring *sub* is found, such 1902 that *sub* is contained within ``s[start:end]``. Optional arguments *start* 1903 and *end* are interpreted as in slice notation. Return ``-1`` on failure. 1904 1905 1906.. method:: str.rindex(sub[, start[, end]]) 1907 1908 Like :meth:`rfind` but raises :exc:`ValueError` when the substring *sub* is not 1909 found. 1910 1911 1912.. method:: str.rjust(width[, fillchar]) 1913 1914 Return the string right justified in a string of length *width*. Padding is 1915 done using the specified *fillchar* (default is an ASCII space). The 1916 original string is returned if *width* is less than or equal to ``len(s)``. 1917 1918 1919.. method:: str.rpartition(sep) 1920 1921 Split the string at the last occurrence of *sep*, and return a 3-tuple 1922 containing the part before the separator, the separator itself, and the part 1923 after the separator. If the separator is not found, return a 3-tuple containing 1924 two empty strings, followed by the string itself. 1925 1926 1927.. method:: str.rsplit(sep=None, maxsplit=-1) 1928 1929 Return a list of the words in the string, using *sep* as the delimiter string. 1930 If *maxsplit* is given, at most *maxsplit* splits are done, the *rightmost* 1931 ones. If *sep* is not specified or ``None``, any whitespace string is a 1932 separator. Except for splitting from the right, :meth:`rsplit` behaves like 1933 :meth:`split` which is described in detail below. 1934 1935 1936.. method:: str.rstrip([chars]) 1937 1938 Return a copy of the string with trailing characters removed. The *chars* 1939 argument is a string specifying the set of characters to be removed. If omitted 1940 or ``None``, the *chars* argument defaults to removing whitespace. The *chars* 1941 argument is not a suffix; rather, all combinations of its values are stripped:: 1942 1943 >>> ' spacious '.rstrip() 1944 ' spacious' 1945 >>> 'mississippi'.rstrip('ipz') 1946 'mississ' 1947 1948 See :meth:`str.removesuffix` for a method that will remove a single suffix 1949 string rather than all of a set of characters. For example:: 1950 1951 >>> 'Monty Python'.rstrip(' Python') 1952 'M' 1953 >>> 'Monty Python'.removesuffix(' Python') 1954 'Monty' 1955 1956.. method:: str.split(sep=None, maxsplit=-1) 1957 1958 Return a list of the words in the string, using *sep* as the delimiter 1959 string. If *maxsplit* is given, at most *maxsplit* splits are done (thus, 1960 the list will have at most ``maxsplit+1`` elements). If *maxsplit* is not 1961 specified or ``-1``, then there is no limit on the number of splits 1962 (all possible splits are made). 1963 1964 If *sep* is given, consecutive delimiters are not grouped together and are 1965 deemed to delimit empty strings (for example, ``'1,,2'.split(',')`` returns 1966 ``['1', '', '2']``). The *sep* argument may consist of multiple characters 1967 (for example, ``'1<>2<>3'.split('<>')`` returns ``['1', '2', '3']``). 1968 Splitting an empty string with a specified separator returns ``['']``. 1969 1970 For example:: 1971 1972 >>> '1,2,3'.split(',') 1973 ['1', '2', '3'] 1974 >>> '1,2,3'.split(',', maxsplit=1) 1975 ['1', '2,3'] 1976 >>> '1,2,,3,'.split(',') 1977 ['1', '2', '', '3', ''] 1978 1979 If *sep* is not specified or is ``None``, a different splitting algorithm is 1980 applied: runs of consecutive whitespace are regarded as a single separator, 1981 and the result will contain no empty strings at the start or end if the 1982 string has leading or trailing whitespace. Consequently, splitting an empty 1983 string or a string consisting of just whitespace with a ``None`` separator 1984 returns ``[]``. 1985 1986 For example:: 1987 1988 >>> '1 2 3'.split() 1989 ['1', '2', '3'] 1990 >>> '1 2 3'.split(maxsplit=1) 1991 ['1', '2 3'] 1992 >>> ' 1 2 3 '.split() 1993 ['1', '2', '3'] 1994 1995 1996.. index:: 1997 single: universal newlines; str.splitlines method 1998 1999.. method:: str.splitlines([keepends]) 2000 2001 Return a list of the lines in the string, breaking at line boundaries. Line 2002 breaks are not included in the resulting list unless *keepends* is given and 2003 true. 2004 2005 This method splits on the following line boundaries. In particular, the 2006 boundaries are a superset of :term:`universal newlines`. 2007 2008 +-----------------------+-----------------------------+ 2009 | Representation | Description | 2010 +=======================+=============================+ 2011 | ``\n`` | Line Feed | 2012 +-----------------------+-----------------------------+ 2013 | ``\r`` | Carriage Return | 2014 +-----------------------+-----------------------------+ 2015 | ``\r\n`` | Carriage Return + Line Feed | 2016 +-----------------------+-----------------------------+ 2017 | ``\v`` or ``\x0b`` | Line Tabulation | 2018 +-----------------------+-----------------------------+ 2019 | ``\f`` or ``\x0c`` | Form Feed | 2020 +-----------------------+-----------------------------+ 2021 | ``\x1c`` | File Separator | 2022 +-----------------------+-----------------------------+ 2023 | ``\x1d`` | Group Separator | 2024 +-----------------------+-----------------------------+ 2025 | ``\x1e`` | Record Separator | 2026 +-----------------------+-----------------------------+ 2027 | ``\x85`` | Next Line (C1 Control Code) | 2028 +-----------------------+-----------------------------+ 2029 | ``\u2028`` | Line Separator | 2030 +-----------------------+-----------------------------+ 2031 | ``\u2029`` | Paragraph Separator | 2032 +-----------------------+-----------------------------+ 2033 2034 .. versionchanged:: 3.2 2035 2036 ``\v`` and ``\f`` added to list of line boundaries. 2037 2038 For example:: 2039 2040 >>> 'ab c\n\nde fg\rkl\r\n'.splitlines() 2041 ['ab c', '', 'de fg', 'kl'] 2042 >>> 'ab c\n\nde fg\rkl\r\n'.splitlines(keepends=True) 2043 ['ab c\n', '\n', 'de fg\r', 'kl\r\n'] 2044 2045 Unlike :meth:`~str.split` when a delimiter string *sep* is given, this 2046 method returns an empty list for the empty string, and a terminal line 2047 break does not result in an extra line:: 2048 2049 >>> "".splitlines() 2050 [] 2051 >>> "One line\n".splitlines() 2052 ['One line'] 2053 2054 For comparison, ``split('\n')`` gives:: 2055 2056 >>> ''.split('\n') 2057 [''] 2058 >>> 'Two lines\n'.split('\n') 2059 ['Two lines', ''] 2060 2061 2062.. method:: str.startswith(prefix[, start[, end]]) 2063 2064 Return ``True`` if string starts with the *prefix*, otherwise return ``False``. 2065 *prefix* can also be a tuple of prefixes to look for. With optional *start*, 2066 test string beginning at that position. With optional *end*, stop comparing 2067 string at that position. 2068 2069 2070.. method:: str.strip([chars]) 2071 2072 Return a copy of the string with the leading and trailing characters removed. 2073 The *chars* argument is a string specifying the set of characters to be removed. 2074 If omitted or ``None``, the *chars* argument defaults to removing whitespace. 2075 The *chars* argument is not a prefix or suffix; rather, all combinations of its 2076 values are stripped:: 2077 2078 >>> ' spacious '.strip() 2079 'spacious' 2080 >>> 'www.example.com'.strip('cmowz.') 2081 'example' 2082 2083 The outermost leading and trailing *chars* argument values are stripped 2084 from the string. Characters are removed from the leading end until 2085 reaching a string character that is not contained in the set of 2086 characters in *chars*. A similar action takes place on the trailing end. 2087 For example:: 2088 2089 >>> comment_string = '#....... Section 3.2.1 Issue #32 .......' 2090 >>> comment_string.strip('.#! ') 2091 'Section 3.2.1 Issue #32' 2092 2093 2094.. method:: str.swapcase() 2095 2096 Return a copy of the string with uppercase characters converted to lowercase and 2097 vice versa. Note that it is not necessarily true that 2098 ``s.swapcase().swapcase() == s``. 2099 2100 2101.. method:: str.title() 2102 2103 Return a titlecased version of the string where words start with an uppercase 2104 character and the remaining characters are lowercase. 2105 2106 For example:: 2107 2108 >>> 'Hello world'.title() 2109 'Hello World' 2110 2111 The algorithm uses a simple language-independent definition of a word as 2112 groups of consecutive letters. The definition works in many contexts but 2113 it means that apostrophes in contractions and possessives form word 2114 boundaries, which may not be the desired result:: 2115 2116 >>> "they're bill's friends from the UK".title() 2117 "They'Re Bill'S Friends From The Uk" 2118 2119 A workaround for apostrophes can be constructed using regular expressions:: 2120 2121 >>> import re 2122 >>> def titlecase(s): 2123 ... return re.sub(r"[A-Za-z]+('[A-Za-z]+)?", 2124 ... lambda mo: mo.group(0).capitalize(), 2125 ... s) 2126 ... 2127 >>> titlecase("they're bill's friends.") 2128 "They're Bill's Friends." 2129 2130 2131.. method:: str.translate(table) 2132 2133 Return a copy of the string in which each character has been mapped through 2134 the given translation table. The table must be an object that implements 2135 indexing via :meth:`__getitem__`, typically a :term:`mapping` or 2136 :term:`sequence`. When indexed by a Unicode ordinal (an integer), the 2137 table object can do any of the following: return a Unicode ordinal or a 2138 string, to map the character to one or more other characters; return 2139 ``None``, to delete the character from the return string; or raise a 2140 :exc:`LookupError` exception, to map the character to itself. 2141 2142 You can use :meth:`str.maketrans` to create a translation map from 2143 character-to-character mappings in different formats. 2144 2145 See also the :mod:`codecs` module for a more flexible approach to custom 2146 character mappings. 2147 2148 2149.. method:: str.upper() 2150 2151 Return a copy of the string with all the cased characters [4]_ converted to 2152 uppercase. Note that ``s.upper().isupper()`` might be ``False`` if ``s`` 2153 contains uncased characters or if the Unicode category of the resulting 2154 character(s) is not "Lu" (Letter, uppercase), but e.g. "Lt" (Letter, 2155 titlecase). 2156 2157 The uppercasing algorithm used is described in section 3.13 of the Unicode 2158 Standard. 2159 2160 2161.. method:: str.zfill(width) 2162 2163 Return a copy of the string left filled with ASCII ``'0'`` digits to 2164 make a string of length *width*. A leading sign prefix (``'+'``/``'-'``) 2165 is handled by inserting the padding *after* the sign character rather 2166 than before. The original string is returned if *width* is less than 2167 or equal to ``len(s)``. 2168 2169 For example:: 2170 2171 >>> "42".zfill(5) 2172 '00042' 2173 >>> "-42".zfill(5) 2174 '-0042' 2175 2176 2177 2178.. _old-string-formatting: 2179 2180``printf``-style String Formatting 2181---------------------------------- 2182 2183.. index:: 2184 single: formatting, string (%) 2185 single: interpolation, string (%) 2186 single: string; formatting, printf 2187 single: string; interpolation, printf 2188 single: printf-style formatting 2189 single: sprintf-style formatting 2190 single: % (percent); printf-style formatting 2191 2192.. note:: 2193 2194 The formatting operations described here exhibit a variety of quirks that 2195 lead to a number of common errors (such as failing to display tuples and 2196 dictionaries correctly). Using the newer :ref:`formatted string literals 2197 <f-strings>`, the :meth:`str.format` interface, or :ref:`template strings 2198 <template-strings>` may help avoid these errors. Each of these 2199 alternatives provides their own trade-offs and benefits of simplicity, 2200 flexibility, and/or extensibility. 2201 2202String objects have one unique built-in operation: the ``%`` operator (modulo). 2203This is also known as the string *formatting* or *interpolation* operator. 2204Given ``format % values`` (where *format* is a string), ``%`` conversion 2205specifications in *format* are replaced with zero or more elements of *values*. 2206The effect is similar to using the :c:func:`sprintf` in the C language. 2207 2208If *format* requires a single argument, *values* may be a single non-tuple 2209object. [5]_ Otherwise, *values* must be a tuple with exactly the number of 2210items specified by the format string, or a single mapping object (for example, a 2211dictionary). 2212 2213.. index:: 2214 single: () (parentheses); in printf-style formatting 2215 single: * (asterisk); in printf-style formatting 2216 single: . (dot); in printf-style formatting 2217 2218A conversion specifier contains two or more characters and has the following 2219components, which must occur in this order: 2220 2221#. The ``'%'`` character, which marks the start of the specifier. 2222 2223#. Mapping key (optional), consisting of a parenthesised sequence of characters 2224 (for example, ``(somename)``). 2225 2226#. Conversion flags (optional), which affect the result of some conversion 2227 types. 2228 2229#. Minimum field width (optional). If specified as an ``'*'`` (asterisk), the 2230 actual width is read from the next element of the tuple in *values*, and the 2231 object to convert comes after the minimum field width and optional precision. 2232 2233#. Precision (optional), given as a ``'.'`` (dot) followed by the precision. If 2234 specified as ``'*'`` (an asterisk), the actual precision is read from the next 2235 element of the tuple in *values*, and the value to convert comes after the 2236 precision. 2237 2238#. Length modifier (optional). 2239 2240#. Conversion type. 2241 2242When the right argument is a dictionary (or other mapping type), then the 2243formats in the string *must* include a parenthesised mapping key into that 2244dictionary inserted immediately after the ``'%'`` character. The mapping key 2245selects the value to be formatted from the mapping. For example: 2246 2247 >>> print('%(language)s has %(number)03d quote types.' % 2248 ... {'language': "Python", "number": 2}) 2249 Python has 002 quote types. 2250 2251In this case no ``*`` specifiers may occur in a format (since they require a 2252sequential parameter list). 2253 2254The conversion flag characters are: 2255 2256.. index:: 2257 single: # (hash); in printf-style formatting 2258 single: - (minus); in printf-style formatting 2259 single: + (plus); in printf-style formatting 2260 single: space; in printf-style formatting 2261 2262+---------+---------------------------------------------------------------------+ 2263| Flag | Meaning | 2264+=========+=====================================================================+ 2265| ``'#'`` | The value conversion will use the "alternate form" (where defined | 2266| | below). | 2267+---------+---------------------------------------------------------------------+ 2268| ``'0'`` | The conversion will be zero padded for numeric values. | 2269+---------+---------------------------------------------------------------------+ 2270| ``'-'`` | The converted value is left adjusted (overrides the ``'0'`` | 2271| | conversion if both are given). | 2272+---------+---------------------------------------------------------------------+ 2273| ``' '`` | (a space) A blank should be left before a positive number (or empty | 2274| | string) produced by a signed conversion. | 2275+---------+---------------------------------------------------------------------+ 2276| ``'+'`` | A sign character (``'+'`` or ``'-'``) will precede the conversion | 2277| | (overrides a "space" flag). | 2278+---------+---------------------------------------------------------------------+ 2279 2280A length modifier (``h``, ``l``, or ``L``) may be present, but is ignored as it 2281is not necessary for Python -- so e.g. ``%ld`` is identical to ``%d``. 2282 2283The conversion types are: 2284 2285+------------+-----------------------------------------------------+-------+ 2286| Conversion | Meaning | Notes | 2287+============+=====================================================+=======+ 2288| ``'d'`` | Signed integer decimal. | | 2289+------------+-----------------------------------------------------+-------+ 2290| ``'i'`` | Signed integer decimal. | | 2291+------------+-----------------------------------------------------+-------+ 2292| ``'o'`` | Signed octal value. | \(1) | 2293+------------+-----------------------------------------------------+-------+ 2294| ``'u'`` | Obsolete type -- it is identical to ``'d'``. | \(6) | 2295+------------+-----------------------------------------------------+-------+ 2296| ``'x'`` | Signed hexadecimal (lowercase). | \(2) | 2297+------------+-----------------------------------------------------+-------+ 2298| ``'X'`` | Signed hexadecimal (uppercase). | \(2) | 2299+------------+-----------------------------------------------------+-------+ 2300| ``'e'`` | Floating point exponential format (lowercase). | \(3) | 2301+------------+-----------------------------------------------------+-------+ 2302| ``'E'`` | Floating point exponential format (uppercase). | \(3) | 2303+------------+-----------------------------------------------------+-------+ 2304| ``'f'`` | Floating point decimal format. | \(3) | 2305+------------+-----------------------------------------------------+-------+ 2306| ``'F'`` | Floating point decimal format. | \(3) | 2307+------------+-----------------------------------------------------+-------+ 2308| ``'g'`` | Floating point format. Uses lowercase exponential | \(4) | 2309| | format if exponent is less than -4 or not less than | | 2310| | precision, decimal format otherwise. | | 2311+------------+-----------------------------------------------------+-------+ 2312| ``'G'`` | Floating point format. Uses uppercase exponential | \(4) | 2313| | format if exponent is less than -4 or not less than | | 2314| | precision, decimal format otherwise. | | 2315+------------+-----------------------------------------------------+-------+ 2316| ``'c'`` | Single character (accepts integer or single | | 2317| | character string). | | 2318+------------+-----------------------------------------------------+-------+ 2319| ``'r'`` | String (converts any Python object using | \(5) | 2320| | :func:`repr`). | | 2321+------------+-----------------------------------------------------+-------+ 2322| ``'s'`` | String (converts any Python object using | \(5) | 2323| | :func:`str`). | | 2324+------------+-----------------------------------------------------+-------+ 2325| ``'a'`` | String (converts any Python object using | \(5) | 2326| | :func:`ascii`). | | 2327+------------+-----------------------------------------------------+-------+ 2328| ``'%'`` | No argument is converted, results in a ``'%'`` | | 2329| | character in the result. | | 2330+------------+-----------------------------------------------------+-------+ 2331 2332Notes: 2333 2334(1) 2335 The alternate form causes a leading octal specifier (``'0o'``) to be 2336 inserted before the first digit. 2337 2338(2) 2339 The alternate form causes a leading ``'0x'`` or ``'0X'`` (depending on whether 2340 the ``'x'`` or ``'X'`` format was used) to be inserted before the first digit. 2341 2342(3) 2343 The alternate form causes the result to always contain a decimal point, even if 2344 no digits follow it. 2345 2346 The precision determines the number of digits after the decimal point and 2347 defaults to 6. 2348 2349(4) 2350 The alternate form causes the result to always contain a decimal point, and 2351 trailing zeroes are not removed as they would otherwise be. 2352 2353 The precision determines the number of significant digits before and after the 2354 decimal point and defaults to 6. 2355 2356(5) 2357 If precision is ``N``, the output is truncated to ``N`` characters. 2358 2359(6) 2360 See :pep:`237`. 2361 2362Since Python strings have an explicit length, ``%s`` conversions do not assume 2363that ``'\0'`` is the end of the string. 2364 2365.. XXX Examples? 2366 2367.. versionchanged:: 3.1 2368 ``%f`` conversions for numbers whose absolute value is over 1e50 are no 2369 longer replaced by ``%g`` conversions. 2370 2371 2372.. index:: 2373 single: buffer protocol; binary sequence types 2374 2375.. _binaryseq: 2376 2377Binary Sequence Types --- :class:`bytes`, :class:`bytearray`, :class:`memoryview` 2378================================================================================= 2379 2380.. index:: 2381 object: bytes 2382 object: bytearray 2383 object: memoryview 2384 module: array 2385 2386The core built-in types for manipulating binary data are :class:`bytes` and 2387:class:`bytearray`. They are supported by :class:`memoryview` which uses 2388the :ref:`buffer protocol <bufferobjects>` to access the memory of other 2389binary objects without needing to make a copy. 2390 2391The :mod:`array` module supports efficient storage of basic data types like 239232-bit integers and IEEE754 double-precision floating values. 2393 2394.. _typebytes: 2395 2396Bytes Objects 2397------------- 2398 2399.. index:: object: bytes 2400 2401Bytes objects are immutable sequences of single bytes. Since many major 2402binary protocols are based on the ASCII text encoding, bytes objects offer 2403several methods that are only valid when working with ASCII compatible 2404data and are closely related to string objects in a variety of other ways. 2405 2406.. class:: bytes([source[, encoding[, errors]]]) 2407 2408 Firstly, the syntax for bytes literals is largely the same as that for string 2409 literals, except that a ``b`` prefix is added: 2410 2411 * Single quotes: ``b'still allows embedded "double" quotes'`` 2412 * Double quotes: ``b"still allows embedded 'single' quotes"``. 2413 * Triple quoted: ``b'''3 single quotes'''``, ``b"""3 double quotes"""`` 2414 2415 Only ASCII characters are permitted in bytes literals (regardless of the 2416 declared source code encoding). Any binary values over 127 must be entered 2417 into bytes literals using the appropriate escape sequence. 2418 2419 As with string literals, bytes literals may also use a ``r`` prefix to disable 2420 processing of escape sequences. See :ref:`strings` for more about the various 2421 forms of bytes literal, including supported escape sequences. 2422 2423 While bytes literals and representations are based on ASCII text, bytes 2424 objects actually behave like immutable sequences of integers, with each 2425 value in the sequence restricted such that ``0 <= x < 256`` (attempts to 2426 violate this restriction will trigger :exc:`ValueError`). This is done 2427 deliberately to emphasise that while many binary formats include ASCII based 2428 elements and can be usefully manipulated with some text-oriented algorithms, 2429 this is not generally the case for arbitrary binary data (blindly applying 2430 text processing algorithms to binary data formats that are not ASCII 2431 compatible will usually lead to data corruption). 2432 2433 In addition to the literal forms, bytes objects can be created in a number of 2434 other ways: 2435 2436 * A zero-filled bytes object of a specified length: ``bytes(10)`` 2437 * From an iterable of integers: ``bytes(range(20))`` 2438 * Copying existing binary data via the buffer protocol: ``bytes(obj)`` 2439 2440 Also see the :ref:`bytes <func-bytes>` built-in. 2441 2442 Since 2 hexadecimal digits correspond precisely to a single byte, hexadecimal 2443 numbers are a commonly used format for describing binary data. Accordingly, 2444 the bytes type has an additional class method to read data in that format: 2445 2446 .. classmethod:: fromhex(string) 2447 2448 This :class:`bytes` class method returns a bytes object, decoding the 2449 given string object. The string must contain two hexadecimal digits per 2450 byte, with ASCII whitespace being ignored. 2451 2452 >>> bytes.fromhex('2Ef0 F1f2 ') 2453 b'.\xf0\xf1\xf2' 2454 2455 .. versionchanged:: 3.7 2456 :meth:`bytes.fromhex` now skips all ASCII whitespace in the string, 2457 not just spaces. 2458 2459 A reverse conversion function exists to transform a bytes object into its 2460 hexadecimal representation. 2461 2462 .. method:: hex([sep[, bytes_per_sep]]) 2463 2464 Return a string object containing two hexadecimal digits for each 2465 byte in the instance. 2466 2467 >>> b'\xf0\xf1\xf2'.hex() 2468 'f0f1f2' 2469 2470 If you want to make the hex string easier to read, you can specify a 2471 single character separator *sep* parameter to include in the output. 2472 By default between each byte. A second optional *bytes_per_sep* 2473 parameter controls the spacing. Positive values calculate the 2474 separator position from the right, negative values from the left. 2475 2476 >>> value = b'\xf0\xf1\xf2' 2477 >>> value.hex('-') 2478 'f0-f1-f2' 2479 >>> value.hex('_', 2) 2480 'f0_f1f2' 2481 >>> b'UUDDLRLRAB'.hex(' ', -4) 2482 '55554444 4c524c52 4142' 2483 2484 .. versionadded:: 3.5 2485 2486 .. versionchanged:: 3.8 2487 :meth:`bytes.hex` now supports optional *sep* and *bytes_per_sep* 2488 parameters to insert separators between bytes in the hex output. 2489 2490Since bytes objects are sequences of integers (akin to a tuple), for a bytes 2491object *b*, ``b[0]`` will be an integer, while ``b[0:1]`` will be a bytes 2492object of length 1. (This contrasts with text strings, where both indexing 2493and slicing will produce a string of length 1) 2494 2495The representation of bytes objects uses the literal format (``b'...'``) 2496since it is often more useful than e.g. ``bytes([46, 46, 46])``. You can 2497always convert a bytes object into a list of integers using ``list(b)``. 2498 2499.. note:: 2500 For Python 2.x users: In the Python 2.x series, a variety of implicit 2501 conversions between 8-bit strings (the closest thing 2.x offers to a 2502 built-in binary data type) and Unicode strings were permitted. This was a 2503 backwards compatibility workaround to account for the fact that Python 2504 originally only supported 8-bit text, and Unicode text was a later 2505 addition. In Python 3.x, those implicit conversions are gone - conversions 2506 between 8-bit binary data and Unicode text must be explicit, and bytes and 2507 string objects will always compare unequal. 2508 2509 2510.. _typebytearray: 2511 2512Bytearray Objects 2513----------------- 2514 2515.. index:: object: bytearray 2516 2517:class:`bytearray` objects are a mutable counterpart to :class:`bytes` 2518objects. 2519 2520.. class:: bytearray([source[, encoding[, errors]]]) 2521 2522 There is no dedicated literal syntax for bytearray objects, instead 2523 they are always created by calling the constructor: 2524 2525 * Creating an empty instance: ``bytearray()`` 2526 * Creating a zero-filled instance with a given length: ``bytearray(10)`` 2527 * From an iterable of integers: ``bytearray(range(20))`` 2528 * Copying existing binary data via the buffer protocol: ``bytearray(b'Hi!')`` 2529 2530 As bytearray objects are mutable, they support the 2531 :ref:`mutable <typesseq-mutable>` sequence operations in addition to the 2532 common bytes and bytearray operations described in :ref:`bytes-methods`. 2533 2534 Also see the :ref:`bytearray <func-bytearray>` built-in. 2535 2536 Since 2 hexadecimal digits correspond precisely to a single byte, hexadecimal 2537 numbers are a commonly used format for describing binary data. Accordingly, 2538 the bytearray type has an additional class method to read data in that format: 2539 2540 .. classmethod:: fromhex(string) 2541 2542 This :class:`bytearray` class method returns bytearray object, decoding 2543 the given string object. The string must contain two hexadecimal digits 2544 per byte, with ASCII whitespace being ignored. 2545 2546 >>> bytearray.fromhex('2Ef0 F1f2 ') 2547 bytearray(b'.\xf0\xf1\xf2') 2548 2549 .. versionchanged:: 3.7 2550 :meth:`bytearray.fromhex` now skips all ASCII whitespace in the string, 2551 not just spaces. 2552 2553 A reverse conversion function exists to transform a bytearray object into its 2554 hexadecimal representation. 2555 2556 .. method:: hex([sep[, bytes_per_sep]]) 2557 2558 Return a string object containing two hexadecimal digits for each 2559 byte in the instance. 2560 2561 >>> bytearray(b'\xf0\xf1\xf2').hex() 2562 'f0f1f2' 2563 2564 .. versionadded:: 3.5 2565 2566 .. versionchanged:: 3.8 2567 Similar to :meth:`bytes.hex`, :meth:`bytearray.hex` now supports 2568 optional *sep* and *bytes_per_sep* parameters to insert separators 2569 between bytes in the hex output. 2570 2571Since bytearray objects are sequences of integers (akin to a list), for a 2572bytearray object *b*, ``b[0]`` will be an integer, while ``b[0:1]`` will be 2573a bytearray object of length 1. (This contrasts with text strings, where 2574both indexing and slicing will produce a string of length 1) 2575 2576The representation of bytearray objects uses the bytes literal format 2577(``bytearray(b'...')``) since it is often more useful than e.g. 2578``bytearray([46, 46, 46])``. You can always convert a bytearray object into 2579a list of integers using ``list(b)``. 2580 2581 2582.. _bytes-methods: 2583 2584Bytes and Bytearray Operations 2585------------------------------ 2586 2587.. index:: pair: bytes; methods 2588 pair: bytearray; methods 2589 2590Both bytes and bytearray objects support the :ref:`common <typesseq-common>` 2591sequence operations. They interoperate not just with operands of the same 2592type, but with any :term:`bytes-like object`. Due to this flexibility, they can be 2593freely mixed in operations without causing errors. However, the return type 2594of the result may depend on the order of operands. 2595 2596.. note:: 2597 2598 The methods on bytes and bytearray objects don't accept strings as their 2599 arguments, just as the methods on strings don't accept bytes as their 2600 arguments. For example, you have to write:: 2601 2602 a = "abc" 2603 b = a.replace("a", "f") 2604 2605 and:: 2606 2607 a = b"abc" 2608 b = a.replace(b"a", b"f") 2609 2610Some bytes and bytearray operations assume the use of ASCII compatible 2611binary formats, and hence should be avoided when working with arbitrary 2612binary data. These restrictions are covered below. 2613 2614.. note:: 2615 Using these ASCII based operations to manipulate binary data that is not 2616 stored in an ASCII based format may lead to data corruption. 2617 2618The following methods on bytes and bytearray objects can be used with 2619arbitrary binary data. 2620 2621.. method:: bytes.count(sub[, start[, end]]) 2622 bytearray.count(sub[, start[, end]]) 2623 2624 Return the number of non-overlapping occurrences of subsequence *sub* in 2625 the range [*start*, *end*]. Optional arguments *start* and *end* are 2626 interpreted as in slice notation. 2627 2628 The subsequence to search for may be any :term:`bytes-like object` or an 2629 integer in the range 0 to 255. 2630 2631 .. versionchanged:: 3.3 2632 Also accept an integer in the range 0 to 255 as the subsequence. 2633 2634 2635.. method:: bytes.removeprefix(prefix, /) 2636 bytearray.removeprefix(prefix, /) 2637 2638 If the binary data starts with the *prefix* string, return 2639 ``bytes[len(prefix):]``. Otherwise, return a copy of the original 2640 binary data:: 2641 2642 >>> b'TestHook'.removeprefix(b'Test') 2643 b'Hook' 2644 >>> b'BaseTestCase'.removeprefix(b'Test') 2645 b'BaseTestCase' 2646 2647 The *prefix* may be any :term:`bytes-like object`. 2648 2649 .. note:: 2650 2651 The bytearray version of this method does *not* operate in place - 2652 it always produces a new object, even if no changes were made. 2653 2654 .. versionadded:: 3.9 2655 2656 2657.. method:: bytes.removesuffix(suffix, /) 2658 bytearray.removesuffix(suffix, /) 2659 2660 If the binary data ends with the *suffix* string and that *suffix* is 2661 not empty, return ``bytes[:-len(suffix)]``. Otherwise, return a copy of 2662 the original binary data:: 2663 2664 >>> b'MiscTests'.removesuffix(b'Tests') 2665 b'Misc' 2666 >>> b'TmpDirMixin'.removesuffix(b'Tests') 2667 b'TmpDirMixin' 2668 2669 The *suffix* may be any :term:`bytes-like object`. 2670 2671 .. note:: 2672 2673 The bytearray version of this method does *not* operate in place - 2674 it always produces a new object, even if no changes were made. 2675 2676 .. versionadded:: 3.9 2677 2678 2679.. method:: bytes.decode(encoding="utf-8", errors="strict") 2680 bytearray.decode(encoding="utf-8", errors="strict") 2681 2682 Return a string decoded from the given bytes. Default encoding is 2683 ``'utf-8'``. *errors* may be given to set a different 2684 error handling scheme. The default for *errors* is ``'strict'``, meaning 2685 that encoding errors raise a :exc:`UnicodeError`. Other possible values are 2686 ``'ignore'``, ``'replace'`` and any other name registered via 2687 :func:`codecs.register_error`, see section :ref:`error-handlers`. For a 2688 list of possible encodings, see section :ref:`standard-encodings`. 2689 2690 By default, the *errors* argument is not checked for best performances, but 2691 only used at the first decoding error. Enable the :ref:`Python Development 2692 Mode <devmode>`, or use a debug build to check *errors*. 2693 2694 .. note:: 2695 2696 Passing the *encoding* argument to :class:`str` allows decoding any 2697 :term:`bytes-like object` directly, without needing to make a temporary 2698 bytes or bytearray object. 2699 2700 .. versionchanged:: 3.1 2701 Added support for keyword arguments. 2702 2703 .. versionchanged:: 3.9 2704 The *errors* is now checked in development mode and in debug mode. 2705 2706 2707.. method:: bytes.endswith(suffix[, start[, end]]) 2708 bytearray.endswith(suffix[, start[, end]]) 2709 2710 Return ``True`` if the binary data ends with the specified *suffix*, 2711 otherwise return ``False``. *suffix* can also be a tuple of suffixes to 2712 look for. With optional *start*, test beginning at that position. With 2713 optional *end*, stop comparing at that position. 2714 2715 The suffix(es) to search for may be any :term:`bytes-like object`. 2716 2717 2718.. method:: bytes.find(sub[, start[, end]]) 2719 bytearray.find(sub[, start[, end]]) 2720 2721 Return the lowest index in the data where the subsequence *sub* is found, 2722 such that *sub* is contained in the slice ``s[start:end]``. Optional 2723 arguments *start* and *end* are interpreted as in slice notation. Return 2724 ``-1`` if *sub* is not found. 2725 2726 The subsequence to search for may be any :term:`bytes-like object` or an 2727 integer in the range 0 to 255. 2728 2729 .. note:: 2730 2731 The :meth:`~bytes.find` method should be used only if you need to know the 2732 position of *sub*. To check if *sub* is a substring or not, use the 2733 :keyword:`in` operator:: 2734 2735 >>> b'Py' in b'Python' 2736 True 2737 2738 .. versionchanged:: 3.3 2739 Also accept an integer in the range 0 to 255 as the subsequence. 2740 2741 2742.. method:: bytes.index(sub[, start[, end]]) 2743 bytearray.index(sub[, start[, end]]) 2744 2745 Like :meth:`~bytes.find`, but raise :exc:`ValueError` when the 2746 subsequence is not found. 2747 2748 The subsequence to search for may be any :term:`bytes-like object` or an 2749 integer in the range 0 to 255. 2750 2751 .. versionchanged:: 3.3 2752 Also accept an integer in the range 0 to 255 as the subsequence. 2753 2754 2755.. method:: bytes.join(iterable) 2756 bytearray.join(iterable) 2757 2758 Return a bytes or bytearray object which is the concatenation of the 2759 binary data sequences in *iterable*. A :exc:`TypeError` will be raised 2760 if there are any values in *iterable* that are not :term:`bytes-like 2761 objects <bytes-like object>`, including :class:`str` objects. The 2762 separator between elements is the contents of the bytes or 2763 bytearray object providing this method. 2764 2765 2766.. staticmethod:: bytes.maketrans(from, to) 2767 bytearray.maketrans(from, to) 2768 2769 This static method returns a translation table usable for 2770 :meth:`bytes.translate` that will map each character in *from* into the 2771 character at the same position in *to*; *from* and *to* must both be 2772 :term:`bytes-like objects <bytes-like object>` and have the same length. 2773 2774 .. versionadded:: 3.1 2775 2776 2777.. method:: bytes.partition(sep) 2778 bytearray.partition(sep) 2779 2780 Split the sequence at the first occurrence of *sep*, and return a 3-tuple 2781 containing the part before the separator, the separator itself or its 2782 bytearray copy, and the part after the separator. 2783 If the separator is not found, return a 3-tuple 2784 containing a copy of the original sequence, followed by two empty bytes or 2785 bytearray objects. 2786 2787 The separator to search for may be any :term:`bytes-like object`. 2788 2789 2790.. method:: bytes.replace(old, new[, count]) 2791 bytearray.replace(old, new[, count]) 2792 2793 Return a copy of the sequence with all occurrences of subsequence *old* 2794 replaced by *new*. If the optional argument *count* is given, only the 2795 first *count* occurrences are replaced. 2796 2797 The subsequence to search for and its replacement may be any 2798 :term:`bytes-like object`. 2799 2800 .. note:: 2801 2802 The bytearray version of this method does *not* operate in place - it 2803 always produces a new object, even if no changes were made. 2804 2805 2806.. method:: bytes.rfind(sub[, start[, end]]) 2807 bytearray.rfind(sub[, start[, end]]) 2808 2809 Return the highest index in the sequence where the subsequence *sub* is 2810 found, such that *sub* is contained within ``s[start:end]``. Optional 2811 arguments *start* and *end* are interpreted as in slice notation. Return 2812 ``-1`` on failure. 2813 2814 The subsequence to search for may be any :term:`bytes-like object` or an 2815 integer in the range 0 to 255. 2816 2817 .. versionchanged:: 3.3 2818 Also accept an integer in the range 0 to 255 as the subsequence. 2819 2820 2821.. method:: bytes.rindex(sub[, start[, end]]) 2822 bytearray.rindex(sub[, start[, end]]) 2823 2824 Like :meth:`~bytes.rfind` but raises :exc:`ValueError` when the 2825 subsequence *sub* is not found. 2826 2827 The subsequence to search for may be any :term:`bytes-like object` or an 2828 integer in the range 0 to 255. 2829 2830 .. versionchanged:: 3.3 2831 Also accept an integer in the range 0 to 255 as the subsequence. 2832 2833 2834.. method:: bytes.rpartition(sep) 2835 bytearray.rpartition(sep) 2836 2837 Split the sequence at the last occurrence of *sep*, and return a 3-tuple 2838 containing the part before the separator, the separator itself or its 2839 bytearray copy, and the part after the separator. 2840 If the separator is not found, return a 3-tuple 2841 containing two empty bytes or bytearray objects, followed by a copy of the 2842 original sequence. 2843 2844 The separator to search for may be any :term:`bytes-like object`. 2845 2846 2847.. method:: bytes.startswith(prefix[, start[, end]]) 2848 bytearray.startswith(prefix[, start[, end]]) 2849 2850 Return ``True`` if the binary data starts with the specified *prefix*, 2851 otherwise return ``False``. *prefix* can also be a tuple of prefixes to 2852 look for. With optional *start*, test beginning at that position. With 2853 optional *end*, stop comparing at that position. 2854 2855 The prefix(es) to search for may be any :term:`bytes-like object`. 2856 2857 2858.. method:: bytes.translate(table, /, delete=b'') 2859 bytearray.translate(table, /, delete=b'') 2860 2861 Return a copy of the bytes or bytearray object where all bytes occurring in 2862 the optional argument *delete* are removed, and the remaining bytes have 2863 been mapped through the given translation table, which must be a bytes 2864 object of length 256. 2865 2866 You can use the :func:`bytes.maketrans` method to create a translation 2867 table. 2868 2869 Set the *table* argument to ``None`` for translations that only delete 2870 characters:: 2871 2872 >>> b'read this short text'.translate(None, b'aeiou') 2873 b'rd ths shrt txt' 2874 2875 .. versionchanged:: 3.6 2876 *delete* is now supported as a keyword argument. 2877 2878 2879The following methods on bytes and bytearray objects have default behaviours 2880that assume the use of ASCII compatible binary formats, but can still be used 2881with arbitrary binary data by passing appropriate arguments. Note that all of 2882the bytearray methods in this section do *not* operate in place, and instead 2883produce new objects. 2884 2885.. method:: bytes.center(width[, fillbyte]) 2886 bytearray.center(width[, fillbyte]) 2887 2888 Return a copy of the object centered in a sequence of length *width*. 2889 Padding is done using the specified *fillbyte* (default is an ASCII 2890 space). For :class:`bytes` objects, the original sequence is returned if 2891 *width* is less than or equal to ``len(s)``. 2892 2893 .. note:: 2894 2895 The bytearray version of this method does *not* operate in place - 2896 it always produces a new object, even if no changes were made. 2897 2898 2899.. method:: bytes.ljust(width[, fillbyte]) 2900 bytearray.ljust(width[, fillbyte]) 2901 2902 Return a copy of the object left justified in a sequence of length *width*. 2903 Padding is done using the specified *fillbyte* (default is an ASCII 2904 space). For :class:`bytes` objects, the original sequence is returned if 2905 *width* is less than or equal to ``len(s)``. 2906 2907 .. note:: 2908 2909 The bytearray version of this method does *not* operate in place - 2910 it always produces a new object, even if no changes were made. 2911 2912 2913.. method:: bytes.lstrip([chars]) 2914 bytearray.lstrip([chars]) 2915 2916 Return a copy of the sequence with specified leading bytes removed. The 2917 *chars* argument is a binary sequence specifying the set of byte values to 2918 be removed - the name refers to the fact this method is usually used with 2919 ASCII characters. If omitted or ``None``, the *chars* argument defaults 2920 to removing ASCII whitespace. The *chars* argument is not a prefix; 2921 rather, all combinations of its values are stripped:: 2922 2923 >>> b' spacious '.lstrip() 2924 b'spacious ' 2925 >>> b'www.example.com'.lstrip(b'cmowz.') 2926 b'example.com' 2927 2928 The binary sequence of byte values to remove may be any 2929 :term:`bytes-like object`. See :meth:`~bytes.removeprefix` for a method 2930 that will remove a single prefix string rather than all of a set of 2931 characters. For example:: 2932 2933 >>> b'Arthur: three!'.lstrip(b'Arthur: ') 2934 b'ee!' 2935 >>> b'Arthur: three!'.removeprefix(b'Arthur: ') 2936 b'three!' 2937 2938 .. note:: 2939 2940 The bytearray version of this method does *not* operate in place - 2941 it always produces a new object, even if no changes were made. 2942 2943 2944.. method:: bytes.rjust(width[, fillbyte]) 2945 bytearray.rjust(width[, fillbyte]) 2946 2947 Return a copy of the object right justified in a sequence of length *width*. 2948 Padding is done using the specified *fillbyte* (default is an ASCII 2949 space). For :class:`bytes` objects, the original sequence is returned if 2950 *width* is less than or equal to ``len(s)``. 2951 2952 .. note:: 2953 2954 The bytearray version of this method does *not* operate in place - 2955 it always produces a new object, even if no changes were made. 2956 2957 2958.. method:: bytes.rsplit(sep=None, maxsplit=-1) 2959 bytearray.rsplit(sep=None, maxsplit=-1) 2960 2961 Split the binary sequence into subsequences of the same type, using *sep* 2962 as the delimiter string. If *maxsplit* is given, at most *maxsplit* splits 2963 are done, the *rightmost* ones. If *sep* is not specified or ``None``, 2964 any subsequence consisting solely of ASCII whitespace is a separator. 2965 Except for splitting from the right, :meth:`rsplit` behaves like 2966 :meth:`split` which is described in detail below. 2967 2968 2969.. method:: bytes.rstrip([chars]) 2970 bytearray.rstrip([chars]) 2971 2972 Return a copy of the sequence with specified trailing bytes removed. The 2973 *chars* argument is a binary sequence specifying the set of byte values to 2974 be removed - the name refers to the fact this method is usually used with 2975 ASCII characters. If omitted or ``None``, the *chars* argument defaults to 2976 removing ASCII whitespace. The *chars* argument is not a suffix; rather, 2977 all combinations of its values are stripped:: 2978 2979 >>> b' spacious '.rstrip() 2980 b' spacious' 2981 >>> b'mississippi'.rstrip(b'ipz') 2982 b'mississ' 2983 2984 The binary sequence of byte values to remove may be any 2985 :term:`bytes-like object`. See :meth:`~bytes.removesuffix` for a method 2986 that will remove a single suffix string rather than all of a set of 2987 characters. For example:: 2988 2989 >>> b'Monty Python'.rstrip(b' Python') 2990 b'M' 2991 >>> b'Monty Python'.removesuffix(b' Python') 2992 b'Monty' 2993 2994 .. note:: 2995 2996 The bytearray version of this method does *not* operate in place - 2997 it always produces a new object, even if no changes were made. 2998 2999 3000.. method:: bytes.split(sep=None, maxsplit=-1) 3001 bytearray.split(sep=None, maxsplit=-1) 3002 3003 Split the binary sequence into subsequences of the same type, using *sep* 3004 as the delimiter string. If *maxsplit* is given and non-negative, at most 3005 *maxsplit* splits are done (thus, the list will have at most ``maxsplit+1`` 3006 elements). If *maxsplit* is not specified or is ``-1``, then there is no 3007 limit on the number of splits (all possible splits are made). 3008 3009 If *sep* is given, consecutive delimiters are not grouped together and are 3010 deemed to delimit empty subsequences (for example, ``b'1,,2'.split(b',')`` 3011 returns ``[b'1', b'', b'2']``). The *sep* argument may consist of a 3012 multibyte sequence (for example, ``b'1<>2<>3'.split(b'<>')`` returns 3013 ``[b'1', b'2', b'3']``). Splitting an empty sequence with a specified 3014 separator returns ``[b'']`` or ``[bytearray(b'')]`` depending on the type 3015 of object being split. The *sep* argument may be any 3016 :term:`bytes-like object`. 3017 3018 For example:: 3019 3020 >>> b'1,2,3'.split(b',') 3021 [b'1', b'2', b'3'] 3022 >>> b'1,2,3'.split(b',', maxsplit=1) 3023 [b'1', b'2,3'] 3024 >>> b'1,2,,3,'.split(b',') 3025 [b'1', b'2', b'', b'3', b''] 3026 3027 If *sep* is not specified or is ``None``, a different splitting algorithm 3028 is applied: runs of consecutive ASCII whitespace are regarded as a single 3029 separator, and the result will contain no empty strings at the start or 3030 end if the sequence has leading or trailing whitespace. Consequently, 3031 splitting an empty sequence or a sequence consisting solely of ASCII 3032 whitespace without a specified separator returns ``[]``. 3033 3034 For example:: 3035 3036 3037 >>> b'1 2 3'.split() 3038 [b'1', b'2', b'3'] 3039 >>> b'1 2 3'.split(maxsplit=1) 3040 [b'1', b'2 3'] 3041 >>> b' 1 2 3 '.split() 3042 [b'1', b'2', b'3'] 3043 3044 3045.. method:: bytes.strip([chars]) 3046 bytearray.strip([chars]) 3047 3048 Return a copy of the sequence with specified leading and trailing bytes 3049 removed. The *chars* argument is a binary sequence specifying the set of 3050 byte values to be removed - the name refers to the fact this method is 3051 usually used with ASCII characters. If omitted or ``None``, the *chars* 3052 argument defaults to removing ASCII whitespace. The *chars* argument is 3053 not a prefix or suffix; rather, all combinations of its values are 3054 stripped:: 3055 3056 >>> b' spacious '.strip() 3057 b'spacious' 3058 >>> b'www.example.com'.strip(b'cmowz.') 3059 b'example' 3060 3061 The binary sequence of byte values to remove may be any 3062 :term:`bytes-like object`. 3063 3064 .. note:: 3065 3066 The bytearray version of this method does *not* operate in place - 3067 it always produces a new object, even if no changes were made. 3068 3069 3070The following methods on bytes and bytearray objects assume the use of ASCII 3071compatible binary formats and should not be applied to arbitrary binary data. 3072Note that all of the bytearray methods in this section do *not* operate in 3073place, and instead produce new objects. 3074 3075.. method:: bytes.capitalize() 3076 bytearray.capitalize() 3077 3078 Return a copy of the sequence with each byte interpreted as an ASCII 3079 character, and the first byte capitalized and the rest lowercased. 3080 Non-ASCII byte values are passed through unchanged. 3081 3082 .. note:: 3083 3084 The bytearray version of this method does *not* operate in place - it 3085 always produces a new object, even if no changes were made. 3086 3087 3088.. method:: bytes.expandtabs(tabsize=8) 3089 bytearray.expandtabs(tabsize=8) 3090 3091 Return a copy of the sequence where all ASCII tab characters are replaced 3092 by one or more ASCII spaces, depending on the current column and the given 3093 tab size. Tab positions occur every *tabsize* bytes (default is 8, 3094 giving tab positions at columns 0, 8, 16 and so on). To expand the 3095 sequence, the current column is set to zero and the sequence is examined 3096 byte by byte. If the byte is an ASCII tab character (``b'\t'``), one or 3097 more space characters are inserted in the result until the current column 3098 is equal to the next tab position. (The tab character itself is not 3099 copied.) If the current byte is an ASCII newline (``b'\n'``) or 3100 carriage return (``b'\r'``), it is copied and the current column is reset 3101 to zero. Any other byte value is copied unchanged and the current column 3102 is incremented by one regardless of how the byte value is represented when 3103 printed:: 3104 3105 >>> b'01\t012\t0123\t01234'.expandtabs() 3106 b'01 012 0123 01234' 3107 >>> b'01\t012\t0123\t01234'.expandtabs(4) 3108 b'01 012 0123 01234' 3109 3110 .. note:: 3111 3112 The bytearray version of this method does *not* operate in place - it 3113 always produces a new object, even if no changes were made. 3114 3115 3116.. method:: bytes.isalnum() 3117 bytearray.isalnum() 3118 3119 Return ``True`` if all bytes in the sequence are alphabetical ASCII characters 3120 or ASCII decimal digits and the sequence is not empty, ``False`` otherwise. 3121 Alphabetic ASCII characters are those byte values in the sequence 3122 ``b'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'``. ASCII decimal 3123 digits are those byte values in the sequence ``b'0123456789'``. 3124 3125 For example:: 3126 3127 >>> b'ABCabc1'.isalnum() 3128 True 3129 >>> b'ABC abc1'.isalnum() 3130 False 3131 3132 3133.. method:: bytes.isalpha() 3134 bytearray.isalpha() 3135 3136 Return ``True`` if all bytes in the sequence are alphabetic ASCII characters 3137 and the sequence is not empty, ``False`` otherwise. Alphabetic ASCII 3138 characters are those byte values in the sequence 3139 ``b'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'``. 3140 3141 For example:: 3142 3143 >>> b'ABCabc'.isalpha() 3144 True 3145 >>> b'ABCabc1'.isalpha() 3146 False 3147 3148 3149.. method:: bytes.isascii() 3150 bytearray.isascii() 3151 3152 Return ``True`` if the sequence is empty or all bytes in the sequence are ASCII, 3153 ``False`` otherwise. 3154 ASCII bytes are in the range 0-0x7F. 3155 3156 .. versionadded:: 3.7 3157 3158 3159.. method:: bytes.isdigit() 3160 bytearray.isdigit() 3161 3162 Return ``True`` if all bytes in the sequence are ASCII decimal digits 3163 and the sequence is not empty, ``False`` otherwise. ASCII decimal digits are 3164 those byte values in the sequence ``b'0123456789'``. 3165 3166 For example:: 3167 3168 >>> b'1234'.isdigit() 3169 True 3170 >>> b'1.23'.isdigit() 3171 False 3172 3173 3174.. method:: bytes.islower() 3175 bytearray.islower() 3176 3177 Return ``True`` if there is at least one lowercase ASCII character 3178 in the sequence and no uppercase ASCII characters, ``False`` otherwise. 3179 3180 For example:: 3181 3182 >>> b'hello world'.islower() 3183 True 3184 >>> b'Hello world'.islower() 3185 False 3186 3187 Lowercase ASCII characters are those byte values in the sequence 3188 ``b'abcdefghijklmnopqrstuvwxyz'``. Uppercase ASCII characters 3189 are those byte values in the sequence ``b'ABCDEFGHIJKLMNOPQRSTUVWXYZ'``. 3190 3191 3192.. method:: bytes.isspace() 3193 bytearray.isspace() 3194 3195 Return ``True`` if all bytes in the sequence are ASCII whitespace and the 3196 sequence is not empty, ``False`` otherwise. ASCII whitespace characters are 3197 those byte values in the sequence ``b' \t\n\r\x0b\f'`` (space, tab, newline, 3198 carriage return, vertical tab, form feed). 3199 3200 3201.. method:: bytes.istitle() 3202 bytearray.istitle() 3203 3204 Return ``True`` if the sequence is ASCII titlecase and the sequence is not 3205 empty, ``False`` otherwise. See :meth:`bytes.title` for more details on the 3206 definition of "titlecase". 3207 3208 For example:: 3209 3210 >>> b'Hello World'.istitle() 3211 True 3212 >>> b'Hello world'.istitle() 3213 False 3214 3215 3216.. method:: bytes.isupper() 3217 bytearray.isupper() 3218 3219 Return ``True`` if there is at least one uppercase alphabetic ASCII character 3220 in the sequence and no lowercase ASCII characters, ``False`` otherwise. 3221 3222 For example:: 3223 3224 >>> b'HELLO WORLD'.isupper() 3225 True 3226 >>> b'Hello world'.isupper() 3227 False 3228 3229 Lowercase ASCII characters are those byte values in the sequence 3230 ``b'abcdefghijklmnopqrstuvwxyz'``. Uppercase ASCII characters 3231 are those byte values in the sequence ``b'ABCDEFGHIJKLMNOPQRSTUVWXYZ'``. 3232 3233 3234.. method:: bytes.lower() 3235 bytearray.lower() 3236 3237 Return a copy of the sequence with all the uppercase ASCII characters 3238 converted to their corresponding lowercase counterpart. 3239 3240 For example:: 3241 3242 >>> b'Hello World'.lower() 3243 b'hello world' 3244 3245 Lowercase ASCII characters are those byte values in the sequence 3246 ``b'abcdefghijklmnopqrstuvwxyz'``. Uppercase ASCII characters 3247 are those byte values in the sequence ``b'ABCDEFGHIJKLMNOPQRSTUVWXYZ'``. 3248 3249 .. note:: 3250 3251 The bytearray version of this method does *not* operate in place - it 3252 always produces a new object, even if no changes were made. 3253 3254 3255.. index:: 3256 single: universal newlines; bytes.splitlines method 3257 single: universal newlines; bytearray.splitlines method 3258 3259.. method:: bytes.splitlines(keepends=False) 3260 bytearray.splitlines(keepends=False) 3261 3262 Return a list of the lines in the binary sequence, breaking at ASCII 3263 line boundaries. This method uses the :term:`universal newlines` approach 3264 to splitting lines. Line breaks are not included in the resulting list 3265 unless *keepends* is given and true. 3266 3267 For example:: 3268 3269 >>> b'ab c\n\nde fg\rkl\r\n'.splitlines() 3270 [b'ab c', b'', b'de fg', b'kl'] 3271 >>> b'ab c\n\nde fg\rkl\r\n'.splitlines(keepends=True) 3272 [b'ab c\n', b'\n', b'de fg\r', b'kl\r\n'] 3273 3274 Unlike :meth:`~bytes.split` when a delimiter string *sep* is given, this 3275 method returns an empty list for the empty string, and a terminal line 3276 break does not result in an extra line:: 3277 3278 >>> b"".split(b'\n'), b"Two lines\n".split(b'\n') 3279 ([b''], [b'Two lines', b'']) 3280 >>> b"".splitlines(), b"One line\n".splitlines() 3281 ([], [b'One line']) 3282 3283 3284.. method:: bytes.swapcase() 3285 bytearray.swapcase() 3286 3287 Return a copy of the sequence with all the lowercase ASCII characters 3288 converted to their corresponding uppercase counterpart and vice-versa. 3289 3290 For example:: 3291 3292 >>> b'Hello World'.swapcase() 3293 b'hELLO wORLD' 3294 3295 Lowercase ASCII characters are those byte values in the sequence 3296 ``b'abcdefghijklmnopqrstuvwxyz'``. Uppercase ASCII characters 3297 are those byte values in the sequence ``b'ABCDEFGHIJKLMNOPQRSTUVWXYZ'``. 3298 3299 Unlike :func:`str.swapcase()`, it is always the case that 3300 ``bin.swapcase().swapcase() == bin`` for the binary versions. Case 3301 conversions are symmetrical in ASCII, even though that is not generally 3302 true for arbitrary Unicode code points. 3303 3304 .. note:: 3305 3306 The bytearray version of this method does *not* operate in place - it 3307 always produces a new object, even if no changes were made. 3308 3309 3310.. method:: bytes.title() 3311 bytearray.title() 3312 3313 Return a titlecased version of the binary sequence where words start with 3314 an uppercase ASCII character and the remaining characters are lowercase. 3315 Uncased byte values are left unmodified. 3316 3317 For example:: 3318 3319 >>> b'Hello world'.title() 3320 b'Hello World' 3321 3322 Lowercase ASCII characters are those byte values in the sequence 3323 ``b'abcdefghijklmnopqrstuvwxyz'``. Uppercase ASCII characters 3324 are those byte values in the sequence ``b'ABCDEFGHIJKLMNOPQRSTUVWXYZ'``. 3325 All other byte values are uncased. 3326 3327 The algorithm uses a simple language-independent definition of a word as 3328 groups of consecutive letters. The definition works in many contexts but 3329 it means that apostrophes in contractions and possessives form word 3330 boundaries, which may not be the desired result:: 3331 3332 >>> b"they're bill's friends from the UK".title() 3333 b"They'Re Bill'S Friends From The Uk" 3334 3335 A workaround for apostrophes can be constructed using regular expressions:: 3336 3337 >>> import re 3338 >>> def titlecase(s): 3339 ... return re.sub(rb"[A-Za-z]+('[A-Za-z]+)?", 3340 ... lambda mo: mo.group(0)[0:1].upper() + 3341 ... mo.group(0)[1:].lower(), 3342 ... s) 3343 ... 3344 >>> titlecase(b"they're bill's friends.") 3345 b"They're Bill's Friends." 3346 3347 .. note:: 3348 3349 The bytearray version of this method does *not* operate in place - it 3350 always produces a new object, even if no changes were made. 3351 3352 3353.. method:: bytes.upper() 3354 bytearray.upper() 3355 3356 Return a copy of the sequence with all the lowercase ASCII characters 3357 converted to their corresponding uppercase counterpart. 3358 3359 For example:: 3360 3361 >>> b'Hello World'.upper() 3362 b'HELLO WORLD' 3363 3364 Lowercase ASCII characters are those byte values in the sequence 3365 ``b'abcdefghijklmnopqrstuvwxyz'``. Uppercase ASCII characters 3366 are those byte values in the sequence ``b'ABCDEFGHIJKLMNOPQRSTUVWXYZ'``. 3367 3368 .. note:: 3369 3370 The bytearray version of this method does *not* operate in place - it 3371 always produces a new object, even if no changes were made. 3372 3373 3374.. method:: bytes.zfill(width) 3375 bytearray.zfill(width) 3376 3377 Return a copy of the sequence left filled with ASCII ``b'0'`` digits to 3378 make a sequence of length *width*. A leading sign prefix (``b'+'``/ 3379 ``b'-'``) is handled by inserting the padding *after* the sign character 3380 rather than before. For :class:`bytes` objects, the original sequence is 3381 returned if *width* is less than or equal to ``len(seq)``. 3382 3383 For example:: 3384 3385 >>> b"42".zfill(5) 3386 b'00042' 3387 >>> b"-42".zfill(5) 3388 b'-0042' 3389 3390 .. note:: 3391 3392 The bytearray version of this method does *not* operate in place - it 3393 always produces a new object, even if no changes were made. 3394 3395 3396.. _bytes-formatting: 3397 3398``printf``-style Bytes Formatting 3399---------------------------------- 3400 3401.. index:: 3402 single: formatting; bytes (%) 3403 single: formatting; bytearray (%) 3404 single: interpolation; bytes (%) 3405 single: interpolation; bytearray (%) 3406 single: bytes; formatting 3407 single: bytearray; formatting 3408 single: bytes; interpolation 3409 single: bytearray; interpolation 3410 single: printf-style formatting 3411 single: sprintf-style formatting 3412 single: % (percent); printf-style formatting 3413 3414.. note:: 3415 3416 The formatting operations described here exhibit a variety of quirks that 3417 lead to a number of common errors (such as failing to display tuples and 3418 dictionaries correctly). If the value being printed may be a tuple or 3419 dictionary, wrap it in a tuple. 3420 3421Bytes objects (``bytes``/``bytearray``) have one unique built-in operation: 3422the ``%`` operator (modulo). 3423This is also known as the bytes *formatting* or *interpolation* operator. 3424Given ``format % values`` (where *format* is a bytes object), ``%`` conversion 3425specifications in *format* are replaced with zero or more elements of *values*. 3426The effect is similar to using the :c:func:`sprintf` in the C language. 3427 3428If *format* requires a single argument, *values* may be a single non-tuple 3429object. [5]_ Otherwise, *values* must be a tuple with exactly the number of 3430items specified by the format bytes object, or a single mapping object (for 3431example, a dictionary). 3432 3433.. index:: 3434 single: () (parentheses); in printf-style formatting 3435 single: * (asterisk); in printf-style formatting 3436 single: . (dot); in printf-style formatting 3437 3438A conversion specifier contains two or more characters and has the following 3439components, which must occur in this order: 3440 3441#. The ``'%'`` character, which marks the start of the specifier. 3442 3443#. Mapping key (optional), consisting of a parenthesised sequence of characters 3444 (for example, ``(somename)``). 3445 3446#. Conversion flags (optional), which affect the result of some conversion 3447 types. 3448 3449#. Minimum field width (optional). If specified as an ``'*'`` (asterisk), the 3450 actual width is read from the next element of the tuple in *values*, and the 3451 object to convert comes after the minimum field width and optional precision. 3452 3453#. Precision (optional), given as a ``'.'`` (dot) followed by the precision. If 3454 specified as ``'*'`` (an asterisk), the actual precision is read from the next 3455 element of the tuple in *values*, and the value to convert comes after the 3456 precision. 3457 3458#. Length modifier (optional). 3459 3460#. Conversion type. 3461 3462When the right argument is a dictionary (or other mapping type), then the 3463formats in the bytes object *must* include a parenthesised mapping key into that 3464dictionary inserted immediately after the ``'%'`` character. The mapping key 3465selects the value to be formatted from the mapping. For example: 3466 3467 >>> print(b'%(language)s has %(number)03d quote types.' % 3468 ... {b'language': b"Python", b"number": 2}) 3469 b'Python has 002 quote types.' 3470 3471In this case no ``*`` specifiers may occur in a format (since they require a 3472sequential parameter list). 3473 3474The conversion flag characters are: 3475 3476.. index:: 3477 single: # (hash); in printf-style formatting 3478 single: - (minus); in printf-style formatting 3479 single: + (plus); in printf-style formatting 3480 single: space; in printf-style formatting 3481 3482+---------+---------------------------------------------------------------------+ 3483| Flag | Meaning | 3484+=========+=====================================================================+ 3485| ``'#'`` | The value conversion will use the "alternate form" (where defined | 3486| | below). | 3487+---------+---------------------------------------------------------------------+ 3488| ``'0'`` | The conversion will be zero padded for numeric values. | 3489+---------+---------------------------------------------------------------------+ 3490| ``'-'`` | The converted value is left adjusted (overrides the ``'0'`` | 3491| | conversion if both are given). | 3492+---------+---------------------------------------------------------------------+ 3493| ``' '`` | (a space) A blank should be left before a positive number (or empty | 3494| | string) produced by a signed conversion. | 3495+---------+---------------------------------------------------------------------+ 3496| ``'+'`` | A sign character (``'+'`` or ``'-'``) will precede the conversion | 3497| | (overrides a "space" flag). | 3498+---------+---------------------------------------------------------------------+ 3499 3500A length modifier (``h``, ``l``, or ``L``) may be present, but is ignored as it 3501is not necessary for Python -- so e.g. ``%ld`` is identical to ``%d``. 3502 3503The conversion types are: 3504 3505+------------+-----------------------------------------------------+-------+ 3506| Conversion | Meaning | Notes | 3507+============+=====================================================+=======+ 3508| ``'d'`` | Signed integer decimal. | | 3509+------------+-----------------------------------------------------+-------+ 3510| ``'i'`` | Signed integer decimal. | | 3511+------------+-----------------------------------------------------+-------+ 3512| ``'o'`` | Signed octal value. | \(1) | 3513+------------+-----------------------------------------------------+-------+ 3514| ``'u'`` | Obsolete type -- it is identical to ``'d'``. | \(8) | 3515+------------+-----------------------------------------------------+-------+ 3516| ``'x'`` | Signed hexadecimal (lowercase). | \(2) | 3517+------------+-----------------------------------------------------+-------+ 3518| ``'X'`` | Signed hexadecimal (uppercase). | \(2) | 3519+------------+-----------------------------------------------------+-------+ 3520| ``'e'`` | Floating point exponential format (lowercase). | \(3) | 3521+------------+-----------------------------------------------------+-------+ 3522| ``'E'`` | Floating point exponential format (uppercase). | \(3) | 3523+------------+-----------------------------------------------------+-------+ 3524| ``'f'`` | Floating point decimal format. | \(3) | 3525+------------+-----------------------------------------------------+-------+ 3526| ``'F'`` | Floating point decimal format. | \(3) | 3527+------------+-----------------------------------------------------+-------+ 3528| ``'g'`` | Floating point format. Uses lowercase exponential | \(4) | 3529| | format if exponent is less than -4 or not less than | | 3530| | precision, decimal format otherwise. | | 3531+------------+-----------------------------------------------------+-------+ 3532| ``'G'`` | Floating point format. Uses uppercase exponential | \(4) | 3533| | format if exponent is less than -4 or not less than | | 3534| | precision, decimal format otherwise. | | 3535+------------+-----------------------------------------------------+-------+ 3536| ``'c'`` | Single byte (accepts integer or single | | 3537| | byte objects). | | 3538+------------+-----------------------------------------------------+-------+ 3539| ``'b'`` | Bytes (any object that follows the | \(5) | 3540| | :ref:`buffer protocol <bufferobjects>` or has | | 3541| | :meth:`__bytes__`). | | 3542+------------+-----------------------------------------------------+-------+ 3543| ``'s'`` | ``'s'`` is an alias for ``'b'`` and should only | \(6) | 3544| | be used for Python2/3 code bases. | | 3545+------------+-----------------------------------------------------+-------+ 3546| ``'a'`` | Bytes (converts any Python object using | \(5) | 3547| | ``repr(obj).encode('ascii','backslashreplace)``). | | 3548+------------+-----------------------------------------------------+-------+ 3549| ``'r'`` | ``'r'`` is an alias for ``'a'`` and should only | \(7) | 3550| | be used for Python2/3 code bases. | | 3551+------------+-----------------------------------------------------+-------+ 3552| ``'%'`` | No argument is converted, results in a ``'%'`` | | 3553| | character in the result. | | 3554+------------+-----------------------------------------------------+-------+ 3555 3556Notes: 3557 3558(1) 3559 The alternate form causes a leading octal specifier (``'0o'``) to be 3560 inserted before the first digit. 3561 3562(2) 3563 The alternate form causes a leading ``'0x'`` or ``'0X'`` (depending on whether 3564 the ``'x'`` or ``'X'`` format was used) to be inserted before the first digit. 3565 3566(3) 3567 The alternate form causes the result to always contain a decimal point, even if 3568 no digits follow it. 3569 3570 The precision determines the number of digits after the decimal point and 3571 defaults to 6. 3572 3573(4) 3574 The alternate form causes the result to always contain a decimal point, and 3575 trailing zeroes are not removed as they would otherwise be. 3576 3577 The precision determines the number of significant digits before and after the 3578 decimal point and defaults to 6. 3579 3580(5) 3581 If precision is ``N``, the output is truncated to ``N`` characters. 3582 3583(6) 3584 ``b'%s'`` is deprecated, but will not be removed during the 3.x series. 3585 3586(7) 3587 ``b'%r'`` is deprecated, but will not be removed during the 3.x series. 3588 3589(8) 3590 See :pep:`237`. 3591 3592.. note:: 3593 3594 The bytearray version of this method does *not* operate in place - it 3595 always produces a new object, even if no changes were made. 3596 3597.. seealso:: 3598 3599 :pep:`461` - Adding % formatting to bytes and bytearray 3600 3601.. versionadded:: 3.5 3602 3603.. _typememoryview: 3604 3605Memory Views 3606------------ 3607 3608:class:`memoryview` objects allow Python code to access the internal data 3609of an object that supports the :ref:`buffer protocol <bufferobjects>` without 3610copying. 3611 3612.. class:: memoryview(obj) 3613 3614 Create a :class:`memoryview` that references *obj*. *obj* must support the 3615 buffer protocol. Built-in objects that support the buffer protocol include 3616 :class:`bytes` and :class:`bytearray`. 3617 3618 A :class:`memoryview` has the notion of an *element*, which is the 3619 atomic memory unit handled by the originating object *obj*. For many 3620 simple types such as :class:`bytes` and :class:`bytearray`, an element 3621 is a single byte, but other types such as :class:`array.array` may have 3622 bigger elements. 3623 3624 ``len(view)`` is equal to the length of :class:`~memoryview.tolist`. 3625 If ``view.ndim = 0``, the length is 1. If ``view.ndim = 1``, the length 3626 is equal to the number of elements in the view. For higher dimensions, 3627 the length is equal to the length of the nested list representation of 3628 the view. The :class:`~memoryview.itemsize` attribute will give you the 3629 number of bytes in a single element. 3630 3631 A :class:`memoryview` supports slicing and indexing to expose its data. 3632 One-dimensional slicing will result in a subview:: 3633 3634 >>> v = memoryview(b'abcefg') 3635 >>> v[1] 3636 98 3637 >>> v[-1] 3638 103 3639 >>> v[1:4] 3640 <memory at 0x7f3ddc9f4350> 3641 >>> bytes(v[1:4]) 3642 b'bce' 3643 3644 If :class:`~memoryview.format` is one of the native format specifiers 3645 from the :mod:`struct` module, indexing with an integer or a tuple of 3646 integers is also supported and returns a single *element* with 3647 the correct type. One-dimensional memoryviews can be indexed 3648 with an integer or a one-integer tuple. Multi-dimensional memoryviews 3649 can be indexed with tuples of exactly *ndim* integers where *ndim* is 3650 the number of dimensions. Zero-dimensional memoryviews can be indexed 3651 with the empty tuple. 3652 3653 Here is an example with a non-byte format:: 3654 3655 >>> import array 3656 >>> a = array.array('l', [-11111111, 22222222, -33333333, 44444444]) 3657 >>> m = memoryview(a) 3658 >>> m[0] 3659 -11111111 3660 >>> m[-1] 3661 44444444 3662 >>> m[::2].tolist() 3663 [-11111111, -33333333] 3664 3665 If the underlying object is writable, the memoryview supports 3666 one-dimensional slice assignment. Resizing is not allowed:: 3667 3668 >>> data = bytearray(b'abcefg') 3669 >>> v = memoryview(data) 3670 >>> v.readonly 3671 False 3672 >>> v[0] = ord(b'z') 3673 >>> data 3674 bytearray(b'zbcefg') 3675 >>> v[1:4] = b'123' 3676 >>> data 3677 bytearray(b'z123fg') 3678 >>> v[2:3] = b'spam' 3679 Traceback (most recent call last): 3680 File "<stdin>", line 1, in <module> 3681 ValueError: memoryview assignment: lvalue and rvalue have different structures 3682 >>> v[2:6] = b'spam' 3683 >>> data 3684 bytearray(b'z1spam') 3685 3686 One-dimensional memoryviews of hashable (read-only) types with formats 3687 'B', 'b' or 'c' are also hashable. The hash is defined as 3688 ``hash(m) == hash(m.tobytes())``:: 3689 3690 >>> v = memoryview(b'abcefg') 3691 >>> hash(v) == hash(b'abcefg') 3692 True 3693 >>> hash(v[2:4]) == hash(b'ce') 3694 True 3695 >>> hash(v[::-2]) == hash(b'abcefg'[::-2]) 3696 True 3697 3698 .. versionchanged:: 3.3 3699 One-dimensional memoryviews can now be sliced. 3700 One-dimensional memoryviews with formats 'B', 'b' or 'c' are now hashable. 3701 3702 .. versionchanged:: 3.4 3703 memoryview is now registered automatically with 3704 :class:`collections.abc.Sequence` 3705 3706 .. versionchanged:: 3.5 3707 memoryviews can now be indexed with tuple of integers. 3708 3709 :class:`memoryview` has several methods: 3710 3711 .. method:: __eq__(exporter) 3712 3713 A memoryview and a :pep:`3118` exporter are equal if their shapes are 3714 equivalent and if all corresponding values are equal when the operands' 3715 respective format codes are interpreted using :mod:`struct` syntax. 3716 3717 For the subset of :mod:`struct` format strings currently supported by 3718 :meth:`tolist`, ``v`` and ``w`` are equal if ``v.tolist() == w.tolist()``:: 3719 3720 >>> import array 3721 >>> a = array.array('I', [1, 2, 3, 4, 5]) 3722 >>> b = array.array('d', [1.0, 2.0, 3.0, 4.0, 5.0]) 3723 >>> c = array.array('b', [5, 3, 1]) 3724 >>> x = memoryview(a) 3725 >>> y = memoryview(b) 3726 >>> x == a == y == b 3727 True 3728 >>> x.tolist() == a.tolist() == y.tolist() == b.tolist() 3729 True 3730 >>> z = y[::-2] 3731 >>> z == c 3732 True 3733 >>> z.tolist() == c.tolist() 3734 True 3735 3736 If either format string is not supported by the :mod:`struct` module, 3737 then the objects will always compare as unequal (even if the format 3738 strings and buffer contents are identical):: 3739 3740 >>> from ctypes import BigEndianStructure, c_long 3741 >>> class BEPoint(BigEndianStructure): 3742 ... _fields_ = [("x", c_long), ("y", c_long)] 3743 ... 3744 >>> point = BEPoint(100, 200) 3745 >>> a = memoryview(point) 3746 >>> b = memoryview(point) 3747 >>> a == point 3748 False 3749 >>> a == b 3750 False 3751 3752 Note that, as with floating point numbers, ``v is w`` does *not* imply 3753 ``v == w`` for memoryview objects. 3754 3755 .. versionchanged:: 3.3 3756 Previous versions compared the raw memory disregarding the item format 3757 and the logical array structure. 3758 3759 .. method:: tobytes(order=None) 3760 3761 Return the data in the buffer as a bytestring. This is equivalent to 3762 calling the :class:`bytes` constructor on the memoryview. :: 3763 3764 >>> m = memoryview(b"abc") 3765 >>> m.tobytes() 3766 b'abc' 3767 >>> bytes(m) 3768 b'abc' 3769 3770 For non-contiguous arrays the result is equal to the flattened list 3771 representation with all elements converted to bytes. :meth:`tobytes` 3772 supports all format strings, including those that are not in 3773 :mod:`struct` module syntax. 3774 3775 .. versionadded:: 3.8 3776 *order* can be {'C', 'F', 'A'}. When *order* is 'C' or 'F', the data 3777 of the original array is converted to C or Fortran order. For contiguous 3778 views, 'A' returns an exact copy of the physical memory. In particular, 3779 in-memory Fortran order is preserved. For non-contiguous views, the 3780 data is converted to C first. *order=None* is the same as *order='C'*. 3781 3782 .. method:: hex([sep[, bytes_per_sep]]) 3783 3784 Return a string object containing two hexadecimal digits for each 3785 byte in the buffer. :: 3786 3787 >>> m = memoryview(b"abc") 3788 >>> m.hex() 3789 '616263' 3790 3791 .. versionadded:: 3.5 3792 3793 .. versionchanged:: 3.8 3794 Similar to :meth:`bytes.hex`, :meth:`memoryview.hex` now supports 3795 optional *sep* and *bytes_per_sep* parameters to insert separators 3796 between bytes in the hex output. 3797 3798 .. method:: tolist() 3799 3800 Return the data in the buffer as a list of elements. :: 3801 3802 >>> memoryview(b'abc').tolist() 3803 [97, 98, 99] 3804 >>> import array 3805 >>> a = array.array('d', [1.1, 2.2, 3.3]) 3806 >>> m = memoryview(a) 3807 >>> m.tolist() 3808 [1.1, 2.2, 3.3] 3809 3810 .. versionchanged:: 3.3 3811 :meth:`tolist` now supports all single character native formats in 3812 :mod:`struct` module syntax as well as multi-dimensional 3813 representations. 3814 3815 .. method:: toreadonly() 3816 3817 Return a readonly version of the memoryview object. The original 3818 memoryview object is unchanged. :: 3819 3820 >>> m = memoryview(bytearray(b'abc')) 3821 >>> mm = m.toreadonly() 3822 >>> mm.tolist() 3823 [89, 98, 99] 3824 >>> mm[0] = 42 3825 Traceback (most recent call last): 3826 File "<stdin>", line 1, in <module> 3827 TypeError: cannot modify read-only memory 3828 >>> m[0] = 43 3829 >>> mm.tolist() 3830 [43, 98, 99] 3831 3832 .. versionadded:: 3.8 3833 3834 .. method:: release() 3835 3836 Release the underlying buffer exposed by the memoryview object. Many 3837 objects take special actions when a view is held on them (for example, 3838 a :class:`bytearray` would temporarily forbid resizing); therefore, 3839 calling release() is handy to remove these restrictions (and free any 3840 dangling resources) as soon as possible. 3841 3842 After this method has been called, any further operation on the view 3843 raises a :class:`ValueError` (except :meth:`release()` itself which can 3844 be called multiple times):: 3845 3846 >>> m = memoryview(b'abc') 3847 >>> m.release() 3848 >>> m[0] 3849 Traceback (most recent call last): 3850 File "<stdin>", line 1, in <module> 3851 ValueError: operation forbidden on released memoryview object 3852 3853 The context management protocol can be used for a similar effect, 3854 using the ``with`` statement:: 3855 3856 >>> with memoryview(b'abc') as m: 3857 ... m[0] 3858 ... 3859 97 3860 >>> m[0] 3861 Traceback (most recent call last): 3862 File "<stdin>", line 1, in <module> 3863 ValueError: operation forbidden on released memoryview object 3864 3865 .. versionadded:: 3.2 3866 3867 .. method:: cast(format[, shape]) 3868 3869 Cast a memoryview to a new format or shape. *shape* defaults to 3870 ``[byte_length//new_itemsize]``, which means that the result view 3871 will be one-dimensional. The return value is a new memoryview, but 3872 the buffer itself is not copied. Supported casts are 1D -> C-:term:`contiguous` 3873 and C-contiguous -> 1D. 3874 3875 The destination format is restricted to a single element native format in 3876 :mod:`struct` syntax. One of the formats must be a byte format 3877 ('B', 'b' or 'c'). The byte length of the result must be the same 3878 as the original length. 3879 3880 Cast 1D/long to 1D/unsigned bytes:: 3881 3882 >>> import array 3883 >>> a = array.array('l', [1,2,3]) 3884 >>> x = memoryview(a) 3885 >>> x.format 3886 'l' 3887 >>> x.itemsize 3888 8 3889 >>> len(x) 3890 3 3891 >>> x.nbytes 3892 24 3893 >>> y = x.cast('B') 3894 >>> y.format 3895 'B' 3896 >>> y.itemsize 3897 1 3898 >>> len(y) 3899 24 3900 >>> y.nbytes 3901 24 3902 3903 Cast 1D/unsigned bytes to 1D/char:: 3904 3905 >>> b = bytearray(b'zyz') 3906 >>> x = memoryview(b) 3907 >>> x[0] = b'a' 3908 Traceback (most recent call last): 3909 File "<stdin>", line 1, in <module> 3910 ValueError: memoryview: invalid value for format "B" 3911 >>> y = x.cast('c') 3912 >>> y[0] = b'a' 3913 >>> b 3914 bytearray(b'ayz') 3915 3916 Cast 1D/bytes to 3D/ints to 1D/signed char:: 3917 3918 >>> import struct 3919 >>> buf = struct.pack("i"*12, *list(range(12))) 3920 >>> x = memoryview(buf) 3921 >>> y = x.cast('i', shape=[2,2,3]) 3922 >>> y.tolist() 3923 [[[0, 1, 2], [3, 4, 5]], [[6, 7, 8], [9, 10, 11]]] 3924 >>> y.format 3925 'i' 3926 >>> y.itemsize 3927 4 3928 >>> len(y) 3929 2 3930 >>> y.nbytes 3931 48 3932 >>> z = y.cast('b') 3933 >>> z.format 3934 'b' 3935 >>> z.itemsize 3936 1 3937 >>> len(z) 3938 48 3939 >>> z.nbytes 3940 48 3941 3942 Cast 1D/unsigned long to 2D/unsigned long:: 3943 3944 >>> buf = struct.pack("L"*6, *list(range(6))) 3945 >>> x = memoryview(buf) 3946 >>> y = x.cast('L', shape=[2,3]) 3947 >>> len(y) 3948 2 3949 >>> y.nbytes 3950 48 3951 >>> y.tolist() 3952 [[0, 1, 2], [3, 4, 5]] 3953 3954 .. versionadded:: 3.3 3955 3956 .. versionchanged:: 3.5 3957 The source format is no longer restricted when casting to a byte view. 3958 3959 There are also several readonly attributes available: 3960 3961 .. attribute:: obj 3962 3963 The underlying object of the memoryview:: 3964 3965 >>> b = bytearray(b'xyz') 3966 >>> m = memoryview(b) 3967 >>> m.obj is b 3968 True 3969 3970 .. versionadded:: 3.3 3971 3972 .. attribute:: nbytes 3973 3974 ``nbytes == product(shape) * itemsize == len(m.tobytes())``. This is 3975 the amount of space in bytes that the array would use in a contiguous 3976 representation. It is not necessarily equal to ``len(m)``:: 3977 3978 >>> import array 3979 >>> a = array.array('i', [1,2,3,4,5]) 3980 >>> m = memoryview(a) 3981 >>> len(m) 3982 5 3983 >>> m.nbytes 3984 20 3985 >>> y = m[::2] 3986 >>> len(y) 3987 3 3988 >>> y.nbytes 3989 12 3990 >>> len(y.tobytes()) 3991 12 3992 3993 Multi-dimensional arrays:: 3994 3995 >>> import struct 3996 >>> buf = struct.pack("d"*12, *[1.5*x for x in range(12)]) 3997 >>> x = memoryview(buf) 3998 >>> y = x.cast('d', shape=[3,4]) 3999 >>> y.tolist() 4000 [[0.0, 1.5, 3.0, 4.5], [6.0, 7.5, 9.0, 10.5], [12.0, 13.5, 15.0, 16.5]] 4001 >>> len(y) 4002 3 4003 >>> y.nbytes 4004 96 4005 4006 .. versionadded:: 3.3 4007 4008 .. attribute:: readonly 4009 4010 A bool indicating whether the memory is read only. 4011 4012 .. attribute:: format 4013 4014 A string containing the format (in :mod:`struct` module style) for each 4015 element in the view. A memoryview can be created from exporters with 4016 arbitrary format strings, but some methods (e.g. :meth:`tolist`) are 4017 restricted to native single element formats. 4018 4019 .. versionchanged:: 3.3 4020 format ``'B'`` is now handled according to the struct module syntax. 4021 This means that ``memoryview(b'abc')[0] == b'abc'[0] == 97``. 4022 4023 .. attribute:: itemsize 4024 4025 The size in bytes of each element of the memoryview:: 4026 4027 >>> import array, struct 4028 >>> m = memoryview(array.array('H', [32000, 32001, 32002])) 4029 >>> m.itemsize 4030 2 4031 >>> m[0] 4032 32000 4033 >>> struct.calcsize('H') == m.itemsize 4034 True 4035 4036 .. attribute:: ndim 4037 4038 An integer indicating how many dimensions of a multi-dimensional array the 4039 memory represents. 4040 4041 .. attribute:: shape 4042 4043 A tuple of integers the length of :attr:`ndim` giving the shape of the 4044 memory as an N-dimensional array. 4045 4046 .. versionchanged:: 3.3 4047 An empty tuple instead of ``None`` when ndim = 0. 4048 4049 .. attribute:: strides 4050 4051 A tuple of integers the length of :attr:`ndim` giving the size in bytes to 4052 access each element for each dimension of the array. 4053 4054 .. versionchanged:: 3.3 4055 An empty tuple instead of ``None`` when ndim = 0. 4056 4057 .. attribute:: suboffsets 4058 4059 Used internally for PIL-style arrays. The value is informational only. 4060 4061 .. attribute:: c_contiguous 4062 4063 A bool indicating whether the memory is C-:term:`contiguous`. 4064 4065 .. versionadded:: 3.3 4066 4067 .. attribute:: f_contiguous 4068 4069 A bool indicating whether the memory is Fortran :term:`contiguous`. 4070 4071 .. versionadded:: 3.3 4072 4073 .. attribute:: contiguous 4074 4075 A bool indicating whether the memory is :term:`contiguous`. 4076 4077 .. versionadded:: 3.3 4078 4079 4080.. _types-set: 4081 4082Set Types --- :class:`set`, :class:`frozenset` 4083============================================== 4084 4085.. index:: object: set 4086 4087A :dfn:`set` object is an unordered collection of distinct :term:`hashable` objects. 4088Common uses include membership testing, removing duplicates from a sequence, and 4089computing mathematical operations such as intersection, union, difference, and 4090symmetric difference. 4091(For other containers see the built-in :class:`dict`, :class:`list`, 4092and :class:`tuple` classes, and the :mod:`collections` module.) 4093 4094Like other collections, sets support ``x in set``, ``len(set)``, and ``for x in 4095set``. Being an unordered collection, sets do not record element position or 4096order of insertion. Accordingly, sets do not support indexing, slicing, or 4097other sequence-like behavior. 4098 4099There are currently two built-in set types, :class:`set` and :class:`frozenset`. 4100The :class:`set` type is mutable --- the contents can be changed using methods 4101like :meth:`~set.add` and :meth:`~set.remove`. Since it is mutable, it has no 4102hash value and cannot be used as either a dictionary key or as an element of 4103another set. The :class:`frozenset` type is immutable and :term:`hashable` --- 4104its contents cannot be altered after it is created; it can therefore be used as 4105a dictionary key or as an element of another set. 4106 4107Non-empty sets (not frozensets) can be created by placing a comma-separated list 4108of elements within braces, for example: ``{'jack', 'sjoerd'}``, in addition to the 4109:class:`set` constructor. 4110 4111The constructors for both classes work the same: 4112 4113.. class:: set([iterable]) 4114 frozenset([iterable]) 4115 4116 Return a new set or frozenset object whose elements are taken from 4117 *iterable*. The elements of a set must be :term:`hashable`. To 4118 represent sets of sets, the inner sets must be :class:`frozenset` 4119 objects. If *iterable* is not specified, a new empty set is 4120 returned. 4121 4122 Sets can be created by several means: 4123 4124 * Use a comma-separated list of elements within braces: ``{'jack', 'sjoerd'}`` 4125 * Use a set comprehension: ``{c for c in 'abracadabra' if c not in 'abc'}`` 4126 * Use the type constructor: ``set()``, ``set('foobar')``, ``set(['a', 'b', 'foo'])`` 4127 4128 Instances of :class:`set` and :class:`frozenset` provide the following 4129 operations: 4130 4131 .. describe:: len(s) 4132 4133 Return the number of elements in set *s* (cardinality of *s*). 4134 4135 .. describe:: x in s 4136 4137 Test *x* for membership in *s*. 4138 4139 .. describe:: x not in s 4140 4141 Test *x* for non-membership in *s*. 4142 4143 .. method:: isdisjoint(other) 4144 4145 Return ``True`` if the set has no elements in common with *other*. Sets are 4146 disjoint if and only if their intersection is the empty set. 4147 4148 .. method:: issubset(other) 4149 set <= other 4150 4151 Test whether every element in the set is in *other*. 4152 4153 .. method:: set < other 4154 4155 Test whether the set is a proper subset of *other*, that is, 4156 ``set <= other and set != other``. 4157 4158 .. method:: issuperset(other) 4159 set >= other 4160 4161 Test whether every element in *other* is in the set. 4162 4163 .. method:: set > other 4164 4165 Test whether the set is a proper superset of *other*, that is, ``set >= 4166 other and set != other``. 4167 4168 .. method:: union(*others) 4169 set | other | ... 4170 4171 Return a new set with elements from the set and all others. 4172 4173 .. method:: intersection(*others) 4174 set & other & ... 4175 4176 Return a new set with elements common to the set and all others. 4177 4178 .. method:: difference(*others) 4179 set - other - ... 4180 4181 Return a new set with elements in the set that are not in the others. 4182 4183 .. method:: symmetric_difference(other) 4184 set ^ other 4185 4186 Return a new set with elements in either the set or *other* but not both. 4187 4188 .. method:: copy() 4189 4190 Return a shallow copy of the set. 4191 4192 4193 Note, the non-operator versions of :meth:`union`, :meth:`intersection`, 4194 :meth:`difference`, and :meth:`symmetric_difference`, :meth:`issubset`, and 4195 :meth:`issuperset` methods will accept any iterable as an argument. In 4196 contrast, their operator based counterparts require their arguments to be 4197 sets. This precludes error-prone constructions like ``set('abc') & 'cbs'`` 4198 in favor of the more readable ``set('abc').intersection('cbs')``. 4199 4200 Both :class:`set` and :class:`frozenset` support set to set comparisons. Two 4201 sets are equal if and only if every element of each set is contained in the 4202 other (each is a subset of the other). A set is less than another set if and 4203 only if the first set is a proper subset of the second set (is a subset, but 4204 is not equal). A set is greater than another set if and only if the first set 4205 is a proper superset of the second set (is a superset, but is not equal). 4206 4207 Instances of :class:`set` are compared to instances of :class:`frozenset` 4208 based on their members. For example, ``set('abc') == frozenset('abc')`` 4209 returns ``True`` and so does ``set('abc') in set([frozenset('abc')])``. 4210 4211 The subset and equality comparisons do not generalize to a total ordering 4212 function. For example, any two nonempty disjoint sets are not equal and are not 4213 subsets of each other, so *all* of the following return ``False``: ``a<b``, 4214 ``a==b``, or ``a>b``. 4215 4216 Since sets only define partial ordering (subset relationships), the output of 4217 the :meth:`list.sort` method is undefined for lists of sets. 4218 4219 Set elements, like dictionary keys, must be :term:`hashable`. 4220 4221 Binary operations that mix :class:`set` instances with :class:`frozenset` 4222 return the type of the first operand. For example: ``frozenset('ab') | 4223 set('bc')`` returns an instance of :class:`frozenset`. 4224 4225 The following table lists operations available for :class:`set` that do not 4226 apply to immutable instances of :class:`frozenset`: 4227 4228 .. method:: update(*others) 4229 set |= other | ... 4230 4231 Update the set, adding elements from all others. 4232 4233 .. method:: intersection_update(*others) 4234 set &= other & ... 4235 4236 Update the set, keeping only elements found in it and all others. 4237 4238 .. method:: difference_update(*others) 4239 set -= other | ... 4240 4241 Update the set, removing elements found in others. 4242 4243 .. method:: symmetric_difference_update(other) 4244 set ^= other 4245 4246 Update the set, keeping only elements found in either set, but not in both. 4247 4248 .. method:: add(elem) 4249 4250 Add element *elem* to the set. 4251 4252 .. method:: remove(elem) 4253 4254 Remove element *elem* from the set. Raises :exc:`KeyError` if *elem* is 4255 not contained in the set. 4256 4257 .. method:: discard(elem) 4258 4259 Remove element *elem* from the set if it is present. 4260 4261 .. method:: pop() 4262 4263 Remove and return an arbitrary element from the set. Raises 4264 :exc:`KeyError` if the set is empty. 4265 4266 .. method:: clear() 4267 4268 Remove all elements from the set. 4269 4270 4271 Note, the non-operator versions of the :meth:`update`, 4272 :meth:`intersection_update`, :meth:`difference_update`, and 4273 :meth:`symmetric_difference_update` methods will accept any iterable as an 4274 argument. 4275 4276 Note, the *elem* argument to the :meth:`__contains__`, :meth:`remove`, and 4277 :meth:`discard` methods may be a set. To support searching for an equivalent 4278 frozenset, a temporary one is created from *elem*. 4279 4280 4281.. _typesmapping: 4282 4283Mapping Types --- :class:`dict` 4284=============================== 4285 4286.. index:: 4287 object: mapping 4288 object: dictionary 4289 triple: operations on; mapping; types 4290 triple: operations on; dictionary; type 4291 statement: del 4292 builtin: len 4293 4294A :term:`mapping` object maps :term:`hashable` values to arbitrary objects. 4295Mappings are mutable objects. There is currently only one standard mapping 4296type, the :dfn:`dictionary`. (For other containers see the built-in 4297:class:`list`, :class:`set`, and :class:`tuple` classes, and the 4298:mod:`collections` module.) 4299 4300A dictionary's keys are *almost* arbitrary values. Values that are not 4301:term:`hashable`, that is, values containing lists, dictionaries or other 4302mutable types (that are compared by value rather than by object identity) may 4303not be used as keys. Numeric types used for keys obey the normal rules for 4304numeric comparison: if two numbers compare equal (such as ``1`` and ``1.0``) 4305then they can be used interchangeably to index the same dictionary entry. (Note 4306however, that since computers store floating-point numbers as approximations it 4307is usually unwise to use them as dictionary keys.) 4308 4309Dictionaries can be created by placing a comma-separated list of ``key: value`` 4310pairs within braces, for example: ``{'jack': 4098, 'sjoerd': 4127}`` or ``{4098: 4311'jack', 4127: 'sjoerd'}``, or by the :class:`dict` constructor. 4312 4313.. class:: dict(**kwarg) 4314 dict(mapping, **kwarg) 4315 dict(iterable, **kwarg) 4316 4317 Return a new dictionary initialized from an optional positional argument 4318 and a possibly empty set of keyword arguments. 4319 4320 Dictionaries can be created by several means: 4321 4322 * Use a comma-separated list of ``key: value`` pairs within braces: 4323 ``{'jack': 4098, 'sjoerd': 4127}`` or ``{4098: 'jack', 4127: 'sjoerd'}`` 4324 * Use a dict comprehension: ``{}``, ``{x: x ** 2 for x in range(10)}`` 4325 * Use the type constructor: ``dict()``, 4326 ``dict([('foo', 100), ('bar', 200)])``, ``dict(foo=100, bar=200)`` 4327 4328 If no positional argument is given, an empty dictionary is created. 4329 If a positional argument is given and it is a mapping object, a dictionary 4330 is created with the same key-value pairs as the mapping object. Otherwise, 4331 the positional argument must be an :term:`iterable` object. Each item in 4332 the iterable must itself be an iterable with exactly two objects. The 4333 first object of each item becomes a key in the new dictionary, and the 4334 second object the corresponding value. If a key occurs more than once, the 4335 last value for that key becomes the corresponding value in the new 4336 dictionary. 4337 4338 If keyword arguments are given, the keyword arguments and their values are 4339 added to the dictionary created from the positional argument. If a key 4340 being added is already present, the value from the keyword argument 4341 replaces the value from the positional argument. 4342 4343 To illustrate, the following examples all return a dictionary equal to 4344 ``{"one": 1, "two": 2, "three": 3}``:: 4345 4346 >>> a = dict(one=1, two=2, three=3) 4347 >>> b = {'one': 1, 'two': 2, 'three': 3} 4348 >>> c = dict(zip(['one', 'two', 'three'], [1, 2, 3])) 4349 >>> d = dict([('two', 2), ('one', 1), ('three', 3)]) 4350 >>> e = dict({'three': 3, 'one': 1, 'two': 2}) 4351 >>> f = dict({'one': 1, 'three': 3}, two=2) 4352 >>> a == b == c == d == e == f 4353 True 4354 4355 Providing keyword arguments as in the first example only works for keys that 4356 are valid Python identifiers. Otherwise, any valid keys can be used. 4357 4358 4359 These are the operations that dictionaries support (and therefore, custom 4360 mapping types should support too): 4361 4362 .. describe:: list(d) 4363 4364 Return a list of all the keys used in the dictionary *d*. 4365 4366 .. describe:: len(d) 4367 4368 Return the number of items in the dictionary *d*. 4369 4370 .. describe:: d[key] 4371 4372 Return the item of *d* with key *key*. Raises a :exc:`KeyError` if *key* is 4373 not in the map. 4374 4375 .. index:: __missing__() 4376 4377 If a subclass of dict defines a method :meth:`__missing__` and *key* 4378 is not present, the ``d[key]`` operation calls that method with the key *key* 4379 as argument. The ``d[key]`` operation then returns or raises whatever is 4380 returned or raised by the ``__missing__(key)`` call. 4381 No other operations or methods invoke :meth:`__missing__`. If 4382 :meth:`__missing__` is not defined, :exc:`KeyError` is raised. 4383 :meth:`__missing__` must be a method; it cannot be an instance variable:: 4384 4385 >>> class Counter(dict): 4386 ... def __missing__(self, key): 4387 ... return 0 4388 >>> c = Counter() 4389 >>> c['red'] 4390 0 4391 >>> c['red'] += 1 4392 >>> c['red'] 4393 1 4394 4395 The example above shows part of the implementation of 4396 :class:`collections.Counter`. A different ``__missing__`` method is used 4397 by :class:`collections.defaultdict`. 4398 4399 .. describe:: d[key] = value 4400 4401 Set ``d[key]`` to *value*. 4402 4403 .. describe:: del d[key] 4404 4405 Remove ``d[key]`` from *d*. Raises a :exc:`KeyError` if *key* is not in the 4406 map. 4407 4408 .. describe:: key in d 4409 4410 Return ``True`` if *d* has a key *key*, else ``False``. 4411 4412 .. describe:: key not in d 4413 4414 Equivalent to ``not key in d``. 4415 4416 .. describe:: iter(d) 4417 4418 Return an iterator over the keys of the dictionary. This is a shortcut 4419 for ``iter(d.keys())``. 4420 4421 .. method:: clear() 4422 4423 Remove all items from the dictionary. 4424 4425 .. method:: copy() 4426 4427 Return a shallow copy of the dictionary. 4428 4429 .. classmethod:: fromkeys(iterable[, value]) 4430 4431 Create a new dictionary with keys from *iterable* and values set to *value*. 4432 4433 :meth:`fromkeys` is a class method that returns a new dictionary. *value* 4434 defaults to ``None``. All of the values refer to just a single instance, 4435 so it generally doesn't make sense for *value* to be a mutable object 4436 such as an empty list. To get distinct values, use a :ref:`dict 4437 comprehension <dict>` instead. 4438 4439 .. method:: get(key[, default]) 4440 4441 Return the value for *key* if *key* is in the dictionary, else *default*. 4442 If *default* is not given, it defaults to ``None``, so that this method 4443 never raises a :exc:`KeyError`. 4444 4445 .. method:: items() 4446 4447 Return a new view of the dictionary's items (``(key, value)`` pairs). 4448 See the :ref:`documentation of view objects <dict-views>`. 4449 4450 .. method:: keys() 4451 4452 Return a new view of the dictionary's keys. See the :ref:`documentation 4453 of view objects <dict-views>`. 4454 4455 .. method:: pop(key[, default]) 4456 4457 If *key* is in the dictionary, remove it and return its value, else return 4458 *default*. If *default* is not given and *key* is not in the dictionary, 4459 a :exc:`KeyError` is raised. 4460 4461 .. method:: popitem() 4462 4463 Remove and return a ``(key, value)`` pair from the dictionary. 4464 Pairs are returned in :abbr:`LIFO (last-in, first-out)` order. 4465 4466 :meth:`popitem` is useful to destructively iterate over a dictionary, as 4467 often used in set algorithms. If the dictionary is empty, calling 4468 :meth:`popitem` raises a :exc:`KeyError`. 4469 4470 .. versionchanged:: 3.7 4471 LIFO order is now guaranteed. In prior versions, :meth:`popitem` would 4472 return an arbitrary key/value pair. 4473 4474 .. describe:: reversed(d) 4475 4476 Return a reverse iterator over the keys of the dictionary. This is a 4477 shortcut for ``reversed(d.keys())``. 4478 4479 .. versionadded:: 3.8 4480 4481 .. method:: setdefault(key[, default]) 4482 4483 If *key* is in the dictionary, return its value. If not, insert *key* 4484 with a value of *default* and return *default*. *default* defaults to 4485 ``None``. 4486 4487 .. method:: update([other]) 4488 4489 Update the dictionary with the key/value pairs from *other*, overwriting 4490 existing keys. Return ``None``. 4491 4492 :meth:`update` accepts either another dictionary object or an iterable of 4493 key/value pairs (as tuples or other iterables of length two). If keyword 4494 arguments are specified, the dictionary is then updated with those 4495 key/value pairs: ``d.update(red=1, blue=2)``. 4496 4497 .. method:: values() 4498 4499 Return a new view of the dictionary's values. See the 4500 :ref:`documentation of view objects <dict-views>`. 4501 4502 An equality comparison between one ``dict.values()`` view and another 4503 will always return ``False``. This also applies when comparing 4504 ``dict.values()`` to itself:: 4505 4506 >>> d = {'a': 1} 4507 >>> d.values() == d.values() 4508 False 4509 4510 .. describe:: d | other 4511 4512 Create a new dictionary with the merged keys and values of *d* and 4513 *other*, which must both be dictionaries. The values of *other* take 4514 priority when *d* and *other* share keys. 4515 4516 .. versionadded:: 3.9 4517 4518 .. describe:: d |= other 4519 4520 Update the dictionary *d* with keys and values from *other*, which may be 4521 either a :term:`mapping` or an :term:`iterable` of key/value pairs. The 4522 values of *other* take priority when *d* and *other* share keys. 4523 4524 .. versionadded:: 3.9 4525 4526 Dictionaries compare equal if and only if they have the same ``(key, 4527 value)`` pairs (regardless of ordering). Order comparisons ('<', '<=', '>=', '>') raise 4528 :exc:`TypeError`. 4529 4530 Dictionaries preserve insertion order. Note that updating a key does not 4531 affect the order. Keys added after deletion are inserted at the end. :: 4532 4533 >>> d = {"one": 1, "two": 2, "three": 3, "four": 4} 4534 >>> d 4535 {'one': 1, 'two': 2, 'three': 3, 'four': 4} 4536 >>> list(d) 4537 ['one', 'two', 'three', 'four'] 4538 >>> list(d.values()) 4539 [1, 2, 3, 4] 4540 >>> d["one"] = 42 4541 >>> d 4542 {'one': 42, 'two': 2, 'three': 3, 'four': 4} 4543 >>> del d["two"] 4544 >>> d["two"] = None 4545 >>> d 4546 {'one': 42, 'three': 3, 'four': 4, 'two': None} 4547 4548 .. versionchanged:: 3.7 4549 Dictionary order is guaranteed to be insertion order. This behavior was 4550 an implementation detail of CPython from 3.6. 4551 4552 Dictionaries and dictionary views are reversible. :: 4553 4554 >>> d = {"one": 1, "two": 2, "three": 3, "four": 4} 4555 >>> d 4556 {'one': 1, 'two': 2, 'three': 3, 'four': 4} 4557 >>> list(reversed(d)) 4558 ['four', 'three', 'two', 'one'] 4559 >>> list(reversed(d.values())) 4560 [4, 3, 2, 1] 4561 >>> list(reversed(d.items())) 4562 [('four', 4), ('three', 3), ('two', 2), ('one', 1)] 4563 4564 .. versionchanged:: 3.8 4565 Dictionaries are now reversible. 4566 4567 4568.. seealso:: 4569 :class:`types.MappingProxyType` can be used to create a read-only view 4570 of a :class:`dict`. 4571 4572 4573.. _dict-views: 4574 4575Dictionary view objects 4576----------------------- 4577 4578The objects returned by :meth:`dict.keys`, :meth:`dict.values` and 4579:meth:`dict.items` are *view objects*. They provide a dynamic view on the 4580dictionary's entries, which means that when the dictionary changes, the view 4581reflects these changes. 4582 4583Dictionary views can be iterated over to yield their respective data, and 4584support membership tests: 4585 4586.. describe:: len(dictview) 4587 4588 Return the number of entries in the dictionary. 4589 4590.. describe:: iter(dictview) 4591 4592 Return an iterator over the keys, values or items (represented as tuples of 4593 ``(key, value)``) in the dictionary. 4594 4595 Keys and values are iterated over in insertion order. 4596 This allows the creation of ``(value, key)`` pairs 4597 using :func:`zip`: ``pairs = zip(d.values(), d.keys())``. Another way to 4598 create the same list is ``pairs = [(v, k) for (k, v) in d.items()]``. 4599 4600 Iterating views while adding or deleting entries in the dictionary may raise 4601 a :exc:`RuntimeError` or fail to iterate over all entries. 4602 4603 .. versionchanged:: 3.7 4604 Dictionary order is guaranteed to be insertion order. 4605 4606.. describe:: x in dictview 4607 4608 Return ``True`` if *x* is in the underlying dictionary's keys, values or 4609 items (in the latter case, *x* should be a ``(key, value)`` tuple). 4610 4611.. describe:: reversed(dictview) 4612 4613 Return a reverse iterator over the keys, values or items of the dictionary. 4614 The view will be iterated in reverse order of the insertion. 4615 4616 .. versionchanged:: 3.8 4617 Dictionary views are now reversible. 4618 4619 4620Keys views are set-like since their entries are unique and hashable. If all 4621values are hashable, so that ``(key, value)`` pairs are unique and hashable, 4622then the items view is also set-like. (Values views are not treated as set-like 4623since the entries are generally not unique.) For set-like views, all of the 4624operations defined for the abstract base class :class:`collections.abc.Set` are 4625available (for example, ``==``, ``<``, or ``^``). 4626 4627An example of dictionary view usage:: 4628 4629 >>> dishes = {'eggs': 2, 'sausage': 1, 'bacon': 1, 'spam': 500} 4630 >>> keys = dishes.keys() 4631 >>> values = dishes.values() 4632 4633 >>> # iteration 4634 >>> n = 0 4635 >>> for val in values: 4636 ... n += val 4637 >>> print(n) 4638 504 4639 4640 >>> # keys and values are iterated over in the same order (insertion order) 4641 >>> list(keys) 4642 ['eggs', 'sausage', 'bacon', 'spam'] 4643 >>> list(values) 4644 [2, 1, 1, 500] 4645 4646 >>> # view objects are dynamic and reflect dict changes 4647 >>> del dishes['eggs'] 4648 >>> del dishes['sausage'] 4649 >>> list(keys) 4650 ['bacon', 'spam'] 4651 4652 >>> # set operations 4653 >>> keys & {'eggs', 'bacon', 'salad'} 4654 {'bacon'} 4655 >>> keys ^ {'sausage', 'juice'} 4656 {'juice', 'sausage', 'bacon', 'spam'} 4657 4658 4659.. _typecontextmanager: 4660 4661Context Manager Types 4662===================== 4663 4664.. index:: 4665 single: context manager 4666 single: context management protocol 4667 single: protocol; context management 4668 4669Python's :keyword:`with` statement supports the concept of a runtime context 4670defined by a context manager. This is implemented using a pair of methods 4671that allow user-defined classes to define a runtime context that is entered 4672before the statement body is executed and exited when the statement ends: 4673 4674 4675.. method:: contextmanager.__enter__() 4676 4677 Enter the runtime context and return either this object or another object 4678 related to the runtime context. The value returned by this method is bound to 4679 the identifier in the :keyword:`!as` clause of :keyword:`with` statements using 4680 this context manager. 4681 4682 An example of a context manager that returns itself is a :term:`file object`. 4683 File objects return themselves from __enter__() to allow :func:`open` to be 4684 used as the context expression in a :keyword:`with` statement. 4685 4686 An example of a context manager that returns a related object is the one 4687 returned by :func:`decimal.localcontext`. These managers set the active 4688 decimal context to a copy of the original decimal context and then return the 4689 copy. This allows changes to be made to the current decimal context in the body 4690 of the :keyword:`with` statement without affecting code outside the 4691 :keyword:`!with` statement. 4692 4693 4694.. method:: contextmanager.__exit__(exc_type, exc_val, exc_tb) 4695 4696 Exit the runtime context and return a Boolean flag indicating if any exception 4697 that occurred should be suppressed. If an exception occurred while executing the 4698 body of the :keyword:`with` statement, the arguments contain the exception type, 4699 value and traceback information. Otherwise, all three arguments are ``None``. 4700 4701 Returning a true value from this method will cause the :keyword:`with` statement 4702 to suppress the exception and continue execution with the statement immediately 4703 following the :keyword:`!with` statement. Otherwise the exception continues 4704 propagating after this method has finished executing. Exceptions that occur 4705 during execution of this method will replace any exception that occurred in the 4706 body of the :keyword:`!with` statement. 4707 4708 The exception passed in should never be reraised explicitly - instead, this 4709 method should return a false value to indicate that the method completed 4710 successfully and does not want to suppress the raised exception. This allows 4711 context management code to easily detect whether or not an :meth:`__exit__` 4712 method has actually failed. 4713 4714Python defines several context managers to support easy thread synchronisation, 4715prompt closure of files or other objects, and simpler manipulation of the active 4716decimal arithmetic context. The specific types are not treated specially beyond 4717their implementation of the context management protocol. See the 4718:mod:`contextlib` module for some examples. 4719 4720Python's :term:`generator`\s and the :class:`contextlib.contextmanager` decorator 4721provide a convenient way to implement these protocols. If a generator function is 4722decorated with the :class:`contextlib.contextmanager` decorator, it will return a 4723context manager implementing the necessary :meth:`__enter__` and 4724:meth:`__exit__` methods, rather than the iterator produced by an undecorated 4725generator function. 4726 4727Note that there is no specific slot for any of these methods in the type 4728structure for Python objects in the Python/C API. Extension types wanting to 4729define these methods must provide them as a normal Python accessible method. 4730Compared to the overhead of setting up the runtime context, the overhead of a 4731single class dictionary lookup is negligible. 4732 4733 4734.. _types-genericalias: 4735 4736Generic Alias Type 4737================== 4738 4739.. index:: 4740 object: GenericAlias 4741 pair: Generic; Alias 4742 4743``GenericAlias`` objects are created by subscripting a class (usually a 4744container), such as ``list[int]``. They are intended primarily for 4745:term:`type annotations <annotation>`. 4746 4747Usually, the :ref:`subscription <subscriptions>` of container objects calls the 4748method :meth:`__getitem__` of the object. However, the subscription of some 4749containers' classes may call the classmethod :meth:`__class_getitem__` of the 4750class instead. The classmethod :meth:`__class_getitem__` should return a 4751``GenericAlias`` object. 4752 4753.. note:: 4754 If the :meth:`__getitem__` of the class' metaclass is present, it will take 4755 precedence over the :meth:`__class_getitem__` defined in the class (see 4756 :pep:`560` for more details). 4757 4758The ``GenericAlias`` object acts as a proxy for :term:`generic types 4759<generic type>`, implementing *parameterized generics* - a specific instance 4760of a generic which provides the types for container elements. 4761 4762The user-exposed type for the ``GenericAlias`` object can be accessed from 4763:class:`types.GenericAlias` and used for :func:`isinstance` checks. It can 4764also be used to create ``GenericAlias`` objects directly. 4765 4766.. describe:: T[X, Y, ...] 4767 4768 Creates a ``GenericAlias`` representing a type ``T`` containing elements 4769 of types *X*, *Y*, and more depending on the ``T`` used. 4770 For example, a function expecting a :class:`list` containing 4771 :class:`float` elements:: 4772 4773 def average(values: list[float]) -> float: 4774 return sum(values) / len(values) 4775 4776 Another example for :term:`mapping` objects, using a :class:`dict`, which 4777 is a generic type expecting two type parameters representing the key type 4778 and the value type. In this example, the function expects a ``dict`` with 4779 keys of type :class:`str` and values of type :class:`int`:: 4780 4781 def send_post_request(url: str, body: dict[str, int]) -> None: 4782 ... 4783 4784The builtin functions :func:`isinstance` and :func:`issubclass` do not accept 4785``GenericAlias`` types for their second argument:: 4786 4787 >>> isinstance([1, 2], list[str]) 4788 Traceback (most recent call last): 4789 File "<stdin>", line 1, in <module> 4790 TypeError: isinstance() argument 2 cannot be a parameterized generic 4791 4792The Python runtime does not enforce :term:`type annotations <annotation>`. 4793This extends to generic types and their type parameters. When creating 4794an object from a ``GenericAlias``, container elements are not checked 4795against their type. For example, the following code is discouraged, but will 4796run without errors:: 4797 4798 >>> t = list[str] 4799 >>> t([1, 2, 3]) 4800 [1, 2, 3] 4801 4802Furthermore, parameterized generics erase type parameters during object 4803creation:: 4804 4805 >>> t = list[str] 4806 >>> type(t) 4807 <class 'types.GenericAlias'> 4808 4809 >>> l = t() 4810 >>> type(l) 4811 <class 'list'> 4812 4813Calling :func:`repr` or :func:`str` on a generic shows the parameterized type:: 4814 4815 >>> repr(list[int]) 4816 'list[int]' 4817 4818 >>> str(list[int]) 4819 'list[int]' 4820 4821The :meth:`__getitem__` method of generics will raise an exception to disallow 4822mistakes like ``dict[str][str]``:: 4823 4824 >>> dict[str][str] 4825 Traceback (most recent call last): 4826 File "<stdin>", line 1, in <module> 4827 TypeError: There are no type variables left in dict[str] 4828 4829However, such expressions are valid when :ref:`type variables <generics>` are 4830used. The index must have as many elements as there are type variable items 4831in the ``GenericAlias`` object's :attr:`__args__ <genericalias.__args__>`. :: 4832 4833 >>> from typing import TypeVar 4834 >>> Y = TypeVar('Y') 4835 >>> dict[str, Y][int] 4836 dict[str, int] 4837 4838 4839Standard Generic Collections 4840---------------------------- 4841 4842These standard library collections support parameterized generics. 4843 4844* :class:`tuple` 4845* :class:`list` 4846* :class:`dict` 4847* :class:`set` 4848* :class:`frozenset` 4849* :class:`type` 4850* :class:`collections.deque` 4851* :class:`collections.defaultdict` 4852* :class:`collections.OrderedDict` 4853* :class:`collections.Counter` 4854* :class:`collections.ChainMap` 4855* :class:`collections.abc.Awaitable` 4856* :class:`collections.abc.Coroutine` 4857* :class:`collections.abc.AsyncIterable` 4858* :class:`collections.abc.AsyncIterator` 4859* :class:`collections.abc.AsyncGenerator` 4860* :class:`collections.abc.Iterable` 4861* :class:`collections.abc.Iterator` 4862* :class:`collections.abc.Generator` 4863* :class:`collections.abc.Reversible` 4864* :class:`collections.abc.Container` 4865* :class:`collections.abc.Collection` 4866* :class:`collections.abc.Callable` 4867* :class:`collections.abc.Set` 4868* :class:`collections.abc.MutableSet` 4869* :class:`collections.abc.Mapping` 4870* :class:`collections.abc.MutableMapping` 4871* :class:`collections.abc.Sequence` 4872* :class:`collections.abc.MutableSequence` 4873* :class:`collections.abc.ByteString` 4874* :class:`collections.abc.MappingView` 4875* :class:`collections.abc.KeysView` 4876* :class:`collections.abc.ItemsView` 4877* :class:`collections.abc.ValuesView` 4878* :class:`contextlib.AbstractContextManager` 4879* :class:`contextlib.AbstractAsyncContextManager` 4880* :ref:`re.Pattern <re-objects>` 4881* :ref:`re.Match <match-objects>` 4882 4883 4884Special Attributes of Generic Alias 4885----------------------------------- 4886 4887All parameterized generics implement special read-only attributes. 4888 4889.. attribute:: genericalias.__origin__ 4890 4891 This attribute points at the non-parameterized generic class:: 4892 4893 >>> list[int].__origin__ 4894 <class 'list'> 4895 4896 4897.. attribute:: genericalias.__args__ 4898 4899 This attribute is a :class:`tuple` (possibly of length 1) of generic 4900 types passed to the original :meth:`__class_getitem__` 4901 of the generic container:: 4902 4903 >>> dict[str, list[int]].__args__ 4904 (<class 'str'>, list[int]) 4905 4906 4907.. attribute:: genericalias.__parameters__ 4908 4909 This attribute is a lazily computed tuple (possibly empty) of unique type 4910 variables found in ``__args__``:: 4911 4912 >>> from typing import TypeVar 4913 4914 >>> T = TypeVar('T') 4915 >>> list[T].__parameters__ 4916 (~T,) 4917 4918 4919.. seealso:: 4920 4921 * :pep:`585` -- "Type Hinting Generics In Standard Collections" 4922 * :meth:`__class_getitem__` -- Used to implement parameterized generics. 4923 * :ref:`generics` -- Generics in the :mod:`typing` module. 4924 4925.. versionadded:: 3.9 4926 4927 4928.. _typesother: 4929 4930Other Built-in Types 4931==================== 4932 4933The interpreter supports several other kinds of objects. Most of these support 4934only one or two operations. 4935 4936 4937.. _typesmodules: 4938 4939Modules 4940------- 4941 4942The only special operation on a module is attribute access: ``m.name``, where 4943*m* is a module and *name* accesses a name defined in *m*'s symbol table. 4944Module attributes can be assigned to. (Note that the :keyword:`import` 4945statement is not, strictly speaking, an operation on a module object; ``import 4946foo`` does not require a module object named *foo* to exist, rather it requires 4947an (external) *definition* for a module named *foo* somewhere.) 4948 4949A special attribute of every module is :attr:`~object.__dict__`. This is the 4950dictionary containing the module's symbol table. Modifying this dictionary will 4951actually change the module's symbol table, but direct assignment to the 4952:attr:`~object.__dict__` attribute is not possible (you can write 4953``m.__dict__['a'] = 1``, which defines ``m.a`` to be ``1``, but you can't write 4954``m.__dict__ = {}``). Modifying :attr:`~object.__dict__` directly is 4955not recommended. 4956 4957Modules built into the interpreter are written like this: ``<module 'sys' 4958(built-in)>``. If loaded from a file, they are written as ``<module 'os' from 4959'/usr/local/lib/pythonX.Y/os.pyc'>``. 4960 4961 4962.. _typesobjects: 4963 4964Classes and Class Instances 4965--------------------------- 4966 4967See :ref:`objects` and :ref:`class` for these. 4968 4969 4970.. _typesfunctions: 4971 4972Functions 4973--------- 4974 4975Function objects are created by function definitions. The only operation on a 4976function object is to call it: ``func(argument-list)``. 4977 4978There are really two flavors of function objects: built-in functions and 4979user-defined functions. Both support the same operation (to call the function), 4980but the implementation is different, hence the different object types. 4981 4982See :ref:`function` for more information. 4983 4984 4985.. _typesmethods: 4986 4987Methods 4988------- 4989 4990.. index:: object: method 4991 4992Methods are functions that are called using the attribute notation. There are 4993two flavors: built-in methods (such as :meth:`append` on lists) and class 4994instance methods. Built-in methods are described with the types that support 4995them. 4996 4997If you access a method (a function defined in a class namespace) through an 4998instance, you get a special object: a :dfn:`bound method` (also called 4999:dfn:`instance method`) object. When called, it will add the ``self`` argument 5000to the argument list. Bound methods have two special read-only attributes: 5001``m.__self__`` is the object on which the method operates, and ``m.__func__`` is 5002the function implementing the method. Calling ``m(arg-1, arg-2, ..., arg-n)`` 5003is completely equivalent to calling ``m.__func__(m.__self__, arg-1, arg-2, ..., 5004arg-n)``. 5005 5006Like function objects, bound method objects support getting arbitrary 5007attributes. However, since method attributes are actually stored on the 5008underlying function object (``meth.__func__``), setting method attributes on 5009bound methods is disallowed. Attempting to set an attribute on a method 5010results in an :exc:`AttributeError` being raised. In order to set a method 5011attribute, you need to explicitly set it on the underlying function object:: 5012 5013 >>> class C: 5014 ... def method(self): 5015 ... pass 5016 ... 5017 >>> c = C() 5018 >>> c.method.whoami = 'my name is method' # can't set on the method 5019 Traceback (most recent call last): 5020 File "<stdin>", line 1, in <module> 5021 AttributeError: 'method' object has no attribute 'whoami' 5022 >>> c.method.__func__.whoami = 'my name is method' 5023 >>> c.method.whoami 5024 'my name is method' 5025 5026See :ref:`types` for more information. 5027 5028 5029.. index:: object; code, code object 5030 5031.. _bltin-code-objects: 5032 5033Code Objects 5034------------ 5035 5036.. index:: 5037 builtin: compile 5038 single: __code__ (function object attribute) 5039 5040Code objects are used by the implementation to represent "pseudo-compiled" 5041executable Python code such as a function body. They differ from function 5042objects because they don't contain a reference to their global execution 5043environment. Code objects are returned by the built-in :func:`compile` function 5044and can be extracted from function objects through their :attr:`__code__` 5045attribute. See also the :mod:`code` module. 5046 5047.. index:: 5048 builtin: exec 5049 builtin: eval 5050 5051A code object can be executed or evaluated by passing it (instead of a source 5052string) to the :func:`exec` or :func:`eval` built-in functions. 5053 5054See :ref:`types` for more information. 5055 5056 5057.. _bltin-type-objects: 5058 5059Type Objects 5060------------ 5061 5062.. index:: 5063 builtin: type 5064 module: types 5065 5066Type objects represent the various object types. An object's type is accessed 5067by the built-in function :func:`type`. There are no special operations on 5068types. The standard module :mod:`types` defines names for all standard built-in 5069types. 5070 5071Types are written like this: ``<class 'int'>``. 5072 5073 5074.. _bltin-null-object: 5075 5076The Null Object 5077--------------- 5078 5079This object is returned by functions that don't explicitly return a value. It 5080supports no special operations. There is exactly one null object, named 5081``None`` (a built-in name). ``type(None)()`` produces the same singleton. 5082 5083It is written as ``None``. 5084 5085 5086.. index:: single: ...; ellipsis literal 5087.. _bltin-ellipsis-object: 5088 5089The Ellipsis Object 5090------------------- 5091 5092This object is commonly used by slicing (see :ref:`slicings`). It supports no 5093special operations. There is exactly one ellipsis object, named 5094:const:`Ellipsis` (a built-in name). ``type(Ellipsis)()`` produces the 5095:const:`Ellipsis` singleton. 5096 5097It is written as ``Ellipsis`` or ``...``. 5098 5099 5100.. _bltin-notimplemented-object: 5101 5102The NotImplemented Object 5103------------------------- 5104 5105This object is returned from comparisons and binary operations when they are 5106asked to operate on types they don't support. See :ref:`comparisons` for more 5107information. There is exactly one ``NotImplemented`` object. 5108``type(NotImplemented)()`` produces the singleton instance. 5109 5110It is written as ``NotImplemented``. 5111 5112 5113.. _bltin-boolean-values: 5114 5115Boolean Values 5116-------------- 5117 5118Boolean values are the two constant objects ``False`` and ``True``. They are 5119used to represent truth values (although other values can also be considered 5120false or true). In numeric contexts (for example when used as the argument to 5121an arithmetic operator), they behave like the integers 0 and 1, respectively. 5122The built-in function :func:`bool` can be used to convert any value to a 5123Boolean, if the value can be interpreted as a truth value (see section 5124:ref:`truth` above). 5125 5126.. index:: 5127 single: False 5128 single: True 5129 pair: Boolean; values 5130 5131They are written as ``False`` and ``True``, respectively. 5132 5133 5134.. _typesinternal: 5135 5136Internal Objects 5137---------------- 5138 5139See :ref:`types` for this information. It describes stack frame objects, 5140traceback objects, and slice objects. 5141 5142 5143.. _specialattrs: 5144 5145Special Attributes 5146================== 5147 5148The implementation adds a few special read-only attributes to several object 5149types, where they are relevant. Some of these are not reported by the 5150:func:`dir` built-in function. 5151 5152 5153.. attribute:: object.__dict__ 5154 5155 A dictionary or other mapping object used to store an object's (writable) 5156 attributes. 5157 5158 5159.. attribute:: instance.__class__ 5160 5161 The class to which a class instance belongs. 5162 5163 5164.. attribute:: class.__bases__ 5165 5166 The tuple of base classes of a class object. 5167 5168 5169.. attribute:: definition.__name__ 5170 5171 The name of the class, function, method, descriptor, or 5172 generator instance. 5173 5174 5175.. attribute:: definition.__qualname__ 5176 5177 The :term:`qualified name` of the class, function, method, descriptor, 5178 or generator instance. 5179 5180 .. versionadded:: 3.3 5181 5182 5183.. attribute:: class.__mro__ 5184 5185 This attribute is a tuple of classes that are considered when looking for 5186 base classes during method resolution. 5187 5188 5189.. method:: class.mro() 5190 5191 This method can be overridden by a metaclass to customize the method 5192 resolution order for its instances. It is called at class instantiation, and 5193 its result is stored in :attr:`~class.__mro__`. 5194 5195 5196.. method:: class.__subclasses__ 5197 5198 Each class keeps a list of weak references to its immediate subclasses. This 5199 method returns a list of all those references still alive. The list is in 5200 definition order. Example:: 5201 5202 >>> int.__subclasses__() 5203 [<class 'bool'>] 5204 5205 5206.. _int_max_str_digits: 5207 5208Integer string conversion length limitation 5209=========================================== 5210 5211CPython has a global limit for converting between :class:`int` and :class:`str` 5212to mitigate denial of service attacks. This limit *only* applies to decimal or 5213other non-power-of-two number bases. Hexadecimal, octal, and binary conversions 5214are unlimited. The limit can be configured. 5215 5216The :class:`int` type in CPython is an abitrary length number stored in binary 5217form (commonly known as a "bignum"). There exists no algorithm that can convert 5218a string to a binary integer or a binary integer to a string in linear time, 5219*unless* the base is a power of 2. Even the best known algorithms for base 10 5220have sub-quadratic complexity. Converting a large value such as ``int('1' * 5221500_000)`` can take over a second on a fast CPU. 5222 5223Limiting conversion size offers a practical way to avoid `CVE-2020-10735 5224<https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2020-10735>`_. 5225 5226The limit is applied to the number of digit characters in the input or output 5227string when a non-linear conversion algorithm would be involved. Underscores 5228and the sign are not counted towards the limit. 5229 5230When an operation would exceed the limit, a :exc:`ValueError` is raised: 5231 5232.. doctest:: 5233 5234 >>> import sys 5235 >>> sys.set_int_max_str_digits(4300) # Illustrative, this is the default. 5236 >>> _ = int('2' * 5432) 5237 Traceback (most recent call last): 5238 ... 5239 ValueError: Exceeds the limit (4300) for integer string conversion: value has 5432 digits. 5240 >>> i = int('2' * 4300) 5241 >>> len(str(i)) 5242 4300 5243 >>> i_squared = i*i 5244 >>> len(str(i_squared)) 5245 Traceback (most recent call last): 5246 ... 5247 ValueError: Exceeds the limit (4300) for integer string conversion: value has 8599 digits. 5248 >>> len(hex(i_squared)) 5249 7144 5250 >>> assert int(hex(i_squared), base=16) == i*i # Hexadecimal is unlimited. 5251The default limit is 4300 digits as provided in 5252:data:`sys.int_info.default_max_str_digits <sys.int_info>`. 5253The lowest limit that can be configured is 640 digits as provided in 5254:data:`sys.int_info.str_digits_check_threshold <sys.int_info>`. 5255 5256Verification: 5257 5258.. doctest:: 5259 5260 >>> import sys 5261 >>> assert sys.int_info.default_max_str_digits == 4300, sys.int_info 5262 >>> assert sys.int_info.str_digits_check_threshold == 640, sys.int_info 5263 >>> msg = int('578966293710682886880994035146873798396722250538762761564' 5264 ... '9252925514383915483333812743580549779436104706260696366600' 5265 ... '571186405732').to_bytes(53, 'big') 5266 ... 5267.. versionadded:: 3.9.2 5268 5269Affected APIs 5270------------- 5271 5272The limitation only applies to potentially slow conversions between :class:`int` 5273and :class:`str` or :class:`bytes`: 5274 5275* ``int(string)`` with default base 10. 5276* ``int(string, base)`` for all bases that are not a power of 2. 5277* ``str(integer)``. 5278* ``repr(integer)`` 5279* any other string conversion to base 10, for example ``f"{integer}"``, 5280 ``"{}".format(integer)``, or ``b"%d" % integer``. 5281 5282The limitations do not apply to functions with a linear algorithm: 5283 5284* ``int(string, base)`` with base 2, 4, 8, 16, or 32. 5285* :func:`int.from_bytes` and :func:`int.to_bytes`. 5286* :func:`hex`, :func:`oct`, :func:`bin`. 5287* :ref:`formatspec` for hex, octal, and binary numbers. 5288* :class:`str` to :class:`float`. 5289* :class:`str` to :class:`decimal.Decimal`. 5290 5291Configuring the limit 5292--------------------- 5293 5294Before Python starts up you can use an environment variable or an interpreter 5295command line flag to configure the limit: 5296 5297* :envvar:`PYTHONINTMAXSTRDIGITS`, e.g. 5298 ``PYTHONINTMAXSTRDIGITS=640 python3`` to set the limit to 640 or 5299 ``PYTHONINTMAXSTRDIGITS=0 python3`` to disable the limitation. 5300* :option:`-X int_max_str_digits <-X>`, e.g. 5301 ``python3 -X int_max_str_digits=640`` 5302* :data:`sys.flags.int_max_str_digits` contains the value of 5303 :envvar:`PYTHONINTMAXSTRDIGITS` or :option:`-X int_max_str_digits <-X>`. 5304 If both the env var and the ``-X`` option are set, the ``-X`` option takes 5305 precedence. A value of *-1* indicates that both were unset, thus a value of 5306 :data:`sys.int_info.default_max_str_digits` was used during initilization. 5307 5308From code, you can inspect the current limit and set a new one using these 5309:mod:`sys` APIs: 5310 5311* :func:`sys.get_int_max_str_digits` and :func:`sys.set_int_max_str_digits` are 5312 a getter and setter for the interpreter-wide limit. Subinterpreters have 5313 their own limit. 5314 5315Information about the default and minimum can be found in :attr:`sys.int_info`: 5316 5317* :data:`sys.int_info.default_max_str_digits <sys.int_info>` is the compiled-in 5318 default limit. 5319* :data:`sys.int_info.str_digits_check_threshold <sys.int_info>` is the lowest 5320 accepted value for the limit (other than 0 which disables it). 5321 5322.. versionadded:: 3.9.2 5323 5324.. caution:: 5325 5326 Setting a low limit *can* lead to problems. While rare, code exists that 5327 contains integer constants in decimal in their source that exceed the 5328 minimum threshold. A consequence of setting the limit is that Python source 5329 code containing decimal integer literals longer than the limit will 5330 encounter an error during parsing, usually at startup time or import time or 5331 even at installation time - anytime an up to date ``.pyc`` does not already 5332 exist for the code. A workaround for source that contains such large 5333 constants is to convert them to ``0x`` hexadecimal form as it has no limit. 5334 5335 Test your application thoroughly if you use a low limit. Ensure your tests 5336 run with the limit set early via the environment or flag so that it applies 5337 during startup and even during any installation step that may invoke Python 5338 to precompile ``.py`` sources to ``.pyc`` files. 5339 5340Recommended configuration 5341------------------------- 5342 5343The default :data:`sys.int_info.default_max_str_digits` is expected to be 5344reasonable for most applications. If your application requires a different 5345limit, set it from your main entry point using Python version agnostic code as 5346these APIs were added in security patch releases in versions before 3.11. 5347 5348Example:: 5349 5350 >>> import sys 5351 >>> if hasattr(sys, "set_int_max_str_digits"): 5352 ... upper_bound = 68000 5353 ... lower_bound = 4004 5354 ... current_limit = sys.get_int_max_str_digits() 5355 ... if current_limit == 0 or current_limit > upper_bound: 5356 ... sys.set_int_max_str_digits(upper_bound) 5357 ... elif current_limit < lower_bound: 5358 ... sys.set_int_max_str_digits(lower_bound) 5359 5360If you need to disable it entirely, set it to ``0``. 5361 5362 5363.. rubric:: Footnotes 5364 5365.. [1] Additional information on these special methods may be found in the Python 5366 Reference Manual (:ref:`customization`). 5367 5368.. [2] As a consequence, the list ``[1, 2]`` is considered equal to ``[1.0, 2.0]``, and 5369 similarly for tuples. 5370 5371.. [3] They must have since the parser can't tell the type of the operands. 5372 5373.. [4] Cased characters are those with general category property being one of 5374 "Lu" (Letter, uppercase), "Ll" (Letter, lowercase), or "Lt" (Letter, titlecase). 5375 5376.. [5] To format only a tuple you should therefore provide a singleton tuple whose only 5377 element is the tuple to be formatted. 5378