oct() is one of Python's built-in functions, used to convert an integer to an octal string.

Function Syntax

oct(x)

Parameters:

  • x: An integer, which can be any base integer, signed or unsigned.

The oct() function returns an octal string representation of integer x, with the prefix '0o'.

The oct() function only works with integer types. If you try to pass other types of objects to oct(), it will raise a TypeError exception.

oct() Function Examples

Here are several examples of the oct() function:

print(oct(10))     # '0o12'
print(oct(-10))    # '-0o12'
print(oct(0))      # '0o0'
print(oct(0x10))   # '0o20'
print(oct(0o123))  # '0o123'

# print(oct(3.14))  # TypeError