{"id":120029,"date":"2026-07-08T22:47:58","date_gmt":"2026-07-08T17:17:58","guid":{"rendered":"https:\/\/www.guvi.in\/blog\/?p=120029"},"modified":"2026-07-08T22:48:01","modified_gmt":"2026-07-08T17:18:01","slug":"binary-to-decimal-conversion-in-python","status":"publish","type":"post","link":"https:\/\/www.guvi.in\/blog\/binary-to-decimal-conversion-in-python\/","title":{"rendered":"Binary to Decimal Conversion in Python: Complete Tutorial"},"content":{"rendered":"\n<p>Many beginners learning number systems and bitwise operations need a reliable way to convert binary to decimal in Python for coding exercises, interview problems, or low-level programming tasks. Python offers multiple approaches, from a single built-in function call to writing the conversion logic manually for deeper understanding. This guide walks through every method step by step with working code.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>TL;DR Summary<\/strong><\/h2>\n\n\n\n<ul>\n<li>To convert binary to decimal in Python, you need to use either the built-in int() function with base 2, or write manual logic using positional value calculation. <\/li>\n\n\n\n<li>Python&#8217;s int(&#8220;1010&#8221;, 2) instantly converts a binary string to its decimal equivalent. <\/li>\n\n\n\n<li>This guide covers every method to convert binary to decimal in Python, including built-in functions, manual algorithms, and handling user input, with complete code examples for each approach.<\/li>\n<\/ul>\n\n\n\n<p>Want to strengthen your Python fundamentals with hands-on coding practice and real projects? Explore <strong>HCL GUVI&#8217;s <\/strong><a href=\"https:\/\/www.guvi.in\/zen-class\/python-course\/?utm_source=blog&amp;utm_medium=hyperlink&amp;utm_campaign=python-binary-to-decimal-conversion\" target=\"_blank\" rel=\"noreferrer noopener\"><strong>Python Programming Course<\/strong><\/a>, designed for beginners building a strong foundation in programming logic.<a href=\"https:\/\/www.guvi.in\/courses\/?utm_source=blog&amp;utm_medium=content&amp;utm_campaign=binary-to-decimal-in-python\">\u00a0<\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>What You Need Before Starting<\/strong><\/h2>\n\n\n\n<p>Before converting <a href=\"https:\/\/www.guvi.in\/code-kata\/binary-to-decimal-conversion\/\" target=\"_blank\" rel=\"noreferrer noopener\">binary to decimal <\/a>in Python, make sure you have:<\/p>\n\n\n\n<ul>\n<li>Python 3 installed on your system, version 3.6 or above<\/li>\n\n\n\n<li>A basic understanding of binary numbers and place values<\/li>\n\n\n\n<li>A code editor or IDE like VS Code, PyCharm, or even the Python shell<\/li>\n<\/ul>\n\n\n\n<p>No external libraries are required. Every method in this guide uses Python&#8217;s standard built-in functionality.<\/p>\n\n\n\n<p><strong>Read More: <\/strong><a href=\"https:\/\/www.guvi.in\/blog\/how-to-round-off-in-python\/\" target=\"_blank\" rel=\"noreferrer noopener\"><strong>How to Round Off in Python: A Complete Guide<\/strong><\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Binary to Decimal Conversion in Python<\/strong><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Step 1: Convert Binary to Decimal Using int()<\/strong><\/h3>\n\n\n\n<p>The simplest and most reliable way to convert binary to decimal in Python is the built-in int() function with base 2 as the second argument.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>binary_string = \"1010\"\n\ndecimal_value = int(binary_string, 2)\n\nprint(decimal_value)&nbsp; # Output: 10<\/code><\/pre>\n\n\n\n<p>The int() function accepts a string and a base. Passing base 2 tells Python to interpret the string as a binary number rather than decimal.<\/p>\n\n\n\n<p>This method works for any valid binary string:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>print(int(\"1111\", 2)) &nbsp; # Output: 15\n\nprint(int(\"100000\", 2)) # Output: 32\n\nprint(int(\"0\", 2))&nbsp; &nbsp; &nbsp; # Output: 0<\/code><\/pre>\n\n\n\n<p>This is the recommended approach for production code because it is fast, reliable, and handles edge cases correctly.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Step 2: Convert Binary to Decimal with a 0b Prefix<\/strong><\/h3>\n\n\n\n<p><a href=\"https:\/\/www.guvi.in\/hub\/python\/\" target=\"_blank\" rel=\"noreferrer noopener\">Python<\/a> also recognises binary literals written with a 0b prefix directly in code.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>binary_number = 0b1010\n\nprint(binary_number)&nbsp; # Output: 10<\/code><\/pre>\n\n\n\n<p>This works only when the binary value is written directly as a literal in your code, not as a string. If you have a string with a 0b prefix, int() still handles it correctly using base 0:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>binary_str = \"0b1010\"\n\ndecimal_value = int(binary_str, 0)\n\nprint(decimal_value)&nbsp; # Output: 10<\/code><\/pre>\n\n\n\n<p>Base 0 tells Python to auto-detect the base from the prefix in the string.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Step 3: Convert Binary to Decimal Manually Using Positional Values<\/strong><\/h3>\n\n\n\n<p>Understanding the manual <a href=\"https:\/\/www.guvi.in\/blog\/what-is-an-algorithm\/\" target=\"_blank\" rel=\"noreferrer noopener\">algorithm <\/a>is valuable for interviews and for situations where you cannot rely on built-in functions.<\/p>\n\n\n\n<p>Binary to decimal conversion works by multiplying each digit by 2 raised to its positional power, starting from the rightmost digit at position 0.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>def binary_to_decimal(binary_str):\n\n&nbsp;&nbsp;&nbsp;&nbsp;decimal_value = 0\n\n&nbsp;&nbsp;&nbsp;&nbsp;power = 0\n\n&nbsp;&nbsp;&nbsp;&nbsp;for digit in reversed(binary_str):\n\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;decimal_value += int(digit) * (2 ** power)\n\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;power += 1\n\n&nbsp;&nbsp;&nbsp;&nbsp;return decimal_value\n\nprint(binary_to_decimal(\"1010\")) &nbsp; # Output: 10\n\nprint(binary_to_decimal(\"1111\")) &nbsp; # Output: 15<\/code><\/pre>\n\n\n\n<p>The function reverses the string so the rightmost digit is processed first, since it represents 2 to the power of 0.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Step 4: Convert Binary to Decimal Using a While Loop<\/strong><\/h3>\n\n\n\n<p>An alternative manual approach uses a <a href=\"https:\/\/www.guvi.in\/hub\/python\/while-loop-in-python\/\" target=\"_blank\" rel=\"noreferrer noopener\">while loop <\/a>and works directly with the binary number rather than reversing the string.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>def binary_to_decimal_loop(binary_str):\n\n&nbsp;&nbsp;&nbsp;&nbsp;decimal_value = 0\n\n&nbsp;&nbsp;&nbsp;&nbsp;binary_str = binary_str&#91;::-1]\n\n&nbsp;&nbsp;&nbsp;&nbsp;for i in range(len(binary_str)):\n\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if binary_str&#91;i] == \"1\":\n\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;decimal_value += 2 ** i\n\n&nbsp;&nbsp;&nbsp;&nbsp;return decimal_value\n\nprint(binary_to_decimal_loop(\"110011\"))&nbsp; # Output: 51<\/code><\/pre>\n\n\n\n<p>This version only adds to the decimal value when the digit is 1, skipping unnecessary additions for 0 digits.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Step 5: Take Binary Input from the User and Convert It<\/strong><\/h3>\n\n\n\n<p>A complete program that accepts binary input from the user and converts it to decimal:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>def convert_user_input():\n\n&nbsp;&nbsp;&nbsp;&nbsp;binary_input = input(\"Enter a binary number: \")\n\n&nbsp;&nbsp;&nbsp;&nbsp;if all(digit in \"01\" for digit in binary_input):\n\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;decimal_result = int(binary_input, 2)\n\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print(f\"Decimal equivalent: {decimal_result}\")\n\n&nbsp;&nbsp;&nbsp;&nbsp;else:\n\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print(\"Invalid binary number. Use only 0s and 1s.\")\n\nconvert_user_input()\n\n# Sample run:\n\n# Enter a binary number: 11001\n\n# Decimal equivalent: 25<\/code><\/pre>\n\n\n\n<p>The validation check using all() ensures the input contains only 0s and 1s before attempting conversion, preventing a ValueError on invalid input.<\/p>\n\n\n\n<p>Want to strengthen your Python fundamentals with hands-on coding practice and real projects? Explore <strong>HCL GUVI&#8217;s <\/strong><a href=\"https:\/\/www.guvi.in\/zen-class\/python-course\/?utm_source=blog&amp;utm_medium=hyperlink&amp;utm_campaign=python-binary-to-decimal-conversion\" target=\"_blank\" rel=\"noreferrer noopener\"><strong>Python Programming Course<\/strong><\/a>, designed for beginners building a strong foundation in programming logic.<a href=\"https:\/\/www.guvi.in\/courses\/?utm_source=blog&amp;utm_medium=content&amp;utm_campaign=binary-to-decimal-in-python\">\u00a0<\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Comparing All Conversion Methods<\/strong><\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td><strong>Method<\/strong><\/td><td><strong>Best For<\/strong><\/td><td><strong>Performance<\/strong><\/td><td><strong>Readability<\/strong><\/td><\/tr><tr><td>int(binary_str, 2)<\/td><td>Production code, quick conversion<\/td><td>Fastest<\/td><td>Highest<\/td><\/tr><tr><td>0b literal<\/td><td>Hardcoded binary values in code<\/td><td>Fastest<\/td><td>High<\/td><\/tr><tr><td>Manual loop (reversed string)<\/td><td>Interviews, learning the algorithm<\/td><td>Slower<\/td><td>Medium<\/td><\/tr><tr><td>Manual loop (while-based)<\/td><td>Interviews, custom logic control<\/td><td>Slower<\/td><td>Medium<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>For any real application, always use int(binary_str, 2). Use the manual methods only for learning or when an interview specifically asks you to avoid built-in functions.<\/p>\n\n\n\n<div style=\"background-color: #099f4e; border: 3px solid #110053; border-radius: 12px; padding: 18px 22px; color: #FFFFFF; font-size: 18px; font-family: Montserrat, Helvetica, sans-serif; line-height: 1.6; box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15); max-width: 750px;\">\n\n  <strong style=\"font-size: 22px; color: #FFFFFF;\">\ud83d\udca1 Did You Know?<\/strong>\n  <br \/><br \/>\n\n  Python\u2019s <strong style=\"color: #FFFFFF;\">int()<\/strong> function supports a <strong style=\"color: #FFFFFF;\">base argument<\/strong> that allows you to convert strings from any numeral system between <strong style=\"color: #FFFFFF;\">base 2 and base 36<\/strong>, not just decimal. For example, <code style=\"color: #FFFFFF;\">int(\"ff\", 16)<\/code> converts a hexadecimal value to decimal, while <code style=\"color: #FFFFFF;\">int(\"777\", 8)<\/code> converts an octal value. This built-in flexibility makes Python\u2019s number parsing capabilities more versatile and convenient than many other programming languages.\n\n<\/div>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Real-World Example: Converting Sensor Data in IoT Applications<\/strong><\/h2>\n\n\n\n<p>Consider an IoT system where a temperature sensor sends status flags as binary strings over a serial connection, such as &#8220;1011&#8221; representing four independent sensor states.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>def parse_sensor_flags(binary_flags):\n\n&nbsp;&nbsp;&nbsp;&nbsp;decimal_status = int(binary_flags, 2)\n\n&nbsp;&nbsp;&nbsp;&nbsp;flags = {\n\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\"power_on\": bool(decimal_status &amp; 0b1000),\n\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\"error\": bool(decimal_status &amp; 0b0100),\n\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\"low_battery\": bool(decimal_status &amp; 0b0010),\n\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\"connected\": bool(decimal_status &amp; 0b0001),\n\n&nbsp;&nbsp;&nbsp;&nbsp;}\n\n&nbsp;&nbsp;&nbsp;&nbsp;return flags\n\nsensor_data = \"1011\"\n\nprint(parse_sensor_flags(sensor_data))\n\n# Output: {'power_on': True, 'error': False, 'low_battery': True, 'connected': True}<\/code><\/pre>\n\n\n\n<p>The binary string is first converted to a decimal integer, then individual bits are checked using bitwise AND operations against each flag position. This pattern is common in embedded systems, networking protocols, and hardware interfacing code written in Python.<\/p>\n\n\n\n<div style=\"background-color: #099f4e; border: 3px solid #110053; border-radius: 12px; padding: 18px 22px; color: #FFFFFF; font-size: 18px; font-family: Montserrat, Helvetica, sans-serif; line-height: 1.6; box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15); max-width: 750px;\">\n\n  <strong style=\"font-size: 22px; color: #FFFFFF;\">\ud83d\udca1 Did You Know?<\/strong>\n  <br \/><br \/>\n\n  According to data from several coding interview platforms, <strong style=\"color: #FFFFFF;\">number system conversion problems<\/strong> appear in over <strong style=\"color: #FFFFFF;\">30%<\/strong> of fresher-level technical screening rounds. These questions are often used as warm-up exercises before moving into more complex topics like data structures and algorithms, as they help interviewers quickly assess a candidate\u2019s understanding of fundamentals such as bases, arithmetic operations, and problem-solving approach.\n\n<\/div>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Conclusion<\/strong><\/h2>\n\n\n\n<p>Converting binary to decimal in Python is straightforward once you know that int(binary_str, 2) handles the entire conversion in a single line for any valid binary string.&nbsp;<\/p>\n\n\n\n<p>Understanding the manual algorithm using positional values is valuable for interviews and for building a deeper understanding of how number systems work internally.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>FAQs<\/strong><\/h2>\n\n\n<div id=\"rank-math-faq\" class=\"rank-math-block\">\n<div class=\"rank-math-list \">\n<div id=\"faq-question-1782962605747\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \"><strong>How do I convert binary to decimal in Python?<\/strong>\u00a0<\/h3>\n<div class=\"rank-math-answer \">\n\n<p>Use int(binary_string, 2), where binary_string is your binary number as a string and 2 specifies the base. This returns the decimal equivalent directly.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1782962610174\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \"><strong>Can I convert a binary literal like 0b1010 to decimal?<\/strong>\u00a0<\/h3>\n<div class=\"rank-math-answer \">\n\n<p>Yes. Writing 0b1010 directly in code is already treated as the decimal integer 10 by Python automatically, no conversion function needed.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1782962620038\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \"><strong>What happens if I forget the base argument in int()?<\/strong>\u00a0<\/h3>\n<div class=\"rank-math-answer \">\n\n<p>Python interprets the string as a decimal number instead of binary, returning an incorrect result like 1010 instead of the intended value 10.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1782962627446\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \"><strong>How do I convert binary to decimal without using int()?<\/strong>\u00a0<\/h3>\n<div class=\"rank-math-answer \">\n\n<p>Use a manual loop that multiplies each binary digit by 2 raised to its positional power, summing the results from right to left.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1782962634837\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \"><strong>How do I validate that user input is a valid binary number in Python?<\/strong>\u00a0<\/h3>\n<div class=\"rank-math-answer \">\n\n<p>Use all(digit in &#8220;01&#8221; for digit in input_string) to confirm every character is either 0 or 1 before attempting conversion.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1782962643676\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \"><strong>Can Python&#8217;s int() function convert other number systems too?<\/strong>\u00a0<\/h3>\n<div class=\"rank-math-answer \">\n\n<p>Yes. int() supports any base from 2 to 36, including hexadecimal with base 16 and octal with base 8, using the same syntax.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1782962659704\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \"><strong>Why is int(binary_str, 2) better than writing manual conversion logic?<\/strong>\u00a0<\/h3>\n<div class=\"rank-math-answer \">\n\n<p>It is faster, less error-prone, and handles edge cases like leading zeros and large binary numbers correctly without additional code.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1782962664811\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \"><strong>Is binary to decimal conversion commonly asked in coding interviews?<\/strong>\u00a0<\/h3>\n<div class=\"rank-math-answer \">\n\n<p>Yes. It frequently appears in entry-level technical interviews in India as a foundational question testing understanding of number systems and basic programming logic.<\/p>\n\n<\/div>\n<\/div>\n<\/div>\n<\/div>","protected":false},"excerpt":{"rendered":"<p>Many beginners learning number systems and bitwise operations need a reliable way to convert binary to decimal in Python for coding exercises, interview problems, or low-level programming tasks. Python offers multiple approaches, from a single built-in function call to writing the conversion logic manually for deeper understanding. This guide walks through every method step by [&hellip;]<\/p>\n","protected":false},"author":63,"featured_media":122096,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[717],"tags":[],"views":"38","authorinfo":{"name":"Vishalini Devarajan","url":"https:\/\/www.guvi.in\/blog\/author\/vishalini\/"},"thumbnailURL":"https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2026\/07\/binary-to-decimal-conversion-in-python-300x117.webp","_links":{"self":[{"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/posts\/120029"}],"collection":[{"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/users\/63"}],"replies":[{"embeddable":true,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/comments?post=120029"}],"version-history":[{"count":5,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/posts\/120029\/revisions"}],"predecessor-version":[{"id":122095,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/posts\/120029\/revisions\/122095"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/media\/122096"}],"wp:attachment":[{"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/media?parent=120029"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/categories?post=120029"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/tags?post=120029"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}