oct() Built-in Function


Python oct()

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

In this tutorial, you will learn the syntax of oct() function, and then its usage with the help of example programs.

Syntax

The syntax of oct() function is

oct(x)

where

ParameterDescription
xAn integer number.

The function returns a string value.

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)

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)

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.