## How do you express binary literals in Python?

Question!

How do you express an integer as a binary number with Python literals?

I was easily able to find the answer for hex:

``````    >>> 0x12AF
4783
>>> 0x100
256
``````

and octal:

``````    >>> 01267
695
>>> 0100
64
``````

How do you use literals to express binary in Python?

• Python 2.5 and earlier: can express binary using `int('01010101111',2)` but not with a literal.
• Python 2.5 and earlier: there is no way to express binary literals.
• Python 2.6 beta: You can do like so: `0b1100111` or `0B1100111`.
• Python 2.6 beta: will also allow `0o27` or `0O27` (second character is the letter O) to represent an octal.
• Python 3.0 beta: Same as 2.6, but will no longer allow the older `027` syntax for octals.

# How do you express binary literals in Python?

They're not "binary" literals, but rather, "integer literals". You can express integer literals with a binary format with a `0` followed by a `B` or `b` followed by a series of zeros and ones, for example:

``````>>> 0b0010101010
170
>>> 0B010101
21
``````

From the Python 3 docs, these are the ways of providing integer literals in Python:

Integer literals are described by the following lexical definitions:

``````integer        ::=  decimalinteger | octinteger | hexinteger | bininteger
decimalinteger ::=  nonzerodigit digit* | "0"+
nonzerodigit   ::=  "1"..."9"
digit          ::=  "0"..."9"
octinteger     ::=  "0" ("o" | "O") octdigit+
hexinteger     ::=  "0" ("x" | "X") hexdigit+
bininteger     ::=  "0" ("b" | "B") bindigit+
octdigit       ::=  "0"..."7"
hexdigit       ::=  digit | "a"..."f" | "A"..."F"
bindigit       ::=  "0" | "1"
``````

There is no limit for the length of integer literals apart from what can be stored in available memory.

Note that leading zeros in a non-zero decimal number are not allowed. This is for disambiguation with C-style octal literals, which Python used before version 3.0.

Some examples of integer literals:

``````7     2147483647                        0o177    0b100110111
3     79228162514264337593543950336     0o377    0xdeadbeef
``````

## Other ways of expressing binary:

You can have the zeros and ones in a string object which can be manipulated (although you should probably just do bitwise operations on the integer in most cases) - just pass int the string of zeros and ones and the base you are converting from (2):

``````>>> int('010101', 2)
21
``````

You can optionally have the `0b` or `0B` prefix:

``````>>> int('0b0010101010', 2)
170
``````

If you pass it `0` as the base, it will assume base 10 if the string doesn't specify with a prefix:

``````>>> int('10101', 0)
10101
>>> int('0b10101', 0)
21
``````

## Converting from int back to human readable binary:

You can pass an integer to bin to see the string representation of a binary literal:

``````>>> bin(21)
'0b10101'
``````

And you can combine `bin` and `int` to go back and forth:

``````>>> bin(int('010101', 2))
'0b10101'
``````

You can use a format specification as well, if you want to have minimum width with preceding zeros:

``````>>> format(int('010101', 2), '{fill}{width}b'.format(width=10, fill=0))
'0000010101'
>>> format(int('010101', 2), '010b')
'0000010101'
``````

0 in the start here specifies that the base is 8 (not 10), which is pretty easy to see:

``````>>> int('010101', 0)
4161
``````

If you don't start with a 0, then python assumes the number is base 10.

``````>>> int('10101', 0)
10101
``````

``````>>> print int('01010101111',2)
687
>>> print int('11111111',2)
255
``````

Another way.