<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>

[{'answer_id': 1478, 'author': 'Mark Biek', 'author_id': 305, 'author_profile': 'https://Stackoverflow.com/users/305', 'pm_score': -1, 'selected': False, 'text': '<p>As far as I can tell Python, up through 2.5, only supports hexadecimal &amp; octal literals.  I did find some discussions about adding binary to future versions but nothing definite.</p>\n'}, {'answer_id': 1479, 'author': 'sparkes', 'author_id': 269, 'author_profile': 'https://Stackoverflow.com/users/269', 'pm_score': -1, 'selected': False, 'text': "<p>I am pretty sure this is one of the things due to change in Python 3.0 with perhaps bin() to go with hex() and oct().</p>\n\n<p>EDIT:\nlbrandy's answer is correct in all cases.</p>\n"}, {'answer_id': 1484, 'author': 'Louis Brandy', 'author_id': 2089740, 'author_profile': 'https://Stackoverflow.com/users/2089740', 'pm_score': 6, 'selected': False, 'text': "<pre><code>&gt;&gt;&gt; print int('01010101111',2)\n687\n&gt;&gt;&gt; print int('11111111',2)\n255\n</code></pre>\n\n<p>Another way.</p>\n"}, {'answer_id': 13107, 'author': 'Andreas Thomas', 'author_id': 1531, 'author_profile': 'https://Stackoverflow.com/users/1531', 'pm_score': 9, 'selected': True, 'text': '<p>For reference&mdash;<em>future</em> Python possibilities:<br>\nStarting with Python 2.6 you can express binary literals using the prefix <strong>0b</strong> or <strong>0B</strong>:</p>\n\n<pre><code>&gt;&gt;&gt; 0b101111\n47\n</code></pre>\n\n<p>You can also use the new <strong>bin</strong> function to get the binary representation of a number:</p>\n\n<pre><code>&gt;&gt;&gt; bin(173)\n\'0b10101101\'\n</code></pre>\n\n<p>Development version of the documentation: <a href="http://docs.python.org/dev/whatsnew/2.6.html#pep-3127-integer-literal-support-and-syntax" rel="noreferrer">What\'s New in Python 2.6</a></p>\n'}, {'answer_id': 37226387, 'author': 'Russia Must Remove Putin', 'author_id': 541136, 'author_profile': 'https://Stackoverflow.com/users/541136', 'pm_score': 5, 'selected': False, 'text': '<blockquote>\n  <h1>How do you express binary literals in Python?</h1>\n</blockquote>\n\n<p>They\'re not "binary" literals, but rather, "integer literals". You can express integer literals with a binary format with a <code>0</code> followed by a <code>B</code> or <code>b</code> followed by a series of zeros and ones, for example:</p>\n\n<pre><code>&gt;&gt;&gt; 0b0010101010\n170\n&gt;&gt;&gt; 0B010101\n21\n</code></pre>\n\n<p>From the Python 3 <a href="https://docs.python.org/3/reference/lexical_analysis.html#integer-literals" rel="noreferrer">docs</a>, these are the ways of providing integer literals in Python:</p>\n\n<blockquote>\n  <p>Integer literals are described by the following lexical definitions:</p>\n\n<pre><code>integer      ::=  decinteger | bininteger | octinteger | hexinteger\ndecinteger   ::=  nonzerodigit (["_"] digit)* | "0"+ (["_"] "0")*\nbininteger   ::=  "0" ("b" | "B") (["_"] bindigit)+\noctinteger   ::=  "0" ("o" | "O") (["_"] octdigit)+\nhexinteger   ::=  "0" ("x" | "X") (["_"] hexdigit)+\nnonzerodigit ::=  "1"..."9"\ndigit        ::=  "0"..."9"\nbindigit     ::=  "0" | "1"\noctdigit     ::=  "0"..."7"\nhexdigit     ::=  digit | "a"..."f" | "A"..."F"\n</code></pre>\n  \n  <p>There is no limit for the length of integer literals apart from what\n  can be stored in available memory.</p>\n  \n  <p>Note that leading zeros in a non-zero decimal number are not allowed.\n  This is for disambiguation with C-style octal literals, which Python\n  used before version 3.0.</p>\n  \n  <p>Some examples of integer literals:</p>\n\n<pre><code>7     2147483647                        0o177    0b100110111\n3     79228162514264337593543950336     0o377    0xdeadbeef\n      100_000_000_000                   0b_1110_0101\n</code></pre>\n  \n  <p><em>Changed in version 3.6:</em> Underscores are now allowed for grouping purposes in literals.</p>\n</blockquote>\n\n<h2>Other ways of expressing binary:</h2>\n\n<p>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):</p>\n\n<pre><code>&gt;&gt;&gt; int(\'010101\', 2)\n21\n</code></pre>\n\n<p>You can optionally have the <code>0b</code> or <code>0B</code> prefix:</p>\n\n<pre><code>&gt;&gt;&gt; int(\'0b0010101010\', 2)\n170\n</code></pre>\n\n<p>If you pass it <code>0</code> as the base, it will assume base 10 if the string doesn\'t specify with a prefix:</p>\n\n<pre><code>&gt;&gt;&gt; int(\'10101\', 0)\n10101\n&gt;&gt;&gt; int(\'0b10101\', 0)\n21\n</code></pre>\n\n<h2>Converting from int back to human readable binary:</h2>\n\n<p>You can pass an integer to bin to see the string representation of a binary literal:</p>\n\n<pre><code>&gt;&gt;&gt; bin(21)\n\'0b10101\'\n</code></pre>\n\n<p>And you can combine <code>bin</code> and <code>int</code> to go back and forth:</p>\n\n<pre><code>&gt;&gt;&gt; bin(int(\'010101\', 2))\n\'0b10101\'\n</code></pre>\n\n<p>You can use a format specification as well, if you want to have minimum width with preceding zeros:</p>\n\n<pre><code>&gt;&gt;&gt; format(int(\'010101\', 2), \'{fill}{width}b\'.format(width=10, fill=0))\n\'0000010101\'\n&gt;&gt;&gt; format(int(\'010101\', 2), \'010b\')\n\'0000010101\'\n</code></pre>\n'}, {'answer_id': 37955839, 'author': 'Mehmet Ugurbil', 'author_id': 6496481, 'author_profile': 'https://Stackoverflow.com/users/6496481', 'pm_score': 2, 'selected': False, 'text': "<p>0 in the start here specifies that the base is 8 (not 10), which is pretty easy to see: </p>\n\n<pre><code>&gt;&gt;&gt; int('010101', 0)\n4161\n</code></pre>\n\n<p>If you don't start with a 0, then python assumes the number is base 10.</p>\n\n<pre><code>&gt;&gt;&gt; int('10101', 0)\n10101\n</code></pre>\n"}, {'answer_id': 63297421, 'author': 'Novus Edge', 'author_id': 14033284, 'author_profile': 'https://Stackoverflow.com/users/14033284', 'pm_score': 1, 'selected': False, 'text': "<p>Another good method to get an integer representation from binary is to use eval()</p>\n<p>Like so:</p>\n<pre><code>def getInt(binNum = 0):\n    return eval(eval('0b' + str(n)))\n</code></pre>\n<p>I guess this is a way to do it too.\nI hope this is a satisfactory answer :D</p>\n"}, {'answer_id': 65057066, 'author': 'Ranjeet R Patil', 'author_id': 12415637, 'author_profile': 'https://Stackoverflow.com/users/12415637', 'pm_score': 2, 'selected': False, 'text': "<p><strong>I've tried this in Python 3.6.9</strong></p>\n<p><strong>Convert Binary to Decimal</strong></p>\n<pre><code>&gt;&gt;&gt; 0b101111\n47\n\n&gt;&gt;&gt; int('101111',2)\n47\n</code></pre>\n<p><strong>Convert Decimal to binary</strong></p>\n<pre><code>&gt;&gt;&gt; bin(47)\n'0b101111'\n</code></pre>\n<p><strong>Place a 0 as the second parameter python assumes it as decimal.</strong></p>\n<pre><code>&gt;&gt;&gt; int('101111',0)\n101111\n</code></pre>\n"}]
['https://Stackoverflow.com/questions/1476', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/92/']