Python

Python String oct() Function

Octal stands for “8” or “Eight” because it contains eight digits. In Python, it is a numbering system that uses the base of “8” and is represented by prefixing the number with a “0o”. Sometimes in Python, we need to convert the binary, hexadecimal, or integer value into an octal number system due to various numerical operations. To accomplish this, the “oct()” function is used in Python.

This write-up presents a guide on the “oct()” function using several examples. Here are the contents to be followed:

What is the “oct()” Function in Python?

In Python, the “oct()” function is used to convert the integers into an octal value (string).

Syntax

oct(int)

 

In the above syntax, the “int” parameter represents the integer that is passed to the “oct()” function. The retrieved string octal value is prefixed with “0o”.

Example 1: Converting Integers (Decimal Value) Into Octal Value

This example is used to convert the decimal value into an octal value using the “oct()” function. The “octal()” function takes the integers and retrieves the octal representation:

print(oct(22))
print(oct(-22))

 

The above code executes the below output:

Example 2: Converting Binary and Hexadecimal Value Into Octal Value

According to the below-provided code, the “oct()” function takes the “binary” as well as “hexadecimal” value as an argument and retrieves the octal value:

print(oct(0b1111))
print(oct(0XBF))

 

The above code executes the below output:

Example 3: Using “oct()” Function For Custom Object

In the below code, the class named “sample” is defined with two methods named __index__() and __int__(). These methods retrieve the value of “self.z”, which is “7” in our case. The object of the class is created and passed to the “oct()” function to retrieve the octal repression of the input binary value:

class sample:
    z = 0b111
    def __index__(self):
        return self.z
    def __int__(self):
        return self.z
obj = sample()
print(oct(obj))

 

The octal representation of the specified value has been retrieved successfully:

Example 4: Converting Binary and Hexadecimal Value Into Octal Value Without “Oo” Prefix

We can also remove the prefix of the octal “0o” representation while converting the binary and hexadecimal value to the octal value using the “oct()” function along with the string slicing method:

print(oct(10))
print(oct(10)[2:])

 

The above code generates the below output:

Example 5: Demonstration of Error While Using the “oct()” Function

The “oct()” function retrieves the “TypeError” when applied to the float value. The below snippet displays the error:

Conclusion

The “oct()” function is used in Python to convert/transform the decimal, binary, and hexadecimal values into octal values. We can also retrieve the octal value by removing the prefix “0o” using the “oct()” function and string slicing method. This guide demonstrated a comprehensive overview of the “oct()” function in Python using multiple examples.

About the author

Haroon Javed

Hi, I'm Haroon. I am an electronics engineer and a technical content writer. I am a tech geek who loves to help people to the best of my knowledge.