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 | How to get function name ?
- Python | dir() function
- Python | cmp() function
- Python | int() function
- Help function in Python
- sum() function in Python
- id() function in Python
- Python | now() function
- Python map() function
- ord() function in Python
- Python | hex() function
- Python reversed() function
- vars() function in Python
- Python | math.gcd() function
- Python range() 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 firstname.lastname@example.org. 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.