Convert String to bytes-Python
Last Updated :
27 Apr, 2025
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)
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 constructor
bytes() 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)
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 constructor
bytearray() 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 encoding
In 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)
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:
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
3 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