Python oct() Function Last Updated : 30 Nov, 2023 Comments Improve Suggest changes Like Article Like Report Python oct() function takes an integer and returns the octal representation in a string format. In this article, we will see how we can convert an integer to an octal in Python. Python oct() Function SyntaxSyntax : oct(x) Parameters: 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 : Raises TypeError when anything other than integer type constants are passed as parameters.How oct() Function in Python works?In this example, we are using oct() to convert an integer to octal in Python. Like we have returned the octal representation of 10 here. Python3 print(oct(10)) Output0o12Example 1: Converting Binary and Hexadecimal Value Into Octal ValueIn this example, we are using oct() to convert numbers from different bases to octal. Python3 # Binary to Octal print(oct(0b110)) # Hexa to octal print(oct(0XB)) Output0o6 0o13Example 2: Python oct() for custom objectsImplementing __int__() magic method to support octal conversion in Math class. Python3 class Math: num = 76 def __index__(self): return self.num def __int__(self): return self.num obj = Math() print(oct(obj)) Output0o114Example 3: Converting Binary and Hexadecimal Value Into Octal Value Without “Oo” PrefixTo convert a binary or hexadecimal value to an octal representation using Python and remove the "0o" prefix, you can use the oct() function along with string slicing. Python3 a=10 result=oct(10)[2:] print(result) Output12Example 4: Demonstrate TypeError in oct() methodPython doesn't have anything like float.oct() to directly convert a floating type constant to its octal representation. Conversion of a floating-point value to it's octal is done manually. Python3 # Python3 program demonstrating TypeError print("The Octal representation of 29.5 is " + oct(29.5)) Output 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 integerApplications: Python oct() is used in all types of standard conversion. For example, Conversion from decimal to octal, binary to octal, hexadecimal to octal forms respectively. Comment More infoAdvertise with us Next Article Python oct() Function retr0 Follow Improve Article Tags : Misc Python Python-Built-in-functions Practice Tags : Miscpython Similar Reads Python String format() Method format() method in Python is a tool used to create formatted strings. By embedding variables or values into placeholders within a template string, we can construct dynamic, well-organized output. It replaces the outdated % formatting method, making string interpolation more readable and efficient. E 8 min read Python - globals() function In Python, the globals() function is used to return the global symbol table - a dictionary representing all the global variables in the current module or script. It provides access to the global variables that are defined in the current scope. This function is particularly useful when you want to in 2 min read Python hash() method Python hash() function is a built-in function and returns the hash value of an object if it has one. The hash value is an integer that is used to quickly compare dictionary keys while looking at a dictionary.Python hash() function SyntaxSyntax : hash(obj)Parameters : obj : The object which we need t 6 min read hex() function in Python hex() function in Python is used to convert an integer to its hexadecimal equivalent. It takes an integer as input and returns a string representing the number in hexadecimal format, starting with "0x" to indicate that it's in base-16. Example:Pythona = 255 res = hex(a) print(res)Output0xff Explanat 2 min read id() function in Python In Python, id() function is a built-in function that returns the unique identifier of an object. The identifier is an integer, which represents the memory address of the object. The id() function is commonly used to check if two variables or objects refer to the same memory location. Python id() Fun 3 min read Python 3 - input() function In Python, we use the input() function to take input from the user. Whatever you enter as input, the input function converts it into a string. If you enter an integer value still input() function converts it into a string.Python input() Function SyntaxSyntax: input(prompt)Parameter:Prompt: (optional 3 min read Python int() Function The Python int() function converts a given object to an integer or converts a decimal (floating-point) number to its integer part by truncating the fractional part.Example:In this example, we passed a string as an argument to the int() function and printed it.Pythonage = "21" print("age =", int(age) 3 min read Python len() Function The len() function in Python is used to get the number of items in an object. It is most commonly used with strings, lists, tuples, dictionaries and other iterable or container types. It returns an integer value representing the length or the number of elements. Example:Pythons = "GeeksforGeeks" # G 2 min read Python map() function The map() function is used to apply a given function to every item of an iterable, such as a list or tuple, and returns a map object (which is an iterator). Let's start with a simple example of using map() to convert a list of strings into a list of integers.Pythons = ['1', '2', '3', '4'] res = map( 4 min read Python - max() function Python max() function returns the largest item in an iterable or the largest of two or more arguments. It has two forms. max() function with objectsmax() function with iterablePython max() function With ObjectsUnlike the max() function of C/C++, the max() function in Python can take any type of obje 4 min read Like