oct() Integer to octal string
Last updated: 2026-09-22
oct() Integer to octal string
oct() converts an integer to an octal string prefixed with '0o'.
| Category | Built-in Functions |
|---|---|
| Kind | Built-in Function |
| Python Version | all |
📝 Syntax
oct(x)
⚙️ Parameters
| x Required | An integer (int or an object implementing __index__). |
|---|
Returns:An octal string prefixed with '0o'.
💥 Raises
TypeError— Raised when the argument is not an integer.
▶ Example
Edit the code, press Run, and see the result in the output panel:
Output:
0o10
0o100
🏷️ Related Built-in Functions
💡 Related Cases
More case studies coming soon — check back later.
❓ FAQ
QWhat does oct(8) return?
AIt returns '0o10' — 8 in octal is 10 (carry at 8).
QWhat is the difference between oct() and format(8, 'o')?
Aoct() includes the 0o prefix; format(8, 'o') returns '10' with no prefix, and format(8, '#o') adds the prefix.
QDoes oct() accept floats?
ANo, it only accepts integers (or objects implementing __index__), otherwise it raises a TypeError.
QWhen would octal be used?
AIn scenarios like file permission notation (e.g. 0o755) or bitwise operations grouped in threes; in daily work it is mainly understood together with bin()/hex() for base conversion.