oct()

oct(number)
number :input integer number
Return the Octal number as string.

Examples

my_num=27
print(oct(my_num))  # 0o33

Using Float

This will generate TypeError
num=27.45
print(oct(num))
TypeError: 'float' object cannot be interpreted as an integer

Binary to Octal

print(oct(0b11011)) # 0o33
In above code the input binary number is equivalent to integer 27.

You can use bin() to get the binary output
print("Binary Number : ", bin(27)) # Binary Number :  0b11011

Hex to Octal

print(oct(0x1b)) # 0o33
In above code the input Hex number is equivalent to integer 27 You can use hex() to get the Hex output
print("Hex  Number : ", hex(27)) # Hex  Number :  0x1b
In our octal output, 0o is always prefixed. As the output is a string we can remove it like this
my_oct=oct(27)
print(my_oct[2:]) # 33
Without using oct() and by using format()
my_num=27
print(format(my_num,'o')) # 33
All Built in Functions in Python
max() Bitwise operators int() hex() float()


Subhendu Mohapatra — author at plus2net
Subhendu Mohapatra

Author

🎥 Join me live on YouTube

Passionate about coding and teaching, I publish practical tutorials on PHP, Python, JavaScript, SQL, and web development. My goal is to make learning simple, engaging, and project‑oriented with real examples and source code.



Subscribe to our YouTube Channel here



plus2net.com







Python Video Tutorials
Python SQLite Video Tutorials
Python MySQL Video Tutorials
Python Tkinter Video Tutorials
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
©2000-2025   plus2net.com   All rights reserved worldwide Privacy Policy Disclaimer