{"id":24835,"date":"2021-11-02T09:54:25","date_gmt":"2021-11-02T04:24:25","guid":{"rendered":"https:\/\/python-programs.com\/?p=24835"},"modified":"2021-11-05T20:38:13","modified_gmt":"2021-11-05T15:08:13","slug":"python-oct-function-with-examples","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-oct-function-with-examples\/","title":{"rendered":"Python oct() Function with Examples"},"content":{"rendered":"
In the previous article, we have discussed Python min() Function with Examples<\/a> The oct() function is used to convert an integer to an octal string.<\/p>\n Python prefixes octal strings with 0o.<\/p>\n Syntax:<\/strong><\/p>\n Parameters<\/strong><\/p>\n number:<\/strong> This is required. It is an integer number.<\/p>\n If the value is not an integer (binary, decimal, or hexadecimal), it should implement __index__() to return an integer.<\/p>\n Return Value:<\/strong><\/p>\n The oct() function takes an integer number and returns an octal string.<\/p>\n Examples:<\/strong><\/p>\n Example1:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Example2:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Fed up with searching various pages for the list of Python Built in Functions<\/a>? Look at the tutorial linked here and explore all coding samples of built-in functions of python.<\/p>\n In the previous article, we have discussed Python min() Function with Examples oct() Function in Python: The oct() function is used to convert an integer to an octal string. Python prefixes octal strings with 0o. Syntax: oct(number) Parameters number: This is required. It is an integer number. If the value is not an integer (binary, …<\/p>\n
\noct() Function in Python:<\/strong><\/p>\noct(number)<\/pre>\n
Given Number = 18<\/pre>\n
The given number's{ 18 } Octal Value = 0o22\r\nThe oct() function Return Type = <class 'str'><\/pre>\n
Given number\u00a0 = 13\r\nGiven binary number\u00a0 = 0b10\r\nGiven hexadecimal number\u00a0 = 0XB<\/pre>\n
The oct(13) value is = 0o15\r\nThe oct(0b10) value = 0o2\r\nThe oct(0XB) value = 0o13<\/pre>\n
oct() Function with Examples in Python<\/h2>\n
\n
Method #1: Using Built-in Functions (Static Input)<\/h3>\n
\n
# Give the number as static input and store it in a variable.\r\ngvn_numbr = 18\r\n# Pass the given number as an argument to the oct() function to get the\r\n# Octal value of a given number.\r\n# Store it in another variable.\r\nOctal_valu = oct(gvn_numbr)\r\n# Print the given number's Octal Value.\r\nprint(\"The given number's{\", gvn_numbr,\r\n \"} Octal Value = \", Octal_valu)\r\n# Print the return Type of oct() Function using the type() method by passing\r\n# the oct(given number) as an argument to it.\r\nprint(\"The oct() function Return Type =\", type(oct(gvn_numbr)))\r\n<\/pre>\n
The given number's{ 18 } Octal Value = 0o22\r\nThe oct() function Return Type = <class 'str'><\/pre>\n
oct() For binary and Hexadecimal numbers<\/h5>\n
# converting decimal to octal\r\nprint('The oct(13) value is = ', oct(13))\r\n\r\n# converting binary to octal\r\nprint('The oct(0b10) value = ', oct(0b10))\r\n\r\n# converting hexadecimal to octal\r\nprint('The oct(0XB) value = ', oct(0XB))\r\n<\/pre>\n
The oct(13) value is = 0o15\r\nThe oct(0b10) value = 0o2\r\nThe oct(0XB) value = 0o13<\/pre>\n
Method #2: Using Built-in Functions (User Input)<\/h3>\n
\n
# Give the number as user input using the int(input()) function and store it in a variable.\r\ngvn_numbr = int(input(\"Enter some random number = \"))\r\n# Pass the given number as an argument to the oct() function to get the\r\n# Octal value of a given number.\r\n# Store it in another variable.\r\nOctal_valu = oct(gvn_numbr)\r\n# Print the given number's Octal Value.\r\nprint(\"The given number's{\", gvn_numbr,\r\n \"} Octal Value = \", Octal_valu)\r\n# Print the return Type of oct() Function using the type() method by passing\r\n# the oct(given number) as an argument to it.\r\nprint(\"The oct() function Return Type =\", type(oct(gvn_numbr)))\r\n<\/pre>\n
Enter some random number = 10\r\nThe given number's{ 10 } Octal Value = 0o12\r\nThe oct() function Return Type = <class 'str'><\/pre>\n
\n