0
0
PythonProgramBeginner · 2 min read

Python Program to Convert Binary to Octal Number

You can convert binary to octal in Python by using oct(int(binary_string, 2))[2:] which converts the binary string to an integer and then to octal, removing the '0o' prefix.
📋

Examples

Input101
Output5
Input110101
Output65
Input0
Output0
🧠

How to Think About It

To convert binary to octal, first understand that binary is base 2 and octal is base 8. We can convert the binary number to a decimal number (base 10) by reading it as base 2, then convert that decimal number to octal by changing its base to 8. This two-step conversion is simple and uses built-in functions.
📐

Algorithm

1
Get the binary number as a string input.
2
Convert the binary string to a decimal integer using base 2.
3
Convert the decimal integer to an octal string.
4
Remove the '0o' prefix from the octal string.
5
Return or print the resulting octal number.
💻

Code

python
binary_str = input('Enter a binary number: ')
octal_str = oct(int(binary_str, 2))[2:]
print('Octal number:', octal_str)
Output
Enter a binary number: 110101 Octal number: 65
🔍

Dry Run

Let's trace the binary number '110101' through the code

1

Input binary string

binary_str = '110101'

2

Convert binary to decimal

int('110101', 2) = 53

3

Convert decimal to octal string

oct(53) = '0o65'

4

Remove '0o' prefix

'0o65'[2:] = '65'

5

Print octal number

Output: Octal number: 65

StepOperationValue
1Input binary_str110101
2Convert to decimal53
3Convert to octal string0o65
4Remove prefix65
5Print outputOctal number: 65
💡

Why This Works

Step 1: Convert binary string to decimal

Using int(binary_str, 2) reads the binary string as a base 2 number and converts it to a decimal integer.

Step 2: Convert decimal to octal string

The oct() function converts the decimal integer to an octal string with a '0o' prefix.

Step 3: Remove the octal prefix

Slicing with [2:] removes the '0o' prefix to get the clean octal number string.

🔄

Alternative Approaches

Manual grouping of binary digits
python
binary_str = input('Enter binary: ')
padded = binary_str.zfill((len(binary_str)+2)//3*3)
octal = ''
for i in range(0, len(padded), 3):
    group = padded[i:i+3]
    octal += str(int(group, 2))
print('Octal:', octal)
This method groups binary digits in threes and converts each group to octal digit manually; it avoids built-in oct() but is longer.
Using format function
python
binary_str = input('Enter binary: ')
dec = int(binary_str, 2)
octal = format(dec, 'o')
print('Octal:', octal)
Uses <code>format()</code> with 'o' to convert decimal to octal string, which is clean and readable.

Complexity: O(n) time, O(n) space

Time Complexity

Converting the binary string to an integer takes O(n) time where n is the length of the binary string. Converting to octal is also O(n) in the worst case.

Space Complexity

The space used is O(n) to store the input string and the output octal string.

Which Approach is Fastest?

Using built-in functions like int() and oct() is fastest and simplest compared to manual grouping.

ApproachTimeSpaceBest For
Built-in int() and oct()O(n)O(n)Simplicity and speed
Manual grouping by 3 bitsO(n)O(n)Learning bit manipulation
Using format()O(n)O(n)Readability and clarity
💡
Use int(binary_string, 2) to convert binary to decimal easily before converting to octal.
⚠️
Beginners often forget to remove the '0o' prefix from the octal string, which can cause confusion.