Python oct() Function

The oct() function in Python is used when we need to convert any specified integer value to its equivalent octal string. For example:

num = 7
print(oct(num))
num = 8
print(oct(num))
num = 87965
print(oct(num))

The output will be:

0o7
0o10
0o253635

That is,

  • 7 (in decimal) is equivalent to 7 (in octal)
  • 8 (in decimal) is equivalent to 10 (in octal)
  • 87965 (in decimal) is equivalent to 253635 (in octal)

Note - The prefix 0o refers that, after the value is in octal number system.

Python oct() Function Syntax

The syntax of oct() function in Python, is:

oct(integerValue)

where integerValue refers to a value, of int type.

Python oct() Function Example

Here is an example of oct() function in Python. This program allows user to define the decimal number, to convert and print the same number in octal system:

print("Enter a Number: ", end="")
num = int(input())

res = oct(num)[2:]
print("\nEquivalent Value (in Octal) =", res)

The sample run with user input 32958 is shown in the snapshot given below:

python oct function

Python Online Test


« Previous Function Next Function »



Like/Share Us on Facebook 😋