How do you express binary literals in Python?
<p>How do you express an integer as a binary number with Python literals?</p>

<p>I was easily able to find the answer for hex:</p>

<pre><code>    &gt;&gt;&gt; 0x12AF
    4783
    &gt;&gt;&gt; 0x100
    256
</code></pre>

<p>and octal:</p>

<pre><code>    &gt;&gt;&gt; 01267
    695
    &gt;&gt;&gt; 0100
    64
</code></pre>

<p><strong>How do you use literals to express binary in Python?</strong></p>

<hr>

<p><strong><em>Summary of Answers</em></strong></p>

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

<p>For reference&mdash;<em>future</em> Python possibilities:<br />
Starting with Python 2.6 you can express binary literals using the prefix <strong>0b</strong> or <strong>0B</strong>:</p>

<pre><code>&gt;&gt;&gt; 0b101111
47
</code></pre>

<p>You can also use the new <strong>bin</strong> function to get the binary representation of a number:</p>

<pre><code>&gt;&gt;&gt; bin(173)
'0b10101101'
</code></pre>

<p>Development version of the documentation: <a href="http://docs.python.org/dev/whatsnew/2.6.html#pep-3127-integer-literal-support-and-syntax">What's New in Python 2.6</a></p>
