oct() function is one of the built-in methods in Python3. The oct() method takes an integer and returns it’s octal representation in a string format.
Syntax : oct(x)
x – Must be an integer number and can be in either binary, decimal or hexadecimal format.
Returns : octal representation of the value.
Errors and Exceptions :
TypeError : Returns TypeError when anything other than integer type constants are passed as parameters.
Code #1 : Illustrates the use of oct() function.
The octal representation of 23 is 0o27 The octal representation of the ascii value of 'z' is 0o172 The octal representation of the binary of 23 is 0o27 The octal representation of the binary of 23 is 0o27
Code #2 : Demonstrate TypeError
Traceback (most recent call last): File "/home/5bf02b72de26687389763e9133669972.py", line 3, in print("The Octal representation of 29.5 is "+oct(29.5)) TypeError: 'float' object cannot be interpreted as an integer
oct() is used in all types of standard conversion. For example, Conversion from decimal to octal, binary to octal, hexadecimal to octal forms respectively.
Code #3 :
a. Hexadecimal to Octal b. Decimal to Octal c. Binary to Octal Enter your choice :- a Enter your input in HEX format :- 0x13 Octal form of 19 is 0o23
a. Hexadecimal to Octal b. Decimal to Octal c. Binary to Octal Enter your choice :- b Enter a number with base-10 format :- 123 Octal form of 123 is 0o173
- Python | hex() function
- Python | cmp() function
- Python | int() function
- Python | now() function
- sum() function in Python
- ord() function in Python
- id() function in Python
- Python | dir() function
- Python map() function
- Python | How to get function name ?
- Help function in Python
- vars() function in Python
- Python | bytearray() function
- Python | randint() function
- Sum 2D array in Python using map() function
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.