Python oct() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Built-in

Definition

In Python, oct(x) converts an integer to an octal (base 8) string with the prefix 0o: oct(8) returns '0o10'. Each octal digit stands for exactly three bits.

Octal is most common for Unix file permissions, where 0o755 means read/write/execute for the owner and read/execute for everyone else. In source code you write octal numbers with the same 0o prefix.

oct() is a built-in function: it lives in the builtins module, so it is always available without an import.

Syntax

Pass an int; you get octal text.

python
text = oct(number)      # '0o...'
CallWhat it does
oct(8)Returns '0o10'.
oct(493)Returns '0o755'.
format(493, "o")Returns '755' without the prefix.
int("755", 8)Returns 493.

Python 3 does not accept old-style octal literals like 0755; you must write 0o755.

Library declarations
python
oct(x, /)
# x: int, or an object with __index__()

Parameters

ParameterDescription
xRequired. An int or an object with __index__().

Return Value

Return ValueDescription
strOctal digits prefixed with 0o (or -0o).

Python Examples

Example 1 — Octal Strings for Several Integers

Convert a few integers and notice how each octal place is a power of 8.

python
for n in [8, 64, 100, 511, -8]:
    print(n, oct(n))

How It Works

  1. Powers of 8. 8 is written 10 and 64 (8 * 8) is 100 in octal.
  2. Mixed digits. 100 is 1 * 64 + 4 * 8 + 4, which reads as 144.
  3. Largest three digits. 511 is 777: nine 1 bits, three per octal digit.
  4. Negatives. The minus sign goes in front of the prefix: -0o10.

Example 2 — Read Unix Permission Bits

Split the classic 0o755 mode into owner, group, and others, and test a flag from the stat module.

python
import stat

mode = 0o755
print(oct(mode))
print(mode)
print(f"{mode:o}")

owner = (mode >> 6) & 0o7
group = (mode >> 3) & 0o7
others = mode & 0o7
print("owner/group/others:", owner, group, others)
print("owner can execute:", bool(mode & stat.S_IXUSR))

How It Works

  1. Same number, two views. 0o755 is the int 493; oct() shows it in the form people use for permissions.
  2. No prefix. The :o format spec prints the bare digits 755.
  3. Three bits per digit. Shifting right by 6 and 3 and masking with 0o7 extracts each permission digit.
  4. Named flags. stat.S_IXUSR is the owner-execute bit (0o100), and it is set in 755.

Example 3 — Parse Octal Text Back into Integers

Read octal strings with and without the prefix, and catch an invalid digit.

python
text = "0o17"

print(int(text, 8))
print(int("17", 8))
print(int(text, 0))
print(oct(int("777", 8)) == "0o777")

try:
    int("19", 8)
except ValueError:
    print("9 is not an octal digit")

How It Works

  1. Base 8 with prefix. int("0o17", 8) accepts the optional prefix and returns 1 * 8 + 7 = 15.
  2. Bare digits. int("17", 8) gives the same 15.
  3. Base 0 detects. With base 0, Python reads the 0o prefix to decide the base by itself.
  4. Invalid digit. Octal digits are 0 to 7, so "19" raises ValueError.

Frequently Asked Questions

A string with the octal digits of an integer and the prefix 0o, for example oct(8) returns '0o10'.
Use format(n, "o") or f"{n:o}".
Each octal digit holds exactly three bits, which matches the read, write, and execute bits for owner, group, and others.
Use int("755", 8). With the prefix, int("0o755", 0) or int("0o755", 8) also work.
No, it is a SyntaxError. Python 3 requires the 0o prefix: 0o755.

Next: open()

Open a file for reading or writing.

open() →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

11 people found this page helpful