ID EN
Built-in Functions

oct

Python 3.11

Convert an integer number to an octal string prefixed with “0o”. The result is a valid Python expression. If x is not a Python int object, it has to define an __index__() method that returns an integer. For example:

Syntax

PYTHON
oct(x)

Examples

Example 1
PYTHON
>>> oct(8)
'0o10'
>>> oct(-56)
'-0o70'
Example 2
PYTHON
>>> '%o' % 10, '%o' % 10
('0o12', '12')
>>> format(10, 'o'), format(10, 'o')
('0o12', '12')
>>> f'{10:o}', f'{10:o}'
('0o12', '12')

See Also

int __index__() format()