{"id":130214,"date":"2022-12-02T19:10:56","date_gmt":"2022-12-02T19:10:56","guid":{"rendered":"https:\/\/blog.finxter.com\/?p=948232"},"modified":"2022-12-02T19:10:56","modified_gmt":"2022-12-02T19:10:56","slug":"how-to-convert-octal-string-to-integer-in-python","status":"publish","type":"post","link":"https:\/\/sickgaming.net\/blog\/2022\/12\/02\/how-to-convert-octal-string-to-integer-in-python\/","title":{"rendered":"How to Convert Octal String to Integer in Python"},"content":{"rendered":"\n<div class=\"kk-star-ratings kksr-auto kksr-align-left kksr-valign-top\" data-payload='{&quot;align&quot;:&quot;left&quot;,&quot;id&quot;:&quot;948232&quot;,&quot;slug&quot;:&quot;default&quot;,&quot;valign&quot;:&quot;top&quot;,&quot;ignore&quot;:&quot;&quot;,&quot;reference&quot;:&quot;auto&quot;,&quot;class&quot;:&quot;&quot;,&quot;count&quot;:&quot;1&quot;,&quot;legendonly&quot;:&quot;&quot;,&quot;readonly&quot;:&quot;&quot;,&quot;score&quot;:&quot;5&quot;,&quot;best&quot;:&quot;5&quot;,&quot;gap&quot;:&quot;5&quot;,&quot;greet&quot;:&quot;Rate this post&quot;,&quot;legend&quot;:&quot;5\\\/5 - (1 vote)&quot;,&quot;size&quot;:&quot;24&quot;,&quot;width&quot;:&quot;142.5&quot;,&quot;_legend&quot;:&quot;{score}\\\/{best} - ({count} {votes})&quot;,&quot;font_factor&quot;:&quot;1.25&quot;}'>\n<div class=\"kksr-stars\">\n<div class=\"kksr-stars-inactive\">\n<div class=\"kksr-star\" data-star=\"1\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" data-star=\"2\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" data-star=\"3\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" data-star=\"4\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" data-star=\"5\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div class=\"kksr-stars-active\" style=\"width: 142.5px;\">\n<div class=\"kksr-star\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/div>\n<div class=\"kksr-legend\" style=\"font-size: 19.2px;\"> 5\/5 &#8211; (1 vote) <\/div>\n<\/div>\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"705\" height=\"497\" src=\"https:\/\/blog.finxter.com\/wp-content\/uploads\/2022\/12\/oct_to_int.gif\" alt=\"\" class=\"wp-image-948279\"\/><\/figure>\n<\/div>\n<h2>Problem Formulation<\/h2>\n<p>Given a string in the octal form:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">s = '0o77'\n# or s = '77'<\/pre>\n<p><strong>How to convert the octal string to an integer in Python?<\/strong><\/p>\n<p>For example, you want to convert the octal string <code>'o10'<\/code> to the decimal integer <code>8<\/code>. <\/p>\n<p>Here are a few other examples:<\/p>\n<figure class=\"wp-block-table is-style-stripes\">\n<table>\n<thead>\n<tr>\n<th>Octal String<\/th>\n<th>Decimal<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td><code>'0o0'<\/code><\/td>\n<td>0<\/td>\n<\/tr>\n<tr>\n<td><code>'0o4'<\/code><\/td>\n<td>4<\/td>\n<\/tr>\n<tr>\n<td><code>'0o10'<\/code><\/td>\n<td>8<\/td>\n<\/tr>\n<tr>\n<td><code>'0o14'<\/code><\/td>\n<td>12<\/td>\n<\/tr>\n<tr>\n<td><code>'0o20'<\/code><\/td>\n<td>16<\/td>\n<\/tr>\n<tr>\n<td><code>'0o77'<\/code><\/td>\n<td>63<\/td>\n<\/tr>\n<tr>\n<td><code>'0o77777'<\/code><\/td>\n<td>32767<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/figure>\n<h2>Oct String to Integer using int() with Base 8<\/h2>\n<p class=\"has-pale-cyan-blue-background-color has-background\">To convert an octal string to an integer, pass the string as a first argument into Python&#8217;s<a title=\"Python int() Function\" rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/python-int-function\/\" target=\"_blank\"> built-in <code>int()<\/code><\/a> function. Use <code>base=8<\/code> as a second argument of the <code>int()<\/code> function to specify that the given string is an octal number. The <code>int()<\/code> function will then convert the octal string to an integer with base 10 and return the result.<\/p>\n<p>Here&#8217;s a minimal example:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"1\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">>>> int('0o77', base=8)\n63<\/pre>\n<h2>Examples<\/h2>\n<p>And here&#8217;s how you can convert the additional examples shown above: <\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">>>> int('0o0', base=8)\n0\n>>> int('0o4', base=8)\n4\n>>> int('0o10', base=8)\n8\n>>> int('0o14', base=8)\n12\n>>> int('0o20', base=8)\n16\n>>> int('0o77', base=8)\n63\n>>> int('0o77777', base=8)\n32767<\/pre>\n<p>You actually don&#8217;t need to use the prefix <code>'0o'<\/code> because your second argument already defines unambiguously that the given string is an octal number:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">>>> int('0', base=8)\n0\n>>> int('4', base=8)\n4\n>>> int('10', base=8)\n8\n>>> int('14', base=8)\n12\n>>> int('20', base=8)\n16\n>>> int('77', base=8)\n63\n>>> int('77777', base=8)\n32767<\/pre>\n<p>However, skipping the base but leaving the prefix raises a <code>ValueError: invalid literal for int() with base 10: '0o77'<\/code>:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"1,5\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">>>> int('0o77')\nTraceback (most recent call last): File \"&lt;pyshell#16>\", line 1, in &lt;module> int('0o77')\nValueError: invalid literal for int() with base 10: '0o77'<\/pre>\n<p>It assumes that the input string is in base 10 when in fact, it isn&#8217;t.<\/p>\n<p class=\"has-pale-pink-background-color has-background\"><img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/14.0.0\/72x72\/1f4a1.png\" alt=\"\ud83d\udca1\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\" \/> <strong>Note<\/strong>: Even though passing a prefixed string <code>'0o...'<\/code> into the <code>int()<\/code> function is unambiguous, Python&#8217;s <code>int()<\/code> function doesn&#8217;t accept it if you don&#8217;t also define the base. This may be fixed in future <a title=\"HOW TO CHECK YOUR PYTHON VERSION\" rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/how-to-check-your-python-version\/\" target=\"_blank\">versions<\/a>!<\/p>\n<p>In fact, you can specify the base argument as <code>0<\/code> to switch on base guessing&#8212;which should be the default behavior anyway! <img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/14.0.0\/72x72\/1f447.png\" alt=\"\ud83d\udc47\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\" \/><\/p>\n<h2>Base Guessing<\/h2>\n<p>You can pass a prefixed string <code>'0o...'<\/code> into the <code>int()<\/code> function and set the base to <code>0<\/code> to switch on <strong><em>base guessing<\/em><\/strong> in Python. This uses the prefix to determine the base automatically&#8212;without you needing to set it to <code>16<\/code>. Yet, you still have to set it to <code>0<\/code> so the benefit is marginal in practice.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">>>> int('0o7', base=8)\n7\n>>> int('0o7', base=0)\n7\n>>> int('0o7', 0)\n7<\/pre>\n<h2>Converting Octal Literals to Int<\/h2>\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img decoding=\"async\" loading=\"lazy\" width=\"978\" height=\"548\" src=\"https:\/\/blog.finxter.com\/wp-content\/uploads\/2022\/12\/image-40.png\" alt=\"\" class=\"wp-image-948266\" srcset=\"https:\/\/blog.finxter.com\/wp-content\/uploads\/2022\/12\/image-40.png 978w, https:\/\/blog.finxter.com\/wp-content\/uploads\/2022\/12\/image-40-300x168.png 300w, https:\/\/blog.finxter.com\/wp-content\/uploads\/2022\/12\/image-40-768x430.png 768w\" sizes=\"auto, (max-width: 978px) 100vw, 978px\" \/><\/figure>\n<\/div>\n<p>If you don&#8217;t have an octal string but a octal number&#8212;called a <em><strong>literal<\/strong><\/em>&#8212;such as <code>0xff<\/code>, you don&#8217;t even need the <code>int()<\/code> function because Python will automatically <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/python-hex-string-to-decimal\/\" data-type=\"URL\" data-id=\"https:\/\/blog.finxter.com\/python-hex-string-to-decimal\/\" target=\"_blank\">convert it to a decimal number<\/a>:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">>>> 0o743\n483\n>>> 0o7\n7\n>>> 0o10\n8<\/pre>\n<h2>Background int()<\/h2>\n<pre class=\"wp-block-preformatted\"><strong>Syntax<\/strong>: <code>int(value [, base]) &#8211; &gt; int<\/code><\/pre>\n<figure class=\"wp-block-table is-style-stripes\">\n<table>\n<tbody>\n<tr>\n<td><strong>Argument<\/strong><\/td>\n<td><code>value<\/code><\/td>\n<td>A Python object to be converted into an integer number. The value object must have an <code>__int__()<\/code> method that returns the associated integer number\u2014otherwise a <code>TypeError<\/code> will be raised.<\/td>\n<\/tr>\n<tr>\n<td><\/td>\n<td><code>base<\/code><\/td>\n<td>An optional integer argument <code>base<\/code> to define the base of the numerical system in the <code>value<\/code> argument. If you set the base, the <code>value<\/code> argument must be a string. The <code>base<\/code> argument determines how the string argument is interpreted.<\/td>\n<\/tr>\n<tr>\n<td><strong>Return Value<\/strong><\/td>\n<td><code>int<\/code><\/td>\n<td>Returns an integer number after converting the input argument <code>value<\/code> using its required <code>__int__()<\/code> method for the conversion.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/figure>\n<figure class=\"wp-block-embed-youtube wp-block-embed is-type-video is-provider-youtube\"><a href=\"https:\/\/blog.finxter.com\/how-to-convert-octal-string-to-integer-in-python\/\"><img decoding=\"async\" src=\"https:\/\/blog.finxter.com\/wp-content\/plugins\/wp-youtube-lyte\/lyteCache.php?origThumbUrl=https%3A%2F%2Fi.ytimg.com%2Fvi%2FJKDzKWtlaQE%2Fhqdefault.jpg\" alt=\"YouTube Video\"><\/a><figcaption><\/figcaption><\/figure>\n<p>Do you still need more background information about Python&#8217;s <a href=\"https:\/\/blog.finxter.com\/python-built-in-functions\/\" target=\"_blank\" rel=\"noreferrer noopener\" title=\"Python Built-In Functions\">built-in<\/a> <code>int()<\/code> function? No problem, read over the related tutorial.<\/p>\n<p class=\"has-base-background-color has-background\"><img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/14.0.0\/72x72\/1f30d.png\" alt=\"\ud83c\udf0d\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\" \/> <strong>Related Tutorial:<\/strong> <a rel=\"noreferrer noopener\" title=\"https:\/\/blog.finxter.com\/python-int-function\/\" href=\"https:\/\/blog.finxter.com\/python-int-function\/\" target=\"_blank\">Python&#8217;s Built-in <code>int()<\/code> Function<\/a><\/p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>5\/5 &#8211; (1 vote) Problem Formulation Given a string in the octal form: s = &#8216;0o77&#8242; # or s = &#8217;77&#8217; How to convert the octal string to an integer in Python? For example, you want to convert the octal string &#8216;o10&#8217; to the decimal integer 8. Here are a few other examples: Octal String [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[857],"tags":[73,468,528],"class_list":["post-130214","post","type-post","status-publish","format-standard","hentry","category-python-tut","tag-programming","tag-python","tag-tutorial"],"_links":{"self":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts\/130214","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/comments?post=130214"}],"version-history":[{"count":0,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts\/130214\/revisions"}],"wp:attachment":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/media?parent=130214"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/categories?post=130214"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/tags?post=130214"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}