Python String formatting

In both Python 3.X and 2.X (as of 3.0 and 2.6), normal str strings support two different flavors of string formatting — operations that format objects according to format description strings:

The original expression (all Python versions), coded with the % operator: fmt % (values)

The newer method (3.0, 2.6, and later), coded with call syntax: fmt.format(values)

Both produce new strings based on possibly type-specific substitution codes. Their results may be displayed, or assigned to variables for later use:

‘%s, %s, %.2f’ % (42, ‘spam’, 1 / 3.0) ’42, spam, 0.33′ >>> ‘{0}, {1}, {2:.2f}’.format(42, ‘spam’, 1 / 3.0) ’42, spam, 0.33′

Although the method call seems to have evolved more rapidly in recent years, the expression is used extensively in existing code, and both forms are still fully supported. Moreover, although some view the method form as marginally more mnemonic and consistent, the expression is often simpler and more concise. As these two forms are largely just minor variations on a theme of equivalent functionality and complexity, there is today no compelling reason to recommend one over the other.

Fair Use Source: B00HZ41PGC


Python String Operations

All string types support all sequence operations (see Table 3), plus string-specific methods (described in String methods). In addition, the str type supports string formatting % expressions and template substitution (discussed next), and the bytearray type supports mutable sequence operations (Table 4, plus extra list-like methods). Also see the re string pattern-matching module in The re Pattern-Matching Module, and string-related, built-in functions in Built-in Functions.

Fair Use Source: B00HZ41PGC


Python String Literals and creation

Literals and creation

String literals are written as a series of characters in quotes, optionally preceded with a designator character, and in all string literal forms an empty string is coded as adjacent quotes. Various built-in operations also return new strings:

‘Python”s’, “Python’s”

Single and double quotes work the same, and each can embed unescaped quotes of the other kind.

“””This is a multiline block”””

Triple-quoted blocks collect multiple lines of text into a single string, with end-of-line markers (\n) inserted between the original quoted lines.


Backslash escape code sequences (see Table 7) are replaced with the special-character code point values they represent (e.g., ‘\n’ is an ASCII character with decimal code-point value 10).

“This” “is” “concatenated”

Adjacent string constants are concatenated. Hint: this form may span lines if parenthesized.

r’a raw\string’, R’another\one’

Raw strings: backslashes are retained literally (except at the end of a string). Useful for regular expressions and Windows (DOS) directory paths: e.g., r’c:\dir1\file’.

hex(), oct(), bin()

Create hex/octal/binary digit strings from integer numbers. See Numbers and Built-in Functions.

The following literal forms and calls make specialized strings described in Unicode Strings:


bytes string literal in Python 3.X: sequence of 8-bit byte values representing raw binary data. For 3.X compatibility, this form is also available in Python 2.6 and 2.7, where it simply creates a normal str string. See String methods, Unicode Strings, and Built-in Functions.


bytearray string construction: a mutable variant of bytes. Available in Python 3.X, and in Python 2.X as of 2.6. See String methods, Unicode Strings, and Built-in Functions.


Unicode string literal in Python 2.X: a sequence of Unicode code points. For 2.X compatibility, this form is also available in Python 3.X as of 3.3, where it simply creates a normal str string (but normal string literals and str strings support Unicode text in Python 3.X). See Unicode Strings.

str(), bytes(), bytearray() (and unicode() in 2.X only)

Create strings from objects, with possible Unicode encoding/decoding in Python 3.X. See Built-in Functions.

String literals may contain escape sequences taken from Table 7 to represent special characters.

Table 7. String constant escape codes






Ignored continuation


Horizontal tab


Backslash ()


Vertical tab


Single quote (‘)


Unicode dbase id


Double quote (“)


Unicode 16-bit hex




Unicode 32-bit hex[a]




Hex (at most 2 digits)




Octal (up to 3 digits)


Line feed


Null (not end of string)


Carriage return


Not an escape

[a] \Uhhhhhhhh takes exactly eight hexadecimal digits (h); both \u and \U can be used only in Unicode string literals.

Fair Use Source: B00HZ41PGC


Python Strings


The normal str string object is an immutable (unchangeable) sequence of characters accessed by offset (position). Its characters are code point ordinals in the underlying character set, and individual characters are string objects of length 1.

The full string object model varies across lines.

Python 3.X has three string types with similar interfaces:


An immutable sequence of characters, used for all text — both ASCII and richer Unicode.


An immutable sequence of short integers, used for the byte values of binary data.


A mutable variant of bytes.

Python 2.X instead has two string types with similar interfaces:


An immutable sequence of characters, used for both byte oriented (8-bit) text and binary data.


An immutable sequence of characters, used for possibly-richer Unicode text.

Python 2.X (as of 2.6) also has the Python 3.X bytearray type as a back-port from 3.X, but it does not impose as sharp a distinction between text and binary data. (It may be mixed with text strings freely in 2.X.)

For Unicode support in both 3.X and 2.X, see Unicode Strings. Most of the remainder of this section pertains to all string types, but see String methods, Unicode Strings, and Built-in Functions for more on bytes and bytearray.

Fair Use Source: B00HZ41PGC


Python Other numeric types

Other numeric types

Python also includes a set type (described in Sets). Additional numeric types such as optimized vectors and matrixes are available as third-party open source extensions (e.g., see the NumPy package at The third-party domain also includes support for visualization, statistical tools, extended precision floating-point math, and more (see the Web).

Fair Use Source: B00HZ41PGC


Python Decimal and fraction

Decimal and fraction

Python provides two additional numeric types in standard library modules — decimal is a fixed-precision, floating-point number, and fraction is a rational type that keeps numerator and denominator explicitly. Both may be used to address inaccuracies of floating-point arithmetic:

0.1 – 0.3 -0.19999999999999998 >>> from decimal import Decimal >>> Decimal(‘0.1’) – Decimal(‘0.3’) Decimal(‘-0.2’) >>> from fractions import Fraction >>> Fraction(1, 10) – Fraction(3, 10) Fraction(-1, 5) >>> Fraction(1, 3) + Fraction(7, 6) Fraction(3, 2)

Fractions automatically simplify results. By fixing precision and supporting various truncation and rounding protocols, decimals are useful for monetary applications. See the Python Library Reference for details.

Fair Use Source: B00HZ41PGC


Python Number Operations


Number types support all number operations (see Table 6). In mixed-type expressions, Python converts operands up to the type of the “highest” type, where integer is lower than floating-point, which is lower than complex. As of Python 3.0 and 2.6, integer and floating-point objects also have a handful of type-specific methods and other attributes; see Python’s Library Reference manual for details:

(2.5).as_integer_ratio() # float attrs (5, 2) >>> (2.5).is_integer() False >>> (2).numerator, (2).denominator # int attrs (2, 1) >>> (255).bit_length(), bin(255) # 3.1+ method (8, ‘0b11111111’)

Fair Use Source: B00HZ41PGC


Python Numeric Literals and creation

Literals and creation

Numbers are written in a variety of numeric literal forms, and created by some built-in operations:

1234, −24, +42, 0

Integers (unlimited precision).[1]

1.23, 3.14e-10, 4E210, 4.0e+210, 1., .1

Floating-point (normally implemented as C doubles in CPython).

0o177, 0x9ff, 0b1111

Octal, hex, and binary literals for integers.[2]

3+4j, 3.0+4.0j, 3J

Complex numbers.

decimal.Decimal(‘1.33’), fractions.Fraction(4, 3)

Module-based types: decimal, fraction.

int(9.1), int(‘-9’), int(‘1111’, 2), int(‘0b1111’, 0),

float(9), float(‘1e2’), float(‘-.1’), complex(3, 4.0)

Create numbers from other objects, or from strings with possible base conversion. Conversely, hex(N), oct(N), and bin(N) create digit strings for integers, and string formatting makes general strings for numbers. See also String formatting, Type Conversions, and Built-in Functions.

Fair Use Source: B00HZ41PGC


Python Numbers

Python Numbers

Numbers are immutable (unchangeable) values, supporting numeric operations. This section covers basic number types (integers, floating-point), as well as more advanced types (complex, decimals, and fractions).

Fair Use Source: B00HZ41PGC


Python Specific Built-in Types

Specific Built-in Types

This section covers numbers, strings, lists, dictionaries, tuples, files, sets, and other core built-in types. Its subsections give details common to both Python 3.X and 2.X. In general, all the compound datatypes covered here (e.g., lists, dictionaries, and tuples) can nest inside each other arbitrarily and as deeply as required. Sets may participate in nesting as well, but may contain only immutable objects.

Fair Use Source: B00HZ41PGC


Python Sequence Operation Notes

Sequence Operation Notes

Examples and notes on selected sequence operations in Table 3 and Table 4:

Indexing: S[i]

Fetches components at offsets (first item is at offset 0).

Negative indexes count backward from the end (last item is at offset −1).

S[0] fetches the first item; S[1] fetches the second item.

S[−2] fetches the second-to-last item (same as S[len(S) − 2]).

Slicing: S[i:j]

Extracts contiguous sections of a sequence, from i through j−1.

Slice boundaries i and j default to 0 and sequence length len(S).

S[1:3] fetches from offsets 1 up to, but not including, 3.

S[1:] fetches from offsets 1 through the end (len(S)-1).

S[:−1] fetches from offsets 0 up to, but not including, the last item.

S[:] makes a top-level (shallow) copy of sequence object S.

Extended slicing: S[i:j:k]

The third item k is a stride (default 1), added to the offset of each item extracted.

S[::2] is every other item in entire sequence S.

S[::−1] is sequence S reversed.

S[4:1:−1] fetches from offsets 4 up to, but not including, 1, reversed.

Slice assignment: S[i:j:k] = I

Slice assignment is similar to deleting and then inserting where deleted.

Iterables assigned to basic slices S[i:j] need not match in size.

Iterables assigned to extended slices S[i:j:k] must match in size.


Concatenation, repetition, and slicing return new objects (though not always for tuples).

Fair Use Source: B00HZ41PGC


Python Operations by Category

Operations by Category

In this section, trailing parentheses are omitted from X method names for brevity. In general, all built-in types support the comparisons and Boolean operations listed in Table 2 (although Python 3.X does not support magnitude comparisons for dictionaries or mixed nonnumeric types).

Boolean true means any nonzero number or any nonempty collection object (list, dictionary, etc.), and all objects have a Boolean value. The built-in names True and False are preassigned to true and false values and behave like integers 1 and 0 with custom display formats. The special object None is false and appears in various Python contexts.

Comparisons return True or False and are automatically applied recursively in compound objects as needed to determine a result.

Boolean and and or operators stop (short-circuit) as soon as a result is known and return one of the two operand objects — the value on the left or the right of the operator — whose Boolean value gives the result.

Table 2. Comparisons and Boolean operations



X < Y

Strictly less than[a]

X <= Y

Less than or equal to

X > Y

Strictly greater than

X >= Y

Greater than or equal to

X == Y

Equal to (same value)

X != Y

Not equal to (same as X<>Y in Python 2.X only)[b]

X is Y

Same object

X is not Y

Negated object identity

X < Y < Z

Chained comparisons

not X

If X is false then True; else, False

X or Y

If X is false then Y; else, X

X and Y

If X is false then X; else, Y

[a] To implement comparison expressions, see both the rich comparison (e.g., lt for <) class methods in 3.X and 2.X, and general cmp method in 2.X, described in Operator Overloading Methods.

[b] != and <> both mean not equal by value in 2.X, but != is the preferred syntax in 2.X and the only supported option in 3.X. is performs an identity test; == performs value comparison, and so is much more generally useful.

Tables 3 through 6 define operations common to types in the three major type categories — sequence (positionally ordered), mapping (access-by-key), and number (all numeric types) — as well as operations available for mutable (changeable) types in Python. Most types also export additional type-specific operations (e.g., methods), as described in Specific Built-in Types.

Table 3. Sequence operations (strings, lists, tuples, bytes, bytearray)



Class method

X in S

X not in S

Membership tests




S1 + S2



S * N, N * S




Index by offset


S[i:j], S[i:j:k]

Slicing: items in S from offset i through j−1 by optional stride k





min(S), max(S)

Minimum, maximum item




Iteration protocol


for X in S:,

[expr for X in S],

map(func, S), etc.

Iteration (all contexts)



[a] See also The iteration protocol for more on these methods and their interplay. If defined, contains is preferred over iter, and iter is preferred over getitem.

[b] In Python 2.X, you may also define getslice, setslice, and delslice to handle slicing operations. In 3.X, these are removed in favor of passing slice objects to their item-based indexing counterparts. Slice objects may be used explicitly in indexing expressions in place of i:j:k bounds.

Table 4. Mutable sequence operations (lists, bytearray)



Class method

S[i] = X

Index assignment: change item at existing offset i to reference X


S[i:j] = I,

S[i:j:k] = I

Slice assignment: S from i through j−1 with optional stride k (possibly empty) is replaced by all items in iterable I


del S[i]

Index deletion


del S[i:j],

del S[i:j:k]

Slice deletion


Table 5. Mapping operations (dictionaries)



Class method


Index by key


D[k] = X

Key assignment: change or create entry for key k to reference X


del D[k]

Delete item by key



Length (number of keys)


k in D

Key membership test[a]

Same as in Table 3

k not in D

Converse of k in D

Same as in Table 3


Iterator object for D’s keys

Same as in Table 3

for k in D:, etc.

Iterate through keys in D (all iteration contexts)

Same as in Table 3

[a] In Python 2.X, key membership may also be coded as D.has_key(k). This method is removed in Python 3.X in favor of the in expression, which is also generally preferred in 2.X. See Dictionaries.

Table 6. Numeric operations (all number types)



Class method

X + Y, X − Y

Add, subtract

add, sub

X * Y, X / Y,

X // Y, X % Y

Multiply, divide, floor divide, remainder

mul, truediv[a],

floordiv, mod

−X, +X

Negative, identity

neg, pos

X | Y, X & Y,

X ^ Y

Bitwise OR, AND, exclusive OR (integers)

or, and, xor

X << N, X >> N

Bitwise left-shift, right-shift (integers)

lshift, rshift


Bitwise invert (integers)


X ** Y

X to the power Y



Absolute value



Convert to integer[b]



Convert to float


complex(X), complex(re,im)

Make a complex value


divmod(X, Y)

Tuple: (X / Y, X % Y)


pow(X, Y [,Z])

Raise to a power


[a] The / operator invokes truediv in Python 3.X, but div in Python 2.X unless true division is enabled. See Operator Usage Notes for division semantics.

[b] In Python 2.X, the long() built-in function invokes the long class method. In Python 3.X, the int type subsumes long, which is removed.

Fair Use Source: B00HZ41PGC


Python Operator Usage Notes

Operator Usage Notes

In Python 2.X only, value inequality can be written as either X != Y or X <> Y. In Python 3.X, the latter of these options is removed because it is redundant.

In Python 2.X only, a backquotes expression X works the same as repr(X), and converts objects to display strings. In Python 3.X, use the more readable str() and repr() built-in functions instead.

In both Python 3.X and 2.X, the X // Y floor division expression always truncates fractional remainders, and returns an integer result for integers.

The X / Y expression performs true division in 3.X (always retaining remainders in a floating-point result), and classic division in 2.X (truncating remainders for integers) unless 3.X’s true division is enabled in 2.X with from future import division or Python option -Qnew.

The syntax [….] is used for both list literals and list comprehension expressions. The latter of these performs an implied loop and collects expression results in a new list.

The syntax (….) is used for tuples and expressions, as well as generator expressions — a form of list comprehension that produces results on demand, instead of building a result list. Parentheses may sometimes be omitted in all three constructs. When a tuple’s parentheses are omitted, the comma separating its items acts like a lowest-precedence operator if not otherwise significant.

The syntax {….} is used for dictionary literals. In Python 3.X and 2.7, it is also used for set literals, and both dictionary and set comprehensions; use set() and looping statements in 2.6 and earlier.

The yield and ternary if/else selection expressions are available in Python 2.5 and later. The former returns send() arguments in generators; the latter is a shorthand for a multiline if statement. yield requires parentheses if not alone on the right side of an assignment statement.

Comparison operators may be chained: X < Y < Z produces the same result as X < Y and Y < Z, but Y is evaluated only once in the chained form.

The slice expression X[i:j:k] is equivalent to indexing with a slice object: X[slice(i, j, k)].

In Python 2.X, magnitude comparisons of mixed types are allowed — converting numbers to a common type, and ordering other mixed types according to the type name. In Python 3.X, nonnumeric mixed-type magnitude comparisons are not allowed and raise exceptions; this includes sorts by proxy.

Magnitude comparisons for dictionaries are also no longer supported in Python 3.X (although equality tests are); comparing sorted(adict.items()) is one possible replacement in 3.X.

Call expressions allow for positional and keyword arguments, and arbitrarily large numbers of both; see The Expression Statement and The def Statement for call syntax.

Python 3.X allows ellipsis (literally, …, and known by built-in name Ellipsis) to be used as an atomic expression anywhere in source code. This may be used as an alternative to pass or None in some contexts (e.g., stubbed-out function bodies, type-independent variable initialization).

Although uncertain at this writing, Python 3.5 or later may generalize the *X and **X star syntax to appear in data structure literals and comprehensions, where it will unpack collections into individual items, much as it currently does in function calls. See The Assignment Statement for more details.

Fair Use Source: B00HZ41PGC


Python Built-in Types and Operators

Operators and Precedence

Table 1 lists Python’s expression operators. Operators in the lower cells of this table have higher precedence (i.e., bind tighter) when used in mixed-operator expressions without parentheses.

Atomic terms and dynamic typing

In Table 1, the replaceable expression items X, Y, Z, i, j, and k may be:

Variable names, replaced with their most recently assigned value

Literal expressions, defined in Specific Built-in Types

Nested expressions, taken from any row in this table, possibly in parentheses

Python variables follow a dynamic typing model — they are not declared, and are created by being assigned; have object references as values, and may reference any type of object; and must be assigned before appearing in expressions, as they have no default value. Case is always significant in variable names (see Name Rules). Objects referenced by variables are automatically created, and automatically reclaimed when no longer in use by Python’s garbage collector, which uses reference counters in CPython.

Also in Table 1, replaceable attr must be the literal (unquoted) name of an attribute; args1 is a formal arguments list as defined in The def Statement; args2 is an input arguments list as defined in The Expression Statement; and a literal … qualifies as an atomic expression in 3.X (only).

The syntax of comprehensions and data structure literals (tuple, list, dictionary, and set) given abstractly in the last three rows of Table 1 is defined in Specific Built-in Types.

Table 1. Python 3.X expression operators and precedence



yield X

Generator function result (returns send() value)

lambda args1: X

Anonymous function maker (returns X when called)

X if Y else Z

Ternary selection (X is evaluated only if Y is true)

X or Y

Logical OR: Y is evaluated only if X is false

X and Y

Logical AND: Y is evaluated only if X is true

not X

Logical negation

X in Y, X not in Y

Membership: iterables, sets

X is Y, X is not Y

Object identity tests

X < Y, X <= Y, X > Y, X >= Y

Magnitude comparisons, set subset and superset

X == Y, X != Y

Equality operators

X | Y

Bitwise OR, set union

X ^ Y

Bitwise exclusive OR, set symmetric difference

X & Y

Bitwise AND, set intersection

X << Y, X >> Y

Shift X left, right by Y bits

X + Y, X − Y

Addition/concatenation, subtraction/set difference

X * Y, X % Y,

X / Y, X // Y

Multiplication/repetition, remainder/format, division, floor division

-X, +X

Unary negation, identity


Bitwise NOT complement (inversion)

X ** Y

Power (exponentiation)


Indexing (sequence, mapping, others)


Slicing (all three bounds optional)


Call (function, method, class, other callable)


Attribute reference


Tuple, expression, generator expression


List, list comprehension


Dictionary, set, dictionary and set comprehension

Fair Use Source: B00HZ41PGC


Python Windows Launcher Usage

Python Windows Launcher Usage

On Windows (only), Python 3.3 and later install a script launcher, also available separately for earlier versions. This launcher consists of the executables py.exe (console) and pyw.exe (nonconsole), which can be invoked without PATH settings; are registered to run Python files via filename associations; and allow Python versions to be selected in three ways — with “#!” Unix-like directives at the top of scripts, with command-line arguments, and with configurable defaults.

Launcher File Directives

The launcher recognizes “#!” lines at the top of script files that name Python versions in one of the following forms, in which * is either: empty to use the default version (currently 2 if installed and similar to omitting a “#!” line); a major version number (e.g., 3) to launch the latest version in that line installed; or a complete major.minor specification, optionally suffixed by −32 to prefer a 32-bit install (e.g., 3.1–32):

!/usr/bin/env python* #!/usr/bin/python* #!/usr/local/bin/python* #!python*

Any Python (python.exe) arguments may be given at the end of the line, and Python 3.4 and later may consult PATH for “#!” lines that give just python with no explicit version number.

Launcher Command Lines

The launcher may also be invoked from a system shell with command lines of the following form:

py [pyarg] [pythonarg] [scriptarg]

More generally, anything that may appear in a python command after its python component may also appear after the optional pyarg in a py command, and is passed on to the spawned Python verbatim. This includes the -m, -c, and – program specification forms; see Python Command-Line Usage.

The launcher accepts the following argument forms for its optional pyarg, which mirror the * part at the end of a file’s “#!” line:

−2 Launch latest 2.X version installed -3 Launch latest 3.X version installed -X.Y Launch specified version (X is 2 or 3) -X.Y−32 Launch the specified 32-bit version

If both are present, command-line arguments have precedence over values given in “#!” lines. As installed, “#!” lines may be applied in more contexts (e.g., icon clicks).

Launcher Environment Variables

The launcher also recognizes optional environment variable settings, which may be used to customize version selection in default or partial cases (e.g., missing or major-only “#!” or py command argument):

PY_PYTHON Version to use in default cases (else 2) PY_PYTHON3 Version to use in 3 partials (e.g., 3.2) PY_PYTHON2 Version to use in 2 partials (e.g., 2.6)

These settings are used only by launcher executables, not when python is invoked directly.

Fair Use Source: B00HZ41PGC