Introduction: Python oct() Function
When working with Python, there are situations where you need to represent numbers in octal format. Whether you’re learning different number systems, or studying computer fundamentals, converting decimal numbers into octal manually can be slow and error-prone.
Python provides a simple solution with the oct() function.
What it is: The oct() function is a built-in Python function that converts an integer into its octal representation. It returns the converted value as a string beginning with the prefix 0o, which indicates that the number is written in octal notation.
Take a look at a quick example to see how it works.
You can also explore its real-world use cases to learn where it is commonly used.
Next, we’ll look at the syntax, parameters, return value, and some practical examples.
💡 Tip: Use oct() when you need to convert an integer to its octal representation. For more built-in functions, see the Python Built-in Functions Learning Guide.
Syntax, Parameters, Return Value and Examples: Python oct() Function
The following section explains the syntax, parameters, return value, and a quick example of the Python oct() Function.
Syntax
oct(number)
Parameters
| Parameter | Description |
|---|---|
number |
An integer (or an object that implements the __index__() method) to be converted into octal. |
Return Value
| Return Value | Description |
|---|---|
str |
Returns the octal representation of the specified integer as a string prefixed with 0o. |
Quick Example
The following example converts a decimal number into octal.
result = oct(20)
print(result)
# Output:
0o24
The oct() function converts the decimal value 20 into its octal representation and returns it as a string with the 0o prefix.
How the Python oct() Function Works
- The
oct()function accepts an integer as its argument. - It converts the integer into its octal representation.
- The returned value is a string rather than a numeric data type.
- Every octal string begins with the prefix
0o, indicating octal notation. - Negative integers are also supported and retain the minus sign before the octal value.
Examples: Python oct() Function
The following examples show how the Python oct() Function works in different programming scenarios.
Example 1: Converting a Decimal Number to Octal
result = oct(64)
print(result)
# Output:
0o100
Explanation: The oct() function converts the decimal number 64 into its octal representation. The returned string begins with 0o, indicating that the value is written in octal notation.
Example 2: Converting Zero into Octal
print(oct(0))
# Output:
0o0
Explanation: Zero is also represented in octal format. Python preserves the standard 0o prefix and returns 0o0 as the result.
Example 3: Converting a Negative Number
print(oct(-25))
# Output:
-0o31
Explanation: The oct() function supports negative integers. Python keeps the minus sign before the octal value instead of converting it into another representation.
Example 4: Converting User Input into Octal
number = int(input("Enter a number: "))
print(oct(number))
# Sample Output:
Enter a number: 45
0o55
Explanation: After reading the decimal value from the user, oct() converts it into octal format and returns the corresponding string.
Example 5: Removing the 0o Prefix
number = 83
octal = oct(number)[2:]
print(octal)
# Output:
123
Explanation: Some applications require only the octal digits. By slicing the string from index 2, the 0o prefix is removed, leaving only the octal value.
Example 6: Displaying Octal Values for Multiple Numbers
numbers = [8, 15, 32, 100]
for number in numbers:
print(number, "->", oct(number))
# Output:
8 -> 0o10
15 -> 0o17
32 -> 0o40
100 -> 0o144
Explanation: Instead of converting one number at a time, this example processes an entire list. Each decimal value is displayed alongside its corresponding octal representation.
Example 7: Handling Invalid Arguments
try:
print(oct(7.5))
except TypeError as e:
print(e)
# Output:
'float' object cannot be interpreted as an integer
Explanation: The oct() function accepts only integers or objects that implement the __index__() method. Since a floating-point number does not meet this requirement, Python raises a TypeError. Here, the exception is handled using a try-except block so the program continues running.
Use Cases: When to use the oct() Function
Below are some common situations where the Python oct() Function becomes useful:
- Converting decimal numbers into octal.
- Learning octal number systems.
- Understanding computer number representations.
- Displaying octal values for debugging.
- Working with low-level programming concepts.
- Teaching number system conversions.
Key Takeaways: oct() Function
Before wrapping up, here are the key points to remember about the Python oct() Function:
- The
oct()function converts an integer into its octal representation. - It accepts integers and objects implementing the
__index__()method. - It returns the octal value as a string.
- Every returned octal string begins with the
0oprefix. - Negative integers are supported.
- Passing an unsupported type raises a
TypeError.
In short, the Python oct() Function provides a simple and reliable way to convert decimal integers into octal strings, making it useful for learning number systems, debugging programs, and understanding computer fundamentals.