{"id":24558,"date":"2021-10-21T10:08:35","date_gmt":"2021-10-21T04:38:35","guid":{"rendered":"https:\/\/python-programs.com\/?p=24558"},"modified":"2021-11-05T19:37:57","modified_gmt":"2021-11-05T14:07:57","slug":"python-bytes-function-with-examples","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-bytes-function-with-examples\/","title":{"rendered":"Python bytes() Function with Examples"},"content":{"rendered":"
In the previous article, we have discussed Python Program for bool() Function<\/a> The bytes() function returns an object of type bytes.<\/p>\n The bytes() method returns a bytes object, which is an immutable (cannot be changed) sequence of integers ranging from 0 to 256.<\/p>\n It can either convert objects to bytes objects or create an empty bytes object of the specified size.<\/p>\n The distinction between bytes() and bytearray() is that bytes() return an object that cannot be modified, whereas bytearray() returns a modifiable object.<\/p>\n Syntax:<\/strong><\/p>\n Parameters<\/strong><\/p>\n x:<\/strong> When creating the bytes object, this is the source that will be used.<\/p>\n If it is an integer, it will generate an empty bytes object of the specified size.<\/p>\n If it’s a String, make sure to specify the source’s encoding.<\/p>\n encoding(optional):<\/strong> The string’s encoding.<\/p>\n error(optional):<\/strong> Specifies what should happen if the encoding fails.<\/p>\n Return Value:<\/strong><\/p>\n The bytes() method returns bytes object with the specified size and initialization parameters.<\/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 Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Find a Comprehensive Collection of Python Built in Functions<\/a> that you need to be aware of and use them as a part of your program.<\/p>\n In the previous article, we have discussed Python Program for bool() Function bytes() Function in Python: The bytes() function returns an object of type bytes. The bytes() method returns a bytes object, which is an immutable (cannot be changed) sequence of integers ranging from 0 to 256. It can either convert objects to bytes objects …<\/p>\n
\nbytes() Function in Python:<\/strong><\/p>\nbytes(x, encoding, error)<\/pre>\n
Given number = 6<\/pre>\n
The result after applying the bytes() method to the given number 6 = b'\\x00\\x00\\x00\\x00\\x00\\x00'<\/pre>\n
Given string = \"welcome to Python-programs\"<\/pre>\n
The result after applying\u00a0the bytes() method to the given string = b'welcome to Python-programs'<\/pre>\n
Program for bytes() Function in Python<\/h2>\n
\n
Method #1: Using Built-in Functions (Static Input)<\/h3>\n
1) For Numbers<\/h4>\n
\n
# Give the number as static input and store it in a variable.\r\ngvn_numb = 6\r\n# Apply bytes() method to the given number that returns a bytes object,\r\n# which is an immutable (cannot be changed) sequence of integers ranging\r\n# from 0 to 256.\r\n# Store it in another variable.\r\nrslt = bytes(gvn_numb)\r\n# Print the result after applying\u00a0the bytes() method to the given number.\r\nprint(\"The result after applying the bytes() method to the given number\",\r\n gvn_numb, \"= \", rslt)\r\n<\/pre>\n
The result after applying the bytes() method to the given number 6 = b'\\x00\\x00\\x00\\x00\\x00\\x00'<\/pre>\n
2) For Strings<\/h4>\n
\n
# Give the string as static input and store it in a variable.\r\ngvn_str = \"welcome to Python-programs\"\r\n# Apply bytes() method to the given string that returns an immutable bytes\r\n# object with the specified size and data.\r\n# Store it in another variable.\r\nrslt = bytes(gvn_str, 'utf-8')\r\n# Print the result after applying\u00a0the bytes() method to the given number.\r\nprint(\"The result after applying\u00a0the bytes() method to the given string = \", rslt)\r\n<\/pre>\n
The result after applying the bytes() method to the given string = b'welcome to Python-programs'<\/pre>\n
Method #2: Using Built-in Functions (User Input)<\/h3>\n
1) For Numbers<\/h4>\n
\n
# Give the number as user input using the int(input()) function and store it in a variable.\r\ngvn_numb = int(input(\"Enter some random number = \"))\r\n# Apply bytes() method to the given number that returns a bytes object,\r\n# which is an immutable (cannot be changed) sequence of integers ranging\r\n# from 0 to 256.\r\n# Store it in another variable.\r\nrslt = bytes(gvn_numb)\r\n# Print the result after applying\u00a0the bytes() method to the given number.\r\nprint(\"The result after applying the bytes() method to the given number\",\r\n gvn_numb, \"= \", rslt)\r\n<\/pre>\n
Enter some random number = 3\r\nThe result after applying the bytes() method to the given number 3 = b'\\x00\\x00\\x00'<\/pre>\n
2) For Strings<\/h4>\n
\n
# Give the string as user input using the input() function and store it in a variable.\r\ngvn_str = input(\"Enter some random string = \")\r\n# Apply bytes() method to the given string that returns an immutable bytes\r\n# object with the specified size and data.\r\n# Store it in another variable.\r\nrslt = bytes(gvn_str, 'utf-8')\r\n# Print the result after applying\u00a0the bytes() method to the given number.\r\nprint(\"The result after applying the bytes() method to the given string = \", rslt)\r\n<\/pre>\n
Enter some random string = hello this is btechgeeks\r\nThe result after applying the bytes() method to the given string = b'hello this is btechgeeks'<\/pre>\n
\n