Skip to main content

oct()

The oct() function is a built-in Python function that takes an integer as an argument and returns its octal representation as a string.

Parameter Values

Parameter Description
number

An integer number whose octal representation is to be returned.

Return Values

The oct() function returns a string representing the octal value of an integer.

How to Use oct() in Python

Example 1:

The oct() function returns the octal representation of a number.

num = 10
oct_num = oct(num)
print(oct_num)
Example 2:

The oct() function also works with negative numbers, returning their octal representation.

num = -8
oct_num = oct(num)
print(oct_num)
Example 3:

The oct() function can be used to convert a hexadecimal number to octal.

hex_num = 0x1A
oct_num = oct(int(hex_num, 16))
print(oct_num)