Python oct()

The syntax of oct() is:

oct(x)

oct() Parameters

The oct() function takes a single parameter x.

This parameter could be:

  • an integer number (binary, decimal or hexadecimal)
  • if not an integer, it should implement __index__() to return an integer

Return value from oct()

The oct() function returns an octal string from the given integer number.


Example 1: How oct() works in Python?

Output

oct(10) is: 0o12
oct(0b101) is: 0o5
oct(0XA) is: 0o12

Example 2: oct() for custom objects

Output

The oct is: 0o27

Here, the Person class implements __index__() and __int__(). That's why we can use oct() on the objects of Person.

Note: For compatibility, it's recommended to implement __int__() and __index__() with the same output.