Python Reference Library →

Python oct() Function – Convert Number to Octal

Let’s look at oct() function – convert number to octal

  1. Notice

    oct() returns a string containing an integer’s octal representation, including a 0o prefix.

  2. Try

    Follow the example, change its sample, and compare what happens.

  3. Check

    Explain the result and test your understanding.

Watch how it works

Focus on how the names, objects, and calculations change. We’ll inspect the printed output in the next section.

Explore the idea

Follow the code and watch its state change. Guided simulation.

Code
Python
number = 8
print(oct(number))
Live state

Top-level scope

number→Object 1
Object 1 · int8
This step

oct() returns a string containing an integer’s octal representation, including a 0o prefix.

Picture oct() function – convert number to octal

oct() returns a string containing an integer’s octal representation, including a 0o prefix.

Image brief

Alt text
A diagram for Python oct() Function – Convert Number to Octal. Instructions: number = 8 print(oct(number)). Displayed result: 0o10. The final example bindings are number --> object 1 (int). object 1: 8
Image generation prompt
Create a clean educational illustration for a Python programming lesson titled "Python oct() Function – Convert Number to Octal". Teaching focus: oct() returns a string containing an integer’s octal representation, including a 0o prefix. Depict exactly the relationships and labels in this reference diagram: EXAMPLE INSTRUCTIONS number = 8 print(oct(number)) | v DISPLAYED RESULT 0o10 FINAL BINDINGS number --> object 1 (int) object 1: 8 Use a flat vector style, generous spacing, clear arrows and legible labels, with a restrained blue/teal palette on a plain background. The diagram must explain the programming idea accurately. Do not depict variables as boxes containing copied objects when they share a reference. Avoid mascots, decorative screenshots, logos, gradients and extra text. Landscape composition, approximately 3:2, suitable for a course card. Accessible description: A diagram for Python oct() Function – Convert Number to Octal. Instructions: number = 8 print(oct(number)). Displayed result: 0o10. The final example bindings are number --> object 1 (int). object 1: 8

Follow the execution

Choose a different sample input, then follow every step through to the output. Before revealing it, predict what the program will display.

Execution workspace

Choose a sample and follow its execution. Guided simulation.

Code
Python
number = 8
print(oct(number))
Live state

Top-level scope

number→Object 1
Object 1 · int8
This step

The example has finished. Compare the final output with the values and references above.

Terminal
Output
0o10

What do you expect this program to print?

What does this example display?

number = 8
print(oct(number))
What does this example display?
Choose one answer. You can try again.

Answer and explanation

0o10

Follow the printed values in order. The output is: 0o10

More examples and reference

Python oct() Function

The oct() function in Python converts an integer number into its octal string representation, prefixed with 0o.

Octal numbers use base 8 and are often used in file permissions and low-level computing.

Syntax

oct(number)

Parameters:

  • number – A valid integer (positive or negative)

Returns:

  • A string that represents the octal value of the integer, prefixed with 0o

Example 1: Convert a Positive Integer to Octal

print(oct(10))
0o12

Explanation: 10 in decimal = 1×8 + 2 = 12 in octal

Example 2: Convert a Negative Integer

print(oct(-25))
-0o31

Explanation: The sign is preserved, and 25 in decimal = 3×8 + 1 = 31 in octal.

Example 3: Use with Hex or Binary Converted Values

print(oct(int('0xA', 16)))   # Hexadecimal to Octal
0o12

Explanation: 0xA (hex) = 10 (decimal) → 0o12 (octal)

Common Use Cases

  • Understanding file permission bits in Unix/Linux systems (e.g., chmod 755)
  • Working with low-level binary/octal representations
  • Debugging bitwise operations or memory layouts

Important Notes

  • oct() only accepts integers. Passing a float or string will raise a TypeError.
  • The result is always a string, starting with 0o.

Interview Tip

Functions like bin(), oct(), and hex() are often used in bit manipulation problems. Know how to use them and convert between bases.

Summary

  • oct(n) returns the octal string representation of an integer n.
  • The result starts with 0o and is always a string.
  • Useful in systems programming, permissions, and binary logic.

Practice Problem

Write a Python program that reads a number from the user and prints its binary, octal, and hexadecimal forms.

num = int(input("Enter a number: "))
print("Octal:", oct(num))
print("Binary:", bin(num))
print("Hexadecimal:", hex(num))

What to remember

Connect what you noticed in the example with the idea below.

Remember

oct() returns a string containing an integer’s octal representation, including a 0o prefix.

Watch out for this idea

oct() returns an integer written in decimal.

This is the misconception to avoid.

Reference checks are optional and do not count toward course progress.

    Reference library →