Working with different number systems is a common task in programming, especially in computer science, digital electronics, and networking. Python makes it easy to convert between Decimal (Base-10) and Octal (Base-8).
In this article, you’ll learn how to:
- Convert a Decimal number to Octal
- Convert an Octal number to Decimal
- Do both using built-in functions and manual methods
Table of contents
Decimal and Octal Numbers
Decimal Number (Base-10)
- The decimal system is the standard number system we use every day.
- It uses 10 digits:
0to9. - Each digit’s place value is a power of 10.
- Example:
347 = ( 3 × 10² ) + ( 4 × 10¹ ) + ( 7 × 10⁰ ) = 300 + 40 + 7
Octal Number (Base-8)
- The Octal Number System is a base-8 number system.
- Uses 8 digits only:
0to7 - Each place value is a power of 8
- Example:
157 (octal) = ( 1 × 8² ) + ( 5 × 8¹ ) + ( 7 × 8⁰ ) = 64 + 40 + 7 = 111 (in decimal)
Convert Decimal to Octal In Python
1. Using Built-in oct() Function
In this approach, we will be using the built-in function oct() that converts a decimal to an octal string.
Syntax: oct(x)
xmust be an integer.- Returns a string starting with
'0o', indicating octal. - For Example,
oct(26) = '0o32'
Use the below syntax if you want to remove the ‘0x’ prefix:
octal = oct(26)[2:] # Output: '32'
Note: [2:] slices a string from index 2 to the end, skipping the first two characters.
Code Example
Output:
Octal value: 32
2. Using format() Function
The format() function in Python converts an integer decimal to its octal representation as a string, without the '0o' prefix.
Syntax: format(number, 'X')
numbermust be an integer (can be positive or negative).'X': Format code for octal. ‘o’ for octal.- Returns a string of octal representation of the integer.
- For Example,
format(255, 'o') = '377'
Note: No '0o' like with oct().
Code Example
3. Using F-String Formatting
An F-string (formatted string literal) lets you embed variables, expressions, or formatting instructions directly into a string using {}.
Just prefix the string with f or F and place variables or expressions inside {}.
For Example,
name = "Alice"
age = 25
f"My name is {name} and I am {age} years old." # Output: My name is Alice and I am 25 years old.
Numbers can be formatted like this:
f"{3.14159:.2f}" # Output: '3.14' → 2 decimal places
f"{{5:b}}" # Output: '101' → binary of 5
f"{255:x}" # Output: 'ff' → hexadecimal of 255
:x inside the f-string formats the decimal as hexadecimal
f"{65:o}" # Output: '101' → Octal of 65
Code Example
4. Using a While Loop
In this approach, we manually convert the decimal number into an octal number using while loop.
Algorithm to convert Decimal to Octal in Python:
- If the decimal number
nis 0, return"0"as the octal result. - Initialize an empty string
octal = ""to store the octal result. - Repeat the following steps while
n > 0:- Find the remainder when
nis divided by 8:remainder = n % 8 - Convert the remainder to a string and prepend it to
octal:octal = str(remainder) + octal - Update
nby integer division by 8:n = n // 8
- Find the remainder when
- After the loop ends, return the string
octal.
For Example: Convert Decimal 65 to Octal: n = 65
Step 1: n = 65
remainder: 65 % 8 = 1
→ octal = "1"
n = 65 ÷ 8 = 8
Step 2: n = 8
remainder: 65 % 8 = 0
→ octal = "01"
n = 8 ÷ 8 = 1
Step 3: n = 1
remainder: 1 % 8 = 1
→ octal = "101"
n = 1 ÷ 8 = 0
Step 4: n = 0 : stop
The loop ends when the decimal becomes 0.
The octal representation of 65 is: 101
Code Example
Octal to Decimal Conversion in Python
1. Using Built-in int() with Base 8
In this approach, we will be using the built-in function int() which converts a octal string to a decimal integer.
Syntax: int(x, base)
x: A string or number (in our case, a octal string, like"101")base: The base of the number system (8for octal)- For Example,
int("101", 8) # Output: 65
Code Example
2. Manual Conversion Using Loop
This approach does not use the built-in function but manually converts the Octal number into a Decimal Number using a for loop in Python.
For example, the octal number "127" means:
= ( 1 × 8² ) + ( 2 × 8¹ ) + ( 7 × 8⁰ )
= ( 1 × 64 ) + ( 2 × 8 ) + ( 7 × 1 )
= 64 + 16 + 7
= 87 (in decimal)
Code Example
Explanation
- Initialize a variable
decimal = 0- This will hold the final converted value.
- Initialize
power = 0- This represents the position of the digit (starting from the rightmost digit, which is 8⁰).
- Reverse the octal string
- So we can process from the least significant digit to the most significant one.
- Loop through each digit in the reversed octal string:
- Convert the digit (character) to an integer:
int(digit) - Multiply it by
8^power(i.e.,8 ** power) - Add the result to
decimal - Increment
powerby 1
- Convert the digit (character) to an integer:
- After the loop, return or print the final value of
decimal.
Summary
Python provides simple and powerful tools for converting between decimal and octal numbers:
- Use built-in functions like
oct()andint(..., 8)for quick results. - Try manual methods to understand how number systems work internally.

Leave a Reply