Python oct() With Examples

Python oct() function is used to convert an integer into octal string prefixed with “0o”.

Python oct()

Python oct() function syntax is:

The output of oct() function is a string. We can pass an object as an argument too, in that case, the object must have __index__() function implementation that returns integer.

Let’s look at some simple examples of oct() function.

Output:

Python oct() with object

Let’s look at another example where we will use oct() function with a custom object as an argument. We will implement __index__() function in this object.

Output: 0o24

You can checkout complete python script and more Python examples from our GitHub Repository.

Reference: Official Documentation

By admin

Leave a Reply

%d bloggers like this: