Binary to Decimal and vice-versa in Python Last Updated : 26 Jun, 2025 Summarize Comments Improve Suggest changes Share Like Article Like Report Binary and decimal are two different number systems. Decimal uses base 10 (0–9), while binary uses base 2 (0 and 1). Example: From decimal to binaryInput : 8Output : 1 0 0 0From binary to decimalInput : 100Output : 4Let's explore ways to convert between binary and decimal in Python.Decimal to binary Using loopThis method divides the number by 2 in a loop and stores the remainders (in reverse order) to form the binary number. Python for num in [8, 18]: n = num b = "" while n > 0: b = str(n % 2) + b n //= 2 print(b) Output1000 10010 Explanation: It goes through numbers 8 and 18 one by one.For each number, it keeps dividing by 2 and adds the remainder to form the binary.Using built-in function - bin()bin() is a built in Python function that converts a decimal number directly to its binary string. Python n = 8 print(bin(n).replace("0b", "")) n = 18 print(bin(n).replace("0b", "")) Output1000 10010 Explanation: bin(n).replace("0b", "") convert each decimal number (8, 18) to binary and removes the "0b" prefix for clean output.Using Format Specifierformat() function in Python is a built-in method that allows you to convert numbers into different formats, including binary. Python n = 4 print('{0:b}'.format(n)) b = format(n, 'b') print(b) Output100 100 Explanation: '{0:b}'.format(n) converts the number n to binary using string formatting.format(n, 'b') does the same using the format() function directly.Note: format() converts decimal to binary, but not binary to decimal. Use int(binary, 2) for that.Binary to decimal Using Positional methodThis method converts binary to decimal by multiplying each binary digit by 2^position, starting from the right, and adding the results to get the decimal number. Python def binaryToDecimal(b): d, p = 0, 0 while b: d += (b % 10) * (2 ** p) b //= 10 p += 1 print(d) for num in [100, 101]: binaryToDecimal(num) Output4 5 Explanation: Converts binary (as integer) to decimal using digit × 2^position.while loop extracts digits right to left and adds their weighted value.Runs for 100 and 101 printing decimal results.Using Built-in function - int()int() function can directly convert a binary string to a decimal number by passing the string and base 2. Python for b in ['100', '101']: print(int(b, 2)) Output4 5 Related Articles:python loopsbin()int()format()decimal systembinary system Comment More infoAdvertise with us Next Article Convert Decimal to String in Python P Pushpanjali chauhan Improve Article Tags : Misc Python base-conversion Practice Tags : Miscpython Similar Reads Binary to decimal and vice-versa in python Binary and decimal are two different number systems. Decimal uses base 10 (0â9), while binary uses base 2 (0 and 1). Example: From decimal to binaryInput : 8Output : 1 0 0 0From binary to decimalInput : 100Output : 4Let's explore ways to convert between binary and decimal in Python.Decimal to binary 2 min read Convert Decimal to String in Python Python defines type conversion functions to directly convert one data type to another. This article is aimed at providing the information about converting decimal to string. Converting Decimal to String str() method can be used to convert decimal to string in Python. Syntax: str(object, encoding=âut 1 min read Convert Decimal to Other Bases in Python Given a number in decimal number convert it into binary, octal and hexadecimal number. Here is function to convert decimal to binary, decimal to octal and decimal to hexadecimal. Examples: Input : 55 Output : 55 in Binary : 0b110111 55 in Octal : 0o67 55 in Hexadecimal : 0x37 Input : 282 Output : 28 2 min read numpy.binary_repr() in Python numpy.binary_repr(number, width=None) function is used to represent binary form of the input number as a string. For negative numbers, if width is not given, a minus sign is added to the front. If width is given, the twoâs complement of the number is returned, with respect to that width. In a twoâs- 3 min read Create integer variable by assigning binary value in Python Given a binary value and our task is to create integer variables and assign values in binary format. To assign value in binary format to a variable, we use the 0b prefix. It tells the compiler that the value (prefixed with 0b) is a binary value and assigns it to the variable.Input: Var = 0b1010Outpu 2 min read Python | Decimal as_integer_ratio() method Decimal#as_integer_ratio() : as_integer_ratio() is a Decimal class method which returns the exponent after shifting out the coefficientâs rightmost digits until only the lead digit remains : as a pair (n, d) Syntax: Decimal.as_integer_ratio() Parameter: Decimal values Return: the exponent after shif 2 min read Like