Python 3 reference · Built-in Functions
Python oct() Function
Convert an integer to an octal string prefixed with 0o, such as oct(8) giving '0o10'. Often used for file permissions.
Python 3 Since 3.0
Syntax
oct(x)
Parameters
| Parameter | Description |
|---|---|
x |
An integer. |
Return value
A string such as '0o755'.
Examples
print(oct(8), oct(493), oct(-56))
print(int("755", 8))
Output
0o10 0o755 -0o70 493
mode = 0o644 # rw-r--r--
print(oct(mode), f"{mode:o}")
Output
0o644 644
Version history
| Version | Change |
|---|---|
| Python 3.0 | Added |
Related
bin()· Convert an integer to a binary string prefixed with 0b, such as bin(5) giving '0b101'. Negative numbers get a minus sign.hex()· Convert an integer to a lowercase hexadecimal string prefixed with 0x, such as hex(255) giving '0xff'.int()· Convert a number or string to an integer. Floats are truncated toward zero, and strings can be in any base from 2 to 36.- Python Numbers · tutorial