Notes

`oct()` Function in Python [ English ]

< Prev Next >

1. Introduction

The oct() function in Python is a built-in function used to convert an integer number into its octal representation. The octal number is returned as a string prefixed with 0o, which indicates that the number is in base-8 (octal system).

The octal number system uses eight digits: 0–7. It is sometimes used in computing, especially in file permissions in operating systems and certain low-level programming tasks.

2. Syntax

oct(number)

Parameter


3. Return Value

4. Basic Example

print(oct(10))

Output

0o12

Explanation

5. Example with Another Number

print(oct(25))

Output

0o31

Explanation

6. Removing the 0o Prefix

If only the octal digits are needed, the prefix can be removed.

num = 25
oct_value = oct(num)

print(oct_value[2:])

Output

31

Explanation

7. Example with Negative Numbers

print(oct(-12))

Output

-0o14

Explanation

8. Practical Example

number = 64

octal_value = oct(number)

print("Decimal:", number)
print("Octal:", octal_value)

Output

Decimal: 64
Octal: 0o100

Explanation

9. Summary

< Prev Next >