oct() Builtin Function

Python oct()

Python oct() builtin function is used to convert an integer number to an octal string representation prefixed with 0o.

Syntax

The syntax of oct() function is

oct(x)

where

  • x is an integer number.

Examples

1. Octal of an integer

In the following program, we take an integer in x, and convert it to octal string using oct() function.

Python Program

x = 29

output = oct(x)
print(output)
Run

Output

0o35

2. Octal of a negative integer

In the following program, we take a negative integer in x, and convert it to octal string using oct() function.

Python Program

x = -29

output = oct(x)
print(output)
Run

Output

-0o35

Summary

In this tutorial of Python Examples, we learned the syntax of oct() builtin function, and how to use oct() function to convert an integer to an octal string, with examples.