Python Glossary Русский

all Python 3Built-in functionsterm

oct

numbers

Converts an integer to its octal string representation with the '0o' prefix.

Syntax

oct(x)

In Python 3 an octal literal must carry the 0o prefix — the old 0777 form seen in legacy examples is a SyntaxError. A common slip with file permissions: os.chmod expects the integer 0o755, not the string that oct() produced; use int(s, 8) to turn such a string back into a number.

Examples

print(oct(8))    # → 0o10
print(oct(255))  # → 0o377
print(oct(64))   # → 0o100
print(oct(-8))   # → -0o10
print(int(oct(8),8))  # → 8

See also

Open in the full glossarydocs.python.orgReport an inaccuracy