Convert String to bytes-Python Last Updated : 27 Apr, 2025 Comments Improve Suggest changes Like Article Like Report The goal here is to convert a string into bytes in Python. This is essential for working with binary data or when encoding strings for storage or transmission. For example, given the string "Hello", these methods can convert it into a byte representation like b'Hello'. Let’s explore different methods to accomplish this efficiently.Using encode()encode() method is a very straightforward way to convert a string into bytes. It turns a string into a sequence of bytes using a specified encoding format (default is UTF-8). Python s = "Hello, World!" res = s.encode("utf-8") print(res) Outputb'Hello, World!' Explanation: encode() method converts a string to bytes using the specified encoding, here "utf-8", allowing the string to be processed as binary data.Using bytes constructorbytes() constructor also converts a string into bytes. This method is very similar to encode(), but it’s slightly longer to write. Python s = "Hello, World!" res = bytes(s, "utf-8") print(res) Outputb'Hello, World!' Explanation: bytes() constructor converts a string to bytes using the specified encoding, here "utf-8", allowing the string to be processed as binary data.Using bytearray constructorbytearray() constructor works just like bytes(), but it creates a mutable sequence of bytes. This means you can modify the byte data later, which is different from the immutability of a bytes object. Python s = "Hello, World!" res = bytearray(s, "utf-8") print(res) Outputbytearray(b'Hello, World!') Explanation: bytearray() constructor converts a string to a mutable sequence of bytes using the specified encoding, here "utf-8". This allows the byte data to be modified later.Using manual ASCII encodingIn this method, we manually convert each character in the string into its ASCII value using the ord() function, then combine them into a byte sequence. Python s = "Hello" res = bytes([ord(char) for char in s]) print(res) Outputb'Hello' Explanation: List comprehension converts each character in "Hello" to its ASCII value using ord() and the bytes() constructor then converts these values into a bytes object.Related Articles:Python – Strings encode() methodbytearray() function – PythonPython bytes() method Comment More infoAdvertise with us Next Article Convert String to bytes-Python manjeet_04 Follow Improve Article Tags : Python Python Programs Python string-programs Practice Tags : python Similar Reads Convert Bytes To Json using Python When dealing with complex byte data in Python, converting it to JSON format is a common task. In this article, we will explore different approaches, each demonstrating how to handle complex byte input and showcasing the resulting JSON output. Convert Bytes To JSON in PythonBelow are some of the ways 2 min read Python - Convert String to unicode characters Convert String to Unicode characters means transforming a string into its corresponding Unicode representations. Unicode is a standard for encoding characters, assigning a unique code point to every character.For example:string "A" has the Unicode code point U+0041.string "ä½ å¥½" corresponds to U+4F60 2 min read Convert Bytearray to Hexadecimal String - Python We are given a byte array a=bytearray([15, 255, 100, 56]) we need to convert it into a hexadecimal string that looks like: 0fff6438. Python provides multiple ways to do this:Using the .hex() method (built-in)Using binascii.hexlify()Using format() with join()Letâs explore each method with examples.Us 2 min read Integer to Binary String in Python We have an Integer and we need to convert the integer to binary string and print as a result. In this article, we will see how we can convert the integer into binary string using some generally used methods.Example: Input : 77Output : 0b1001101Explanation: Here, we have integer 77 which we converted 4 min read .to_bytes() in Python In Python, the .to_bytes() method is used to convert an integer into its byte representation. This is useful when weneed to store or transmit data in binary format.Example: Convert the integer 10 into bytesPythonnum = 10 byte_data = num.to_bytes(2, 'big') print(byte_data) Outputb'\x00\n' Explanation 2 min read Python - Interconvert Tuple to Byte Integer Sometimes, while working with Python data, we can have a problem in which we need to perform conversion of tuple values, into combined byte and then to integer and vice-versa. This kind of problem can have application in data domains. Let's discuss certain ways in which this task can be performed. I 2 min read Python program to convert a byte string to a list of integers We have to convert a byte string to a list of integers extracts the byte values (ASCII codes) from the byte string and stores them as integers in a list. For Example, we are having a byte string s=b"Hello" we need to write a program to convert this string to list of integers so the output should be 2 min read Convert Floating to Binary - Python The task of converting a floating-point number to its binary representation in Python involves representing the number in the IEEE 754 format, which consists of a sign bit, an exponent and a mantissa. For example, given the floating-point number 10.75, its IEEE 754 32-bit binary representation is "0 3 min read Ways to Convert List of ASCII Value to String - Python The task of converting a list of ASCII values to a string in Python involves transforming each integer in the list, which represents an ASCII code, into its corresponding character. For example, with the list a = [71, 101, 101, 107, 115], the goal is to convert each value into a character, resulting 3 min read Python program to convert binary to ASCII In this article, we are going to see the conversion of Binary to ASCII in the Python programming language. There are multiple approaches by which this conversion can be performed that are illustrated below: Method 1: By using binascii module Binascii helps convert between binary and various ASCII-en 3 min read Like